[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 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; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 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. */ |
---|
[5ebff60] | 36 | static char *clean_handle(const char *orig) |
---|
[b7d3cc34] | 37 | { |
---|
[5ebff60] | 38 | char *new = g_malloc(strlen(orig) + 1); |
---|
[5b52a48] | 39 | int i = 0; |
---|
[5ebff60] | 40 | |
---|
[5b52a48] | 41 | do { |
---|
[5ebff60] | 42 | if (*orig != ' ') { |
---|
| 43 | new[i++] = g_ascii_tolower(*orig); |
---|
| 44 | } |
---|
| 45 | } while (*(orig++)); |
---|
| 46 | |
---|
[5b52a48] | 47 | return new; |
---|
| 48 | } |
---|
| 49 | |
---|
[5ebff60] | 50 | void nick_set_raw(account_t *acc, const char *handle, const char *nick) |
---|
[5b52a48] | 51 | { |
---|
[5ebff60] | 52 | char *store_handle, *store_nick = g_malloc(MAX_NICK_LENGTH + 1); |
---|
[e277e80] | 53 | irc_t *irc = (irc_t *) acc->bee->ui_data; |
---|
[5ebff60] | 54 | |
---|
| 55 | store_handle = clean_handle(handle); |
---|
[b1f818b] | 56 | store_nick[MAX_NICK_LENGTH] = '\0'; |
---|
[5ebff60] | 57 | strncpy(store_nick, nick, MAX_NICK_LENGTH); |
---|
| 58 | nick_strip(irc, store_nick); |
---|
| 59 | |
---|
| 60 | g_hash_table_replace(acc->nicks, store_handle, store_nick); |
---|
[b7d3cc34] | 61 | } |
---|
| 62 | |
---|
[5ebff60] | 63 | void nick_set(bee_user_t *bu, const char *nick) |
---|
[b1f818b] | 64 | { |
---|
[5ebff60] | 65 | nick_set_raw(bu->ic->acc, bu->handle, nick); |
---|
[b1f818b] | 66 | } |
---|
| 67 | |
---|
[5ebff60] | 68 | char *nick_get(bee_user_t *bu) |
---|
[b7d3cc34] | 69 | { |
---|
[5ebff60] | 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; |
---|
[5ebff60] | 73 | |
---|
| 74 | memset(nick, 0, MAX_NICK_LENGTH + 1); |
---|
| 75 | |
---|
| 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. */ |
---|
[5ebff60] | 79 | if ((found_nick = g_hash_table_lookup(bu->ic->acc->nicks, store_handle))) { |
---|
| 80 | strncpy(nick, found_nick, MAX_NICK_LENGTH); |
---|
| 81 | } else if ((found_nick = nick_gen(bu))) { |
---|
| 82 | strncpy(nick, found_nick, MAX_NICK_LENGTH); |
---|
| 83 | g_free(found_nick); |
---|
| 84 | } else { |
---|
[2e0eaac] | 85 | /* Keep this fallback since nick_gen() can return NULL in some cases. */ |
---|
[b7d3cc34] | 86 | char *s; |
---|
[5ebff60] | 87 | |
---|
| 88 | g_snprintf(nick, MAX_NICK_LENGTH, "%s", bu->handle); |
---|
| 89 | if ((s = strchr(nick, '@'))) { |
---|
| 90 | while (*s) { |
---|
[b7d3cc34] | 91 | *(s++) = 0; |
---|
[5ebff60] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | nick_strip(irc, nick); |
---|
| 96 | if (set_getbool(&bu->bee->set, "lcnicks")) { |
---|
| 97 | nick_lc(irc, nick); |
---|
| 98 | } |
---|
[b7d3cc34] | 99 | } |
---|
[5ebff60] | 100 | g_free(store_handle); |
---|
| 101 | |
---|
[d06eabf] | 102 | /* Make sure the nick doesn't collide with an existing one by adding |
---|
| 103 | underscores and that kind of stuff, if necessary. */ |
---|
[5ebff60] | 104 | nick_dedupe(bu, nick); |
---|
| 105 | |
---|
[d06eabf] | 106 | return nick; |
---|
| 107 | } |
---|
| 108 | |
---|
[5ebff60] | 109 | char *nick_gen(bee_user_t *bu) |
---|
[d06eabf] | 110 | { |
---|
[2e0eaac] | 111 | gboolean ok = FALSE; /* Set to true once the nick contains something unique. */ |
---|
[5ebff60] | 112 | GString *ret = g_string_sized_new(MAX_NICK_LENGTH + 1); |
---|
[c608891] | 113 | char *rets; |
---|
| 114 | irc_t *irc = (irc_t *) bu->bee->ui_data; |
---|
[5ebff60] | 115 | char *fmt = set_getstr(&bu->ic->acc->set, "nick_format") ? : |
---|
| 116 | set_getstr(&bu->bee->set, "nick_format"); |
---|
| 117 | |
---|
| 118 | while (fmt && *fmt && ret->len < MAX_NICK_LENGTH) { |
---|
[7e84168] | 119 | char *part = NULL, chop = '\0', *asc = NULL, *s; |
---|
| 120 | int len = INT_MAX; |
---|
[5ebff60] | 121 | |
---|
| 122 | if (*fmt != '%') { |
---|
| 123 | g_string_append_c(ret, *fmt); |
---|
| 124 | fmt++; |
---|
[2e0eaac] | 125 | continue; |
---|
| 126 | } |
---|
[5ebff60] | 127 | |
---|
| 128 | fmt++; |
---|
| 129 | while (*fmt) { |
---|
[2e0eaac] | 130 | /* -char means chop off everything from char */ |
---|
[5ebff60] | 131 | if (*fmt == '-') { |
---|
[2e0eaac] | 132 | chop = fmt[1]; |
---|
[5ebff60] | 133 | if (chop == '\0') { |
---|
| 134 | g_string_free(ret, TRUE); |
---|
[2e0eaac] | 135 | return NULL; |
---|
[9ed0081] | 136 | } |
---|
[2e0eaac] | 137 | fmt += 2; |
---|
[5ebff60] | 138 | } else if (g_ascii_isdigit(*fmt)) { |
---|
[7e84168] | 139 | len = 0; |
---|
| 140 | /* Grab a number. */ |
---|
[5ebff60] | 141 | while (g_ascii_isdigit(*fmt)) { |
---|
| 142 | len = len * 10 + (*(fmt++) - '0'); |
---|
| 143 | } |
---|
| 144 | } else if (g_strncasecmp(fmt, "nick", 4) == 0) { |
---|
[09dfb68] | 145 | part = bu->nick ? : bu->handle; |
---|
| 146 | fmt += 4; |
---|
| 147 | ok |= TRUE; |
---|
| 148 | break; |
---|
[5ebff60] | 149 | } else if (g_strncasecmp(fmt, "handle", 6) == 0) { |
---|
[2e0eaac] | 150 | part = bu->handle; |
---|
| 151 | fmt += 6; |
---|
| 152 | ok |= TRUE; |
---|
| 153 | break; |
---|
[5ebff60] | 154 | } else if (g_strncasecmp(fmt, "full_name", 9) == 0) { |
---|
[2e0eaac] | 155 | part = bu->fullname; |
---|
| 156 | fmt += 9; |
---|
| 157 | ok |= part && *part; |
---|
| 158 | break; |
---|
[5ebff60] | 159 | } else if (g_strncasecmp(fmt, "first_name", 10) == 0) { |
---|
[2e0eaac] | 160 | part = bu->fullname; |
---|
| 161 | fmt += 10; |
---|
| 162 | ok |= part && *part; |
---|
| 163 | chop = ' '; |
---|
| 164 | break; |
---|
[5ebff60] | 165 | } else if (g_strncasecmp(fmt, "group", 5) == 0) { |
---|
[09dfb68] | 166 | part = bu->group ? bu->group->name : NULL; |
---|
| 167 | fmt += 5; |
---|
| 168 | break; |
---|
[5ebff60] | 169 | } else if (g_strncasecmp(fmt, "account", 7) == 0) { |
---|
[3b3c50d9] | 170 | part = bu->ic->acc->tag; |
---|
| 171 | fmt += 7; |
---|
| 172 | break; |
---|
[5ebff60] | 173 | } else { |
---|
| 174 | g_string_free(ret, TRUE); |
---|
[2e0eaac] | 175 | return NULL; |
---|
| 176 | } |
---|
| 177 | } |
---|
[5ebff60] | 178 | |
---|
| 179 | if (!part) { |
---|
[c608891] | 180 | continue; |
---|
[5ebff60] | 181 | } |
---|
| 182 | |
---|
[badd148] | 183 | /* Credits to Josay_ in #bitlbee for this idea. //TRANSLIT |
---|
| 184 | should do lossy/approximate conversions, so letters with |
---|
| 185 | accents don't just get stripped. Note that it depends on |
---|
| 186 | LC_CTYPE being set to something other than C/POSIX. */ |
---|
[5ebff60] | 187 | if (!(irc && irc->status & IRC_UTF8_NICKS)) { |
---|
| 188 | part = asc = g_convert_with_fallback(part, -1, "ASCII//TRANSLIT", |
---|
| 189 | "UTF-8", "", NULL, NULL, NULL); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | if (part && chop && (s = strchr(part, chop))) { |
---|
| 193 | len = MIN(len, s - part); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | if (part) { |
---|
| 197 | if (len < INT_MAX) { |
---|
| 198 | g_string_append_len(ret, part, len); |
---|
| 199 | } else { |
---|
| 200 | g_string_append(ret, part); |
---|
| 201 | } |
---|
[7e84168] | 202 | } |
---|
[5ebff60] | 203 | g_free(asc); |
---|
[2e0eaac] | 204 | } |
---|
[5ebff60] | 205 | |
---|
| 206 | rets = g_string_free(ret, FALSE); |
---|
| 207 | if (ok && rets && *rets) { |
---|
| 208 | nick_strip(irc, rets); |
---|
| 209 | truncate_utf8(rets, MAX_NICK_LENGTH); |
---|
[c608891] | 210 | return rets; |
---|
| 211 | } |
---|
[5ebff60] | 212 | g_free(rets); |
---|
[c608891] | 213 | return NULL; |
---|
[b1f818b] | 214 | } |
---|
| 215 | |
---|
[5ebff60] | 216 | void nick_dedupe(bee_user_t *bu, char nick[MAX_NICK_LENGTH + 1]) |
---|
[b1f818b] | 217 | { |
---|
[5ebff60] | 218 | irc_t *irc = (irc_t *) bu->bee->ui_data; |
---|
[d06eabf] | 219 | int inf_protection = 256; |
---|
[badd148] | 220 | irc_user_t *iu; |
---|
[5ebff60] | 221 | |
---|
[5b52a48] | 222 | /* Now, find out if the nick is already in use at the moment, and make |
---|
| 223 | subtle changes to make it unique. */ |
---|
[5ebff60] | 224 | while (!nick_ok(irc, nick) || |
---|
| 225 | ((iu = irc_user_by_name(irc, nick)) && iu->bu != bu)) { |
---|
| 226 | if (strlen(nick) < (MAX_NICK_LENGTH - 1)) { |
---|
| 227 | nick[strlen(nick) + 1] = 0; |
---|
[b7d3cc34] | 228 | nick[strlen(nick)] = '_'; |
---|
[5ebff60] | 229 | } else { |
---|
[fed4f76] | 230 | /* We've got no more space for underscores, |
---|
| 231 | so truncate it and replace the last three |
---|
| 232 | chars with a random "_XX" suffix */ |
---|
[5ebff60] | 233 | int len = truncate_utf8(nick, MAX_NICK_LENGTH - 3); |
---|
[fed4f76] | 234 | nick[len] = '_'; |
---|
[5ebff60] | 235 | g_snprintf(nick + len + 1, 3, "%2x", rand()); |
---|
[b7d3cc34] | 236 | } |
---|
[5ebff60] | 237 | |
---|
| 238 | if (inf_protection-- == 0) { |
---|
| 239 | g_snprintf(nick, MAX_NICK_LENGTH + 1, "xx%x", rand()); |
---|
| 240 | |
---|
| 241 | irc_rootmsg(irc, "Warning: Something went wrong while trying " |
---|
| 242 | "to generate a nickname for contact %s on %s.", |
---|
| 243 | bu->handle, bu->ic->acc->tag); |
---|
| 244 | irc_rootmsg(irc, "This might be a bug in BitlBee, or the result " |
---|
| 245 | "of a faulty nick_format setting. Will use %s " |
---|
| 246 | "instead.", nick); |
---|
| 247 | |
---|
[b7d3cc34] | 248 | break; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
[d323394c] | 253 | /* Just check if there is a nickname set for this buddy or if we'd have to |
---|
| 254 | generate one. */ |
---|
[5ebff60] | 255 | int nick_saved(bee_user_t *bu) |
---|
[d323394c] | 256 | { |
---|
| 257 | char *store_handle, *found; |
---|
[5ebff60] | 258 | |
---|
| 259 | store_handle = clean_handle(bu->handle); |
---|
| 260 | found = g_hash_table_lookup(bu->ic->acc->nicks, store_handle); |
---|
| 261 | g_free(store_handle); |
---|
| 262 | |
---|
[d323394c] | 263 | return found != NULL; |
---|
| 264 | } |
---|
| 265 | |
---|
[5ebff60] | 266 | void nick_del(bee_user_t *bu) |
---|
[b7d3cc34] | 267 | { |
---|
[5ebff60] | 268 | g_hash_table_remove(bu->ic->acc->nicks, bu->handle); |
---|
[b7d3cc34] | 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
[5ebff60] | 272 | void nick_strip(irc_t *irc, char *nick) |
---|
[b7d3cc34] | 273 | { |
---|
[c608891] | 274 | int len = 0; |
---|
[5ebff60] | 275 | |
---|
| 276 | if (irc && (irc->status & IRC_UTF8_NICKS)) { |
---|
[c608891] | 277 | gunichar c; |
---|
[5ebff60] | 278 | char *p = nick, *n, tmp[strlen(nick) + 1]; |
---|
| 279 | |
---|
| 280 | while (p && *p) { |
---|
| 281 | c = g_utf8_get_char_validated(p, -1); |
---|
| 282 | n = g_utf8_find_next_char(p, NULL); |
---|
| 283 | |
---|
| 284 | if ((c < 0x7f && !(strchr(nick_lc_chars, c) || |
---|
| 285 | strchr(nick_uc_chars, c))) || |
---|
| 286 | !g_unichar_isgraph(c)) { |
---|
| 287 | strcpy(tmp, n); |
---|
| 288 | strcpy(p, tmp); |
---|
| 289 | } else { |
---|
[c608891] | 290 | p = n; |
---|
[5ebff60] | 291 | } |
---|
[c608891] | 292 | } |
---|
[5ebff60] | 293 | if (p) { |
---|
[c608891] | 294 | len = p - nick; |
---|
[5ebff60] | 295 | } |
---|
| 296 | } else { |
---|
[c608891] | 297 | int i; |
---|
[5ebff60] | 298 | |
---|
| 299 | for (i = len = 0; nick[i] && len < MAX_NICK_LENGTH; i++) { |
---|
| 300 | if (strchr(nick_lc_chars, nick[i]) || |
---|
| 301 | strchr(nick_uc_chars, nick[i])) { |
---|
[c608891] | 302 | nick[len] = nick[i]; |
---|
| 303 | len++; |
---|
| 304 | } |
---|
[b7d3cc34] | 305 | } |
---|
| 306 | } |
---|
[5ebff60] | 307 | if (g_ascii_isdigit(nick[0])) { |
---|
[0f47613] | 308 | char *orig; |
---|
[5ebff60] | 309 | |
---|
[c608891] | 310 | /* First character of a nick can't be a digit, so insert an |
---|
| 311 | underscore if necessary. */ |
---|
[5ebff60] | 312 | orig = g_strdup(nick); |
---|
| 313 | g_snprintf(nick, MAX_NICK_LENGTH, "_%s", orig); |
---|
| 314 | g_free(orig); |
---|
| 315 | len++; |
---|
[0f47613] | 316 | } |
---|
[5ebff60] | 317 | while (len <= MAX_NICK_LENGTH) { |
---|
[c608891] | 318 | nick[len++] = '\0'; |
---|
[5ebff60] | 319 | } |
---|
[b7d3cc34] | 320 | } |
---|
| 321 | |
---|
[5ebff60] | 322 | gboolean nick_ok(irc_t *irc, const char *nick) |
---|
[b7d3cc34] | 323 | { |
---|
[1eddf6b] | 324 | const char *s; |
---|
[5ebff60] | 325 | |
---|
[0f47613] | 326 | /* Empty/long nicks are not allowed, nor numbers at [0] */ |
---|
[5ebff60] | 327 | if (!*nick || g_ascii_isdigit(nick[0]) || strlen(nick) > MAX_NICK_LENGTH) { |
---|
[c608891] | 328 | return 0; |
---|
[5ebff60] | 329 | } |
---|
| 330 | |
---|
| 331 | if (irc && (irc->status & IRC_UTF8_NICKS)) { |
---|
[c608891] | 332 | gunichar c; |
---|
| 333 | const char *p = nick, *n; |
---|
[5ebff60] | 334 | |
---|
| 335 | while (p && *p) { |
---|
| 336 | c = g_utf8_get_char_validated(p, -1); |
---|
| 337 | n = g_utf8_find_next_char(p, NULL); |
---|
| 338 | |
---|
| 339 | if ((c < 0x7f && !(strchr(nick_lc_chars, c) || |
---|
| 340 | strchr(nick_uc_chars, c))) || |
---|
| 341 | !g_unichar_isgraph(c)) { |
---|
[c608891] | 342 | return FALSE; |
---|
| 343 | } |
---|
| 344 | p = n; |
---|
| 345 | } |
---|
[5ebff60] | 346 | } else { |
---|
| 347 | for (s = nick; *s; s++) { |
---|
| 348 | if (!strchr(nick_lc_chars, *s) && !strchr(nick_uc_chars, *s)) { |
---|
[c608891] | 349 | return FALSE; |
---|
[5ebff60] | 350 | } |
---|
| 351 | } |
---|
[c608891] | 352 | } |
---|
[5ebff60] | 353 | |
---|
[c608891] | 354 | return TRUE; |
---|
[b7d3cc34] | 355 | } |
---|
| 356 | |
---|
[5ebff60] | 357 | int nick_lc(irc_t *irc, char *nick) |
---|
[b7d3cc34] | 358 | { |
---|
[d323394c] | 359 | static char tab[128] = { 0 }; |
---|
[b7d3cc34] | 360 | int i; |
---|
[5ebff60] | 361 | |
---|
| 362 | if (tab['A'] == 0) { |
---|
| 363 | for (i = 0; nick_lc_chars[i]; i++) { |
---|
| 364 | tab[(int) nick_uc_chars[i]] = nick_lc_chars[i]; |
---|
| 365 | tab[(int) nick_lc_chars[i]] = nick_lc_chars[i]; |
---|
[b7d3cc34] | 366 | } |
---|
[5ebff60] | 367 | } |
---|
| 368 | |
---|
| 369 | if (irc && (irc->status & IRC_UTF8_NICKS)) { |
---|
| 370 | gchar *down = g_utf8_strdown(nick, -1); |
---|
| 371 | if (strlen(down) > strlen(nick)) { |
---|
| 372 | truncate_utf8(down, strlen(nick)); |
---|
[b7d3cc34] | 373 | } |
---|
[5ebff60] | 374 | strcpy(nick, down); |
---|
| 375 | g_free(down); |
---|
[c608891] | 376 | } |
---|
[5ebff60] | 377 | |
---|
| 378 | for (i = 0; nick[i]; i++) { |
---|
| 379 | if (((guchar) nick[i]) < 0x7f) { |
---|
| 380 | nick[i] = tab[(guchar) nick[i]]; |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | return nick_ok(irc, nick); |
---|
[b7d3cc34] | 385 | } |
---|
| 386 | |
---|
[5ebff60] | 387 | int nick_cmp(irc_t *irc, const char *a, const char *b) |
---|
[b7d3cc34] | 388 | { |
---|
| 389 | char aa[1024] = "", bb[1024] = ""; |
---|
[5ebff60] | 390 | |
---|
| 391 | strncpy(aa, a, sizeof(aa) - 1); |
---|
| 392 | strncpy(bb, b, sizeof(bb) - 1); |
---|
| 393 | if (nick_lc(irc, aa) && nick_lc(irc, bb)) { |
---|
| 394 | return(strcmp(aa, bb)); |
---|
| 395 | } else { |
---|
| 396 | return(-1); /* Hmm... Not a clear answer.. :-/ */ |
---|
[b7d3cc34] | 397 | } |
---|
| 398 | } |
---|