[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* Some stuff to fetch, save and handle nicknames for your buddies */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | This program is free software; you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License with |
---|
| 21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
| 22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #define BITLBEE_CORE |
---|
| 27 | #include "bitlbee.h" |
---|
| 28 | |
---|
[2e0eaac] | 29 | /* Character maps, _lc_[x] == _uc_[x] (but uppercase), according to the RFC's. |
---|
| 30 | With one difference, we allow dashes. These are used to do uc/lc conversions |
---|
| 31 | and strip invalid chars. */ |
---|
| 32 | static char *nick_lc_chars = "0123456789abcdefghijklmnopqrstuvwxyz{}^`-_|"; |
---|
| 33 | static char *nick_uc_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[]~`-_\\"; |
---|
| 34 | |
---|
[5b52a48] | 35 | /* Store handles in lower case and strip spaces, because AIM is braindead. */ |
---|
| 36 | static char *clean_handle( const char *orig ) |
---|
[b7d3cc34] | 37 | { |
---|
[5b52a48] | 38 | char *new = g_malloc( strlen( orig ) + 1 ); |
---|
| 39 | int i = 0; |
---|
[b7d3cc34] | 40 | |
---|
[5b52a48] | 41 | do { |
---|
| 42 | if (*orig != ' ') |
---|
| 43 | new[i++] = tolower( *orig ); |
---|
[b7d3cc34] | 44 | } |
---|
[5b52a48] | 45 | while (*(orig++)); |
---|
[b7d3cc34] | 46 | |
---|
[5b52a48] | 47 | return new; |
---|
| 48 | } |
---|
| 49 | |
---|
[b1f818b] | 50 | void nick_set_raw( account_t *acc, const char *handle, const char *nick ) |
---|
[5b52a48] | 51 | { |
---|
| 52 | char *store_handle, *store_nick = g_malloc( MAX_NICK_LENGTH + 1 ); |
---|
[e277e80] | 53 | irc_t *irc = (irc_t *) acc->bee->ui_data; |
---|
[b7d3cc34] | 54 | |
---|
[5b52a48] | 55 | store_handle = clean_handle( handle ); |
---|
[b1f818b] | 56 | store_nick[MAX_NICK_LENGTH] = '\0'; |
---|
[5b52a48] | 57 | strncpy( store_nick, nick, MAX_NICK_LENGTH ); |
---|
[e277e80] | 58 | nick_strip( irc, store_nick ); |
---|
[b7d3cc34] | 59 | |
---|
[5b52a48] | 60 | g_hash_table_replace( acc->nicks, store_handle, store_nick ); |
---|
[b7d3cc34] | 61 | } |
---|
| 62 | |
---|
[b1f818b] | 63 | void nick_set( bee_user_t *bu, const char *nick ) |
---|
| 64 | { |
---|
| 65 | nick_set_raw( bu->ic->acc, bu->handle, nick ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | char *nick_get( bee_user_t *bu ) |
---|
[b7d3cc34] | 69 | { |
---|
| 70 | static char nick[MAX_NICK_LENGTH+1]; |
---|
[5b52a48] | 71 | char *store_handle, *found_nick; |
---|
[e277e80] | 72 | irc_t *irc = (irc_t *) bu->bee->ui_data; |
---|
[b7d3cc34] | 73 | |
---|
| 74 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
| 75 | |
---|
[b1f818b] | 76 | store_handle = clean_handle( bu->handle ); |
---|
[5b52a48] | 77 | /* Find out if we stored a nick for this person already. If not, try |
---|
| 78 | to generate a sane nick automatically. */ |
---|
[b1f818b] | 79 | if( ( found_nick = g_hash_table_lookup( bu->ic->acc->nicks, store_handle ) ) ) |
---|
[5b52a48] | 80 | { |
---|
| 81 | strncpy( nick, found_nick, MAX_NICK_LENGTH ); |
---|
| 82 | } |
---|
[2e0eaac] | 83 | else if( ( found_nick = nick_gen( bu ) ) ) |
---|
| 84 | { |
---|
| 85 | strncpy( nick, found_nick, MAX_NICK_LENGTH ); |
---|
| 86 | g_free( found_nick ); |
---|
| 87 | } |
---|
[5b52a48] | 88 | else |
---|
[b7d3cc34] | 89 | { |
---|
[2e0eaac] | 90 | /* Keep this fallback since nick_gen() can return NULL in some cases. */ |
---|
[b7d3cc34] | 91 | char *s; |
---|
| 92 | |
---|
[b1f818b] | 93 | g_snprintf( nick, MAX_NICK_LENGTH, "%s", bu->handle ); |
---|
[b7d3cc34] | 94 | if( ( s = strchr( nick, '@' ) ) ) |
---|
| 95 | while( *s ) |
---|
| 96 | *(s++) = 0; |
---|
| 97 | |
---|
[e277e80] | 98 | nick_strip( irc, nick ); |
---|
[b1f818b] | 99 | if( set_getbool( &bu->bee->set, "lcnicks" ) ) |
---|
[e277e80] | 100 | nick_lc( irc, nick ); |
---|
[b7d3cc34] | 101 | } |
---|
[5b52a48] | 102 | g_free( store_handle ); |
---|
[b7d3cc34] | 103 | |
---|
[d06eabf] | 104 | /* Make sure the nick doesn't collide with an existing one by adding |
---|
| 105 | underscores and that kind of stuff, if necessary. */ |
---|
[b1f818b] | 106 | nick_dedupe( bu, nick ); |
---|
[d06eabf] | 107 | |
---|
| 108 | return nick; |
---|
| 109 | } |
---|
| 110 | |
---|
[b1f818b] | 111 | char *nick_gen( bee_user_t *bu ) |
---|
[d06eabf] | 112 | { |
---|
[2e0eaac] | 113 | gboolean ok = FALSE; /* Set to true once the nick contains something unique. */ |
---|
[c608891] | 114 | GString *ret = g_string_sized_new( MAX_NICK_LENGTH + 1 ); |
---|
| 115 | char *rets; |
---|
| 116 | irc_t *irc = (irc_t *) bu->bee->ui_data; |
---|
[2e0eaac] | 117 | char *fmt = set_getstr( &bu->ic->acc->set, "nick_format" ) ? : |
---|
| 118 | set_getstr( &bu->bee->set, "nick_format" ); |
---|
| 119 | |
---|
| 120 | while( fmt && *fmt && ret->len < MAX_NICK_LENGTH ) |
---|
| 121 | { |
---|
[7e84168] | 122 | char *part = NULL, chop = '\0', *asc = NULL, *s; |
---|
| 123 | int len = INT_MAX; |
---|
[2e0eaac] | 124 | |
---|
| 125 | if( *fmt != '%' ) |
---|
| 126 | { |
---|
| 127 | g_string_append_c( ret, *fmt ); |
---|
| 128 | fmt ++; |
---|
| 129 | continue; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | fmt ++; |
---|
| 133 | while( *fmt ) |
---|
| 134 | { |
---|
| 135 | /* -char means chop off everything from char */ |
---|
| 136 | if( *fmt == '-' ) |
---|
| 137 | { |
---|
| 138 | chop = fmt[1]; |
---|
| 139 | if( chop == '\0' ) |
---|
[9ed0081] | 140 | { |
---|
| 141 | g_string_free( ret, TRUE ); |
---|
[2e0eaac] | 142 | return NULL; |
---|
[9ed0081] | 143 | } |
---|
[2e0eaac] | 144 | fmt += 2; |
---|
| 145 | } |
---|
[7e84168] | 146 | else if( isdigit( *fmt ) ) |
---|
| 147 | { |
---|
| 148 | len = 0; |
---|
| 149 | /* Grab a number. */ |
---|
| 150 | while( isdigit( *fmt ) ) |
---|
| 151 | len = len * 10 + ( *(fmt++) - '0' ); |
---|
| 152 | } |
---|
[09dfb68] | 153 | else if( g_strncasecmp( fmt, "nick", 4 ) == 0 ) |
---|
| 154 | { |
---|
| 155 | part = bu->nick ? : bu->handle; |
---|
| 156 | fmt += 4; |
---|
| 157 | ok |= TRUE; |
---|
| 158 | break; |
---|
| 159 | } |
---|
[2e0eaac] | 160 | else if( g_strncasecmp( fmt, "handle", 6 ) == 0 ) |
---|
| 161 | { |
---|
| 162 | part = bu->handle; |
---|
| 163 | fmt += 6; |
---|
| 164 | ok |= TRUE; |
---|
| 165 | break; |
---|
| 166 | } |
---|
| 167 | else if( g_strncasecmp( fmt, "full_name", 9 ) == 0 ) |
---|
| 168 | { |
---|
| 169 | part = bu->fullname; |
---|
| 170 | fmt += 9; |
---|
| 171 | ok |= part && *part; |
---|
| 172 | break; |
---|
| 173 | } |
---|
| 174 | else if( g_strncasecmp( fmt, "first_name", 10 ) == 0 ) |
---|
| 175 | { |
---|
| 176 | part = bu->fullname; |
---|
| 177 | fmt += 10; |
---|
| 178 | ok |= part && *part; |
---|
| 179 | chop = ' '; |
---|
| 180 | break; |
---|
| 181 | } |
---|
[09dfb68] | 182 | else if( g_strncasecmp( fmt, "group", 5 ) == 0 ) |
---|
| 183 | { |
---|
| 184 | part = bu->group ? bu->group->name : NULL; |
---|
| 185 | fmt += 5; |
---|
| 186 | break; |
---|
| 187 | } |
---|
[3b3c50d9] | 188 | else if( g_strncasecmp( fmt, "account", 7 ) == 0 ) |
---|
| 189 | { |
---|
| 190 | part = bu->ic->acc->tag; |
---|
| 191 | fmt += 7; |
---|
| 192 | break; |
---|
| 193 | } |
---|
[2e0eaac] | 194 | else |
---|
| 195 | { |
---|
[9ed0081] | 196 | g_string_free( ret, TRUE ); |
---|
[2e0eaac] | 197 | return NULL; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
[c608891] | 201 | if( !part ) |
---|
| 202 | continue; |
---|
| 203 | |
---|
[badd148] | 204 | /* Credits to Josay_ in #bitlbee for this idea. //TRANSLIT |
---|
| 205 | should do lossy/approximate conversions, so letters with |
---|
| 206 | accents don't just get stripped. Note that it depends on |
---|
| 207 | LC_CTYPE being set to something other than C/POSIX. */ |
---|
[c608891] | 208 | if( !( irc && irc->status & IRC_UTF8_NICKS ) ) |
---|
[560129a] | 209 | part = asc = g_convert_with_fallback( part, -1, "ASCII//TRANSLIT", |
---|
| 210 | "UTF-8", "", NULL, NULL, NULL ); |
---|
[badd148] | 211 | |
---|
[7e84168] | 212 | if( chop && ( s = strchr( part, chop ) ) ) |
---|
| 213 | len = MIN( len, s - part ); |
---|
| 214 | |
---|
[c608891] | 215 | if( part ) |
---|
[7e84168] | 216 | { |
---|
| 217 | if( len < INT_MAX ) |
---|
| 218 | g_string_append_len( ret, part, len ); |
---|
| 219 | else |
---|
| 220 | g_string_append( ret, part ); |
---|
| 221 | } |
---|
[badd148] | 222 | g_free( asc ); |
---|
[2e0eaac] | 223 | } |
---|
| 224 | |
---|
[c608891] | 225 | rets = g_string_free( ret, FALSE ); |
---|
| 226 | if( ok && rets && *rets ) |
---|
| 227 | { |
---|
| 228 | nick_strip( irc, rets ); |
---|
| 229 | rets[MAX_NICK_LENGTH] = '\0'; |
---|
| 230 | return rets; |
---|
| 231 | } |
---|
| 232 | g_free( rets ); |
---|
| 233 | return NULL; |
---|
[b1f818b] | 234 | } |
---|
| 235 | |
---|
| 236 | void nick_dedupe( bee_user_t *bu, char nick[MAX_NICK_LENGTH+1] ) |
---|
| 237 | { |
---|
| 238 | irc_t *irc = (irc_t*) bu->bee->ui_data; |
---|
[d06eabf] | 239 | int inf_protection = 256; |
---|
[badd148] | 240 | irc_user_t *iu; |
---|
[d06eabf] | 241 | |
---|
[5b52a48] | 242 | /* Now, find out if the nick is already in use at the moment, and make |
---|
| 243 | subtle changes to make it unique. */ |
---|
[e277e80] | 244 | while( !nick_ok( irc, nick ) || |
---|
[badd148] | 245 | ( ( iu = irc_user_by_name( irc, nick ) ) && iu->bu != bu ) ) |
---|
[b7d3cc34] | 246 | { |
---|
| 247 | if( strlen( nick ) < ( MAX_NICK_LENGTH - 1 ) ) |
---|
| 248 | { |
---|
| 249 | nick[strlen(nick)+1] = 0; |
---|
| 250 | nick[strlen(nick)] = '_'; |
---|
| 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
| 254 | nick[0] ++; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | if( inf_protection-- == 0 ) |
---|
| 258 | { |
---|
| 259 | g_snprintf( nick, MAX_NICK_LENGTH + 1, "xx%x", rand() ); |
---|
| 260 | |
---|
[c608891] | 261 | irc_rootmsg( irc, "Warning: Something went wrong while trying " |
---|
| 262 | "to generate a nickname for contact %s on %s.", |
---|
| 263 | bu->handle, bu->ic->acc->tag ); |
---|
| 264 | irc_rootmsg( irc, "This might be a bug in BitlBee, or the result " |
---|
| 265 | "of a faulty nick_format setting. Will use %s " |
---|
| 266 | "instead.", nick ); |
---|
| 267 | |
---|
[b7d3cc34] | 268 | break; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
[d323394c] | 273 | /* Just check if there is a nickname set for this buddy or if we'd have to |
---|
| 274 | generate one. */ |
---|
[b1f818b] | 275 | int nick_saved( bee_user_t *bu ) |
---|
[d323394c] | 276 | { |
---|
| 277 | char *store_handle, *found; |
---|
| 278 | |
---|
[b1f818b] | 279 | store_handle = clean_handle( bu->handle ); |
---|
| 280 | found = g_hash_table_lookup( bu->ic->acc->nicks, store_handle ); |
---|
[d323394c] | 281 | g_free( store_handle ); |
---|
| 282 | |
---|
| 283 | return found != NULL; |
---|
| 284 | } |
---|
| 285 | |
---|
[b1f818b] | 286 | void nick_del( bee_user_t *bu ) |
---|
[b7d3cc34] | 287 | { |
---|
[b1f818b] | 288 | g_hash_table_remove( bu->ic->acc->nicks, bu->handle ); |
---|
[b7d3cc34] | 289 | } |
---|
| 290 | |
---|
| 291 | |
---|
[e277e80] | 292 | void nick_strip( irc_t *irc, char *nick ) |
---|
[b7d3cc34] | 293 | { |
---|
[c608891] | 294 | int len = 0; |
---|
[b7d3cc34] | 295 | |
---|
[c608891] | 296 | if( irc && ( irc->status & IRC_UTF8_NICKS ) ) |
---|
[b7d3cc34] | 297 | { |
---|
[c608891] | 298 | gunichar c; |
---|
| 299 | char *p = nick, *n, tmp[strlen(nick)+1]; |
---|
| 300 | |
---|
| 301 | while( p && *p ) |
---|
[b7d3cc34] | 302 | { |
---|
[c608891] | 303 | c = g_utf8_get_char_validated( p, -1 ); |
---|
| 304 | n = g_utf8_find_next_char( p, NULL ); |
---|
| 305 | |
---|
| 306 | if( ( c < 0x7f && !( strchr( nick_lc_chars, c ) || |
---|
| 307 | strchr( nick_uc_chars, c ) ) ) || |
---|
| 308 | !g_unichar_isgraph( c ) ) |
---|
| 309 | { |
---|
| 310 | strcpy( tmp, n ); |
---|
| 311 | strcpy( p, tmp ); |
---|
| 312 | } |
---|
| 313 | else |
---|
| 314 | p = n; |
---|
| 315 | } |
---|
| 316 | if( p ) |
---|
| 317 | len = p - nick; |
---|
| 318 | } |
---|
| 319 | else |
---|
| 320 | { |
---|
| 321 | int i; |
---|
| 322 | |
---|
| 323 | for( i = len = 0; nick[i] && len < MAX_NICK_LENGTH; i++ ) |
---|
| 324 | { |
---|
| 325 | if( strchr( nick_lc_chars, nick[i] ) || |
---|
| 326 | strchr( nick_uc_chars, nick[i] ) ) |
---|
| 327 | { |
---|
| 328 | nick[len] = nick[i]; |
---|
| 329 | len++; |
---|
| 330 | } |
---|
[b7d3cc34] | 331 | } |
---|
| 332 | } |
---|
[0f47613] | 333 | if( isdigit( nick[0] ) ) |
---|
| 334 | { |
---|
| 335 | char *orig; |
---|
| 336 | |
---|
[c608891] | 337 | /* First character of a nick can't be a digit, so insert an |
---|
| 338 | underscore if necessary. */ |
---|
[0f47613] | 339 | orig = g_strdup( nick ); |
---|
| 340 | g_snprintf( nick, MAX_NICK_LENGTH, "_%s", orig ); |
---|
| 341 | g_free( orig ); |
---|
[c608891] | 342 | len ++; |
---|
[0f47613] | 343 | } |
---|
[c608891] | 344 | while( len <= MAX_NICK_LENGTH ) |
---|
| 345 | nick[len++] = '\0'; |
---|
[b7d3cc34] | 346 | } |
---|
| 347 | |
---|
[c608891] | 348 | gboolean nick_ok( irc_t *irc, const char *nick ) |
---|
[b7d3cc34] | 349 | { |
---|
[1eddf6b] | 350 | const char *s; |
---|
[b7d3cc34] | 351 | |
---|
[0f47613] | 352 | /* Empty/long nicks are not allowed, nor numbers at [0] */ |
---|
| 353 | if( !*nick || isdigit( nick[0] ) || strlen( nick ) > MAX_NICK_LENGTH ) |
---|
[c608891] | 354 | return 0; |
---|
[b7d3cc34] | 355 | |
---|
[c608891] | 356 | if( irc && ( irc->status & IRC_UTF8_NICKS ) ) |
---|
| 357 | { |
---|
| 358 | gunichar c; |
---|
| 359 | const char *p = nick, *n; |
---|
| 360 | |
---|
| 361 | while( p && *p ) |
---|
| 362 | { |
---|
| 363 | c = g_utf8_get_char_validated( p, -1 ); |
---|
| 364 | n = g_utf8_find_next_char( p, NULL ); |
---|
| 365 | |
---|
| 366 | if( ( c < 0x7f && !( strchr( nick_lc_chars, c ) || |
---|
| 367 | strchr( nick_uc_chars, c ) ) ) || |
---|
| 368 | !g_unichar_isgraph( c ) ) |
---|
| 369 | { |
---|
| 370 | return FALSE; |
---|
| 371 | } |
---|
| 372 | p = n; |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | else |
---|
| 376 | { |
---|
| 377 | for( s = nick; *s; s ++ ) |
---|
| 378 | if( !strchr( nick_lc_chars, *s ) && !strchr( nick_uc_chars, *s ) ) |
---|
| 379 | return FALSE; |
---|
| 380 | } |
---|
[b7d3cc34] | 381 | |
---|
[c608891] | 382 | return TRUE; |
---|
[b7d3cc34] | 383 | } |
---|
| 384 | |
---|
[e277e80] | 385 | int nick_lc( irc_t *irc, char *nick ) |
---|
[b7d3cc34] | 386 | { |
---|
[d323394c] | 387 | static char tab[128] = { 0 }; |
---|
[b7d3cc34] | 388 | int i; |
---|
| 389 | |
---|
| 390 | if( tab['A'] == 0 ) |
---|
| 391 | for( i = 0; nick_lc_chars[i]; i ++ ) |
---|
| 392 | { |
---|
| 393 | tab[(int)nick_uc_chars[i]] = nick_lc_chars[i]; |
---|
| 394 | tab[(int)nick_lc_chars[i]] = nick_lc_chars[i]; |
---|
| 395 | } |
---|
| 396 | |
---|
[c608891] | 397 | if( irc && ( irc->status & IRC_UTF8_NICKS ) ) |
---|
[b7d3cc34] | 398 | { |
---|
[c608891] | 399 | gchar *down = g_utf8_strdown( nick, -1 ); |
---|
| 400 | if( strlen( down ) > strlen( nick ) ) |
---|
[b7d3cc34] | 401 | { |
---|
[c608891] | 402 | /* Well crap. Corrupt it if we have to. */ |
---|
| 403 | down[strlen(nick)] = '\0'; |
---|
[b7d3cc34] | 404 | } |
---|
[c608891] | 405 | strcpy( nick, down ); |
---|
| 406 | g_free( down ); |
---|
| 407 | } |
---|
[b7d3cc34] | 408 | |
---|
| 409 | for( i = 0; nick[i]; i ++ ) |
---|
[1a2c1c0] | 410 | if( nick[i] < 0x7f ) |
---|
| 411 | nick[i] = tab[(int)nick[i]]; |
---|
[b7d3cc34] | 412 | |
---|
[c608891] | 413 | return nick_ok( irc, nick ); |
---|
[b7d3cc34] | 414 | } |
---|
| 415 | |
---|
[e277e80] | 416 | int nick_cmp( irc_t *irc, const char *a, const char *b ) |
---|
[b7d3cc34] | 417 | { |
---|
| 418 | char aa[1024] = "", bb[1024] = ""; |
---|
| 419 | |
---|
| 420 | strncpy( aa, a, sizeof( aa ) - 1 ); |
---|
| 421 | strncpy( bb, b, sizeof( bb ) - 1 ); |
---|
[e277e80] | 422 | if( nick_lc( irc, aa ) && nick_lc( irc, bb ) ) |
---|
[b7d3cc34] | 423 | { |
---|
| 424 | return( strcmp( aa, bb ) ); |
---|
| 425 | } |
---|
| 426 | else |
---|
| 427 | { |
---|
| 428 | return( -1 ); /* Hmm... Not a clear answer.. :-/ */ |
---|
| 429 | } |
---|
| 430 | } |
---|