Changes in nick.c [0e788f5:c608891]
Legend:
- Unmodified
- Added
- Removed
-
nick.c
r0e788f5 rc608891 51 51 { 52 52 char *store_handle, *store_nick = g_malloc( MAX_NICK_LENGTH + 1 ); 53 irc_t *irc = (irc_t *) acc->bee->ui_data; 53 54 54 55 store_handle = clean_handle( handle ); 55 56 store_nick[MAX_NICK_LENGTH] = '\0'; 56 57 strncpy( store_nick, nick, MAX_NICK_LENGTH ); 57 nick_strip( store_nick );58 nick_strip( irc, store_nick ); 58 59 59 60 g_hash_table_replace( acc->nicks, store_handle, store_nick ); … … 69 70 static char nick[MAX_NICK_LENGTH+1]; 70 71 char *store_handle, *found_nick; 72 irc_t *irc = (irc_t *) bu->bee->ui_data; 71 73 72 74 memset( nick, 0, MAX_NICK_LENGTH + 1 ); … … 94 96 *(s++) = 0; 95 97 96 nick_strip( nick );98 nick_strip( irc, nick ); 97 99 if( set_getbool( &bu->bee->set, "lcnicks" ) ) 98 nick_lc( nick );100 nick_lc( irc, nick ); 99 101 } 100 102 g_free( store_handle ); … … 110 112 { 111 113 gboolean ok = FALSE; /* Set to true once the nick contains something unique. */ 112 GString *ret = g_string_new( "" ); 114 GString *ret = g_string_sized_new( MAX_NICK_LENGTH + 1 ); 115 char *rets; 116 irc_t *irc = (irc_t *) bu->bee->ui_data; 113 117 char *fmt = set_getstr( &bu->ic->acc->set, "nick_format" ) ? : 114 118 set_getstr( &bu->bee->set, "nick_format" ); … … 117 121 { 118 122 char *part = NULL, chop = '\0', *asc = NULL; 119 int len = MAX_NICK_LENGTH;120 123 121 124 if( *fmt != '%' ) … … 140 143 fmt += 2; 141 144 } 142 else if( isdigit( *fmt ) )143 {144 len = 0;145 /* Grab a number. */146 while( isdigit( *fmt ) )147 len = len * 10 + ( *(fmt++) - '0' );148 }149 145 else if( g_strncasecmp( fmt, "nick", 4 ) == 0 ) 150 146 { … … 195 191 } 196 192 193 if( !part ) 194 continue; 195 197 196 /* Credits to Josay_ in #bitlbee for this idea. //TRANSLIT 198 197 should do lossy/approximate conversions, so letters with 199 198 accents don't just get stripped. Note that it depends on 200 199 LC_CTYPE being set to something other than C/POSIX. */ 201 if( part)200 if( !( irc && irc->status & IRC_UTF8_NICKS ) ) 202 201 part = asc = g_convert_with_fallback( part, -1, "ASCII//TRANSLIT", 203 202 "UTF-8", "", NULL, NULL, NULL ); 204 203 205 if( ret->len == 0 && part && isdigit( *part ) ) 206 g_string_append_c( ret, '_' ); 207 208 while( part && *part && *part != chop && len > 0 ) 209 { 210 if( strchr( nick_lc_chars, *part ) || 211 strchr( nick_uc_chars, *part ) ) 212 g_string_append_c( ret, *part ); 213 214 part ++; 215 len --; 216 } 204 if( part ) 205 g_string_append( ret, part ); 217 206 g_free( asc ); 218 207 } 219 208 220 /* This returns NULL if the nick is empty or otherwise not ok. */ 221 return g_string_free( ret, ret->len == 0 || !ok ); 209 rets = g_string_free( ret, FALSE ); 210 if( ok && rets && *rets ) 211 { 212 nick_strip( irc, rets ); 213 rets[MAX_NICK_LENGTH] = '\0'; 214 return rets; 215 } 216 g_free( rets ); 217 return NULL; 222 218 } 223 219 … … 230 226 /* Now, find out if the nick is already in use at the moment, and make 231 227 subtle changes to make it unique. */ 232 while( !nick_ok( nick ) ||228 while( !nick_ok( irc, nick ) || 233 229 ( ( iu = irc_user_by_name( irc, nick ) ) && iu->bu != bu ) ) 234 230 { … … 245 241 if( inf_protection-- == 0 ) 246 242 { 247 int i;243 g_snprintf( nick, MAX_NICK_LENGTH + 1, "xx%x", rand() ); 248 244 249 irc_rootmsg( irc, "Warning: Almost had an infinite loop in nick_get()! " 250 "This used to be a fatal BitlBee bug, but we tried to fix it. " 251 "This message should *never* appear anymore. " 252 "If it does, please *do* send us a bug report! " 253 "Please send all the following lines in your report:" ); 254 255 irc_rootmsg( irc, "Trying to get a sane nick for handle %s", bu->handle ); 256 for( i = 0; i < MAX_NICK_LENGTH; i ++ ) 257 irc_rootmsg( irc, "Char %d: %c/%d", i, nick[i], nick[i] ); 258 259 irc_rootmsg( irc, "FAILED. Returning an insane nick now. Things might break. " 260 "Good luck, and please don't forget to paste the lines up here " 261 "in #bitlbee on OFTC or in a mail to wilmer@gaast.net" ); 262 263 g_snprintf( nick, MAX_NICK_LENGTH + 1, "xx%x", rand() ); 245 irc_rootmsg( irc, "Warning: Something went wrong while trying " 246 "to generate a nickname for contact %s on %s.", 247 bu->handle, bu->ic->acc->tag ); 248 irc_rootmsg( irc, "This might be a bug in BitlBee, or the result " 249 "of a faulty nick_format setting. Will use %s " 250 "instead.", nick ); 264 251 265 252 break; … … 287 274 288 275 289 void nick_strip( char *nick ) 290 { 291 int i, j; 292 293 for( i = j = 0; nick[i] && j < MAX_NICK_LENGTH; i++ ) 294 { 295 if( strchr( nick_lc_chars, nick[i] ) || 296 strchr( nick_uc_chars, nick[i] ) ) 297 { 298 nick[j] = nick[i]; 299 j++; 276 void nick_strip( irc_t *irc, char *nick ) 277 { 278 int len = 0; 279 280 if( irc && ( irc->status & IRC_UTF8_NICKS ) ) 281 { 282 gunichar c; 283 char *p = nick, *n, tmp[strlen(nick)+1]; 284 285 while( p && *p ) 286 { 287 c = g_utf8_get_char_validated( p, -1 ); 288 n = g_utf8_find_next_char( p, NULL ); 289 290 if( ( c < 0x7f && !( strchr( nick_lc_chars, c ) || 291 strchr( nick_uc_chars, c ) ) ) || 292 !g_unichar_isgraph( c ) ) 293 { 294 strcpy( tmp, n ); 295 strcpy( p, tmp ); 296 } 297 else 298 p = n; 299 } 300 if( p ) 301 len = p - nick; 302 } 303 else 304 { 305 int i; 306 307 for( i = len = 0; nick[i] && len < MAX_NICK_LENGTH; i++ ) 308 { 309 if( strchr( nick_lc_chars, nick[i] ) || 310 strchr( nick_uc_chars, nick[i] ) ) 311 { 312 nick[len] = nick[i]; 313 len++; 314 } 300 315 } 301 316 } … … 304 319 char *orig; 305 320 321 /* First character of a nick can't be a digit, so insert an 322 underscore if necessary. */ 306 323 orig = g_strdup( nick ); 307 324 g_snprintf( nick, MAX_NICK_LENGTH, "_%s", orig ); 308 325 g_free( orig ); 309 j++;310 } 311 while( j<= MAX_NICK_LENGTH )312 nick[ j++] = '\0';313 } 314 315 int nick_ok(const char *nick )326 len ++; 327 } 328 while( len <= MAX_NICK_LENGTH ) 329 nick[len++] = '\0'; 330 } 331 332 gboolean nick_ok( irc_t *irc, const char *nick ) 316 333 { 317 334 const char *s; … … 319 336 /* Empty/long nicks are not allowed, nor numbers at [0] */ 320 337 if( !*nick || isdigit( nick[0] ) || strlen( nick ) > MAX_NICK_LENGTH ) 321 return( 0 ); 322 323 for( s = nick; *s; s ++ ) 324 if( !strchr( nick_lc_chars, *s ) && !strchr( nick_uc_chars, *s ) ) 325 return( 0 ); 326 327 return( 1 ); 328 } 329 330 int nick_lc( char *nick ) 338 return 0; 339 340 if( irc && ( irc->status & IRC_UTF8_NICKS ) ) 341 { 342 gunichar c; 343 const char *p = nick, *n; 344 345 while( p && *p ) 346 { 347 c = g_utf8_get_char_validated( p, -1 ); 348 n = g_utf8_find_next_char( p, NULL ); 349 350 if( ( c < 0x7f && !( strchr( nick_lc_chars, c ) || 351 strchr( nick_uc_chars, c ) ) ) || 352 !g_unichar_isgraph( c ) ) 353 { 354 return FALSE; 355 } 356 p = n; 357 } 358 } 359 else 360 { 361 for( s = nick; *s; s ++ ) 362 if( !strchr( nick_lc_chars, *s ) && !strchr( nick_uc_chars, *s ) ) 363 return FALSE; 364 } 365 366 return TRUE; 367 } 368 369 int nick_lc( irc_t *irc, char *nick ) 331 370 { 332 371 static char tab[128] = { 0 }; … … 340 379 } 341 380 381 if( irc && ( irc->status & IRC_UTF8_NICKS ) ) 382 { 383 gchar *down = g_utf8_strdown( nick, -1 ); 384 if( strlen( down ) > strlen( nick ) ) 385 { 386 /* Well crap. Corrupt it if we have to. */ 387 down[strlen(nick)] = '\0'; 388 } 389 strcpy( nick, down ); 390 g_free( down ); 391 } 392 342 393 for( i = 0; nick[i]; i ++ ) 343 {344 if( !tab[(int)nick[i]] )345 return( 0 );346 347 394 nick[i] = tab[(int)nick[i]]; 348 } 349 350 return( 1 ); 351 } 352 353 int nick_uc( char *nick ) 354 { 355 static char tab[128] = { 0 }; 356 int i; 357 358 if( tab['A'] == 0 ) 359 for( i = 0; nick_lc_chars[i]; i ++ ) 360 { 361 tab[(int)nick_uc_chars[i]] = nick_uc_chars[i]; 362 tab[(int)nick_lc_chars[i]] = nick_uc_chars[i]; 363 } 364 365 for( i = 0; nick[i]; i ++ ) 366 { 367 if( !tab[(int)nick[i]] ) 368 return( 0 ); 369 370 nick[i] = tab[(int)nick[i]]; 371 } 372 373 return( 1 ); 374 } 375 376 int nick_cmp( const char *a, const char *b ) 395 396 return nick_ok( irc, nick ); 397 } 398 399 int nick_cmp( irc_t *irc, const char *a, const char *b ) 377 400 { 378 401 char aa[1024] = "", bb[1024] = ""; … … 380 403 strncpy( aa, a, sizeof( aa ) - 1 ); 381 404 strncpy( bb, b, sizeof( bb ) - 1 ); 382 if( nick_lc( aa ) && nick_lc(bb ) )405 if( nick_lc( irc, aa ) && nick_lc( irc, bb ) ) 383 406 { 384 407 return( strcmp( aa, bb ) ); … … 389 412 } 390 413 } 391 392 char *nick_dup( const char *nick )393 {394 return g_strndup( nick, MAX_NICK_LENGTH );395 }
Note: See TracChangeset
for help on using the changeset viewer.