1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
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., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "bitlbee.h" |
---|
28 | |
---|
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 | |
---|
35 | /* Store handles in lower case and strip spaces, because AIM is braindead. */ |
---|
36 | static char *clean_handle(const char *orig) |
---|
37 | { |
---|
38 | char *new = g_malloc(strlen(orig) + 1); |
---|
39 | int i = 0; |
---|
40 | |
---|
41 | do { |
---|
42 | if (*orig != ' ') { |
---|
43 | new[i++] = g_ascii_tolower(*orig); |
---|
44 | } |
---|
45 | } while (*(orig++)); |
---|
46 | |
---|
47 | return new; |
---|
48 | } |
---|
49 | |
---|
50 | void nick_set_raw(account_t *acc, const char *handle, const char *nick) |
---|
51 | { |
---|
52 | char *store_handle, *store_nick = g_malloc(MAX_NICK_LENGTH + 1); |
---|
53 | irc_t *irc = (irc_t *) acc->bee->ui_data; |
---|
54 | |
---|
55 | store_handle = clean_handle(handle); |
---|
56 | store_nick[MAX_NICK_LENGTH] = '\0'; |
---|
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); |
---|
61 | } |
---|
62 | |
---|
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) |
---|
69 | { |
---|
70 | static char nick[MAX_NICK_LENGTH + 1]; |
---|
71 | char *store_handle, *found_nick; |
---|
72 | irc_t *irc = (irc_t *) bu->bee->ui_data; |
---|
73 | |
---|
74 | memset(nick, 0, MAX_NICK_LENGTH + 1); |
---|
75 | |
---|
76 | store_handle = clean_handle(bu->handle); |
---|
77 | /* Find out if we stored a nick for this person already. If not, try |
---|
78 | to generate a sane nick automatically. */ |
---|
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 { |
---|
85 | /* Keep this fallback since nick_gen() can return NULL in some cases. */ |
---|
86 | char *s; |
---|
87 | |
---|
88 | g_snprintf(nick, MAX_NICK_LENGTH, "%s", bu->handle); |
---|
89 | if ((s = strchr(nick, '@'))) { |
---|
90 | while (*s) { |
---|
91 | *(s++) = 0; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | nick_strip(irc, nick); |
---|
96 | if (set_getbool(&bu->bee->set, "lcnicks")) { |
---|
97 | nick_lc(irc, nick); |
---|
98 | } |
---|
99 | } |
---|
100 | g_free(store_handle); |
---|
101 | |
---|
102 | /* Make sure the nick doesn't collide with an existing one by adding |
---|
103 | underscores and that kind of stuff, if necessary. */ |
---|
104 | nick_dedupe(bu, nick); |
---|
105 | |
---|
106 | return nick; |
---|
107 | } |
---|
108 | |
---|
109 | char *nick_gen(bee_user_t *bu) |
---|
110 | { |
---|
111 | gboolean ok = FALSE; /* Set to true once the nick contains something unique. */ |
---|
112 | GString *ret = g_string_sized_new(MAX_NICK_LENGTH + 1); |
---|
113 | char *rets; |
---|
114 | irc_t *irc = (irc_t *) bu->bee->ui_data; |
---|
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) { |
---|
119 | char *part = NULL, chop = '\0', *asc = NULL, *s; |
---|
120 | int len = INT_MAX; |
---|
121 | |
---|
122 | if (*fmt != '%') { |
---|
123 | g_string_append_c(ret, *fmt); |
---|
124 | fmt++; |
---|
125 | continue; |
---|
126 | } |
---|
127 | |
---|
128 | fmt++; |
---|
129 | while (*fmt) { |
---|
130 | /* -char means chop off everything from char */ |
---|
131 | if (*fmt == '-') { |
---|
132 | chop = fmt[1]; |
---|
133 | if (chop == '\0') { |
---|
134 | g_string_free(ret, TRUE); |
---|
135 | return NULL; |
---|
136 | } |
---|
137 | fmt += 2; |
---|
138 | } else if (g_ascii_isdigit(*fmt)) { |
---|
139 | len = 0; |
---|
140 | /* Grab a number. */ |
---|
141 | while (g_ascii_isdigit(*fmt)) { |
---|
142 | len = len * 10 + (*(fmt++) - '0'); |
---|
143 | } |
---|
144 | } else if (g_strncasecmp(fmt, "nick", 4) == 0) { |
---|
145 | part = bu->nick ? : bu->handle; |
---|
146 | fmt += 4; |
---|
147 | ok |= TRUE; |
---|
148 | break; |
---|
149 | } else if (g_strncasecmp(fmt, "handle", 6) == 0) { |
---|
150 | part = bu->handle; |
---|
151 | fmt += 6; |
---|
152 | ok |= TRUE; |
---|
153 | break; |
---|
154 | } else if (g_strncasecmp(fmt, "full_name", 9) == 0) { |
---|
155 | part = bu->fullname; |
---|
156 | fmt += 9; |
---|
157 | ok |= part && *part; |
---|
158 | break; |
---|
159 | } else if (g_strncasecmp(fmt, "first_name", 10) == 0) { |
---|
160 | part = bu->fullname; |
---|
161 | fmt += 10; |
---|
162 | ok |= part && *part; |
---|
163 | chop = ' '; |
---|
164 | break; |
---|
165 | } else if (g_strncasecmp(fmt, "group", 5) == 0) { |
---|
166 | part = bu->group ? bu->group->name : NULL; |
---|
167 | fmt += 5; |
---|
168 | break; |
---|
169 | } else if (g_strncasecmp(fmt, "account", 7) == 0) { |
---|
170 | part = bu->ic->acc->tag; |
---|
171 | fmt += 7; |
---|
172 | break; |
---|
173 | } else { |
---|
174 | g_string_free(ret, TRUE); |
---|
175 | return NULL; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | if (!part) { |
---|
180 | continue; |
---|
181 | } |
---|
182 | |
---|
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. */ |
---|
187 | if (!(irc && irc->status & IRC_UTF8_NICKS)) { |
---|
188 | asc = g_convert_with_fallback(part, -1, "ASCII//TRANSLIT", "UTF-8", "", NULL, NULL, NULL); |
---|
189 | |
---|
190 | if (!asc) { |
---|
191 | /* If above failed, try again without //TRANSLIT. |
---|
192 | //TRANSLIT is a GNU iconv special and is not POSIX. |
---|
193 | Other platforms may not support it. */ |
---|
194 | asc = g_convert_with_fallback(part, -1, "ASCII", "UTF-8", "", NULL, NULL, NULL); |
---|
195 | } |
---|
196 | |
---|
197 | part = asc; |
---|
198 | } |
---|
199 | |
---|
200 | if (part && chop && (s = strchr(part, chop))) { |
---|
201 | len = MIN(len, s - part); |
---|
202 | } |
---|
203 | |
---|
204 | if (part) { |
---|
205 | if (len < INT_MAX) { |
---|
206 | g_string_append_len(ret, part, len); |
---|
207 | } else { |
---|
208 | g_string_append(ret, part); |
---|
209 | } |
---|
210 | } |
---|
211 | g_free(asc); |
---|
212 | } |
---|
213 | |
---|
214 | rets = g_string_free(ret, FALSE); |
---|
215 | if (ok && rets && *rets) { |
---|
216 | nick_strip(irc, rets); |
---|
217 | truncate_utf8(rets, MAX_NICK_LENGTH); |
---|
218 | return rets; |
---|
219 | } |
---|
220 | g_free(rets); |
---|
221 | return NULL; |
---|
222 | } |
---|
223 | |
---|
224 | /* Used for nicks and channel names too! */ |
---|
225 | void underscore_dedupe(char nick[MAX_NICK_LENGTH + 1]) |
---|
226 | { |
---|
227 | if (strlen(nick) < (MAX_NICK_LENGTH - 1)) { |
---|
228 | nick[strlen(nick) + 1] = 0; |
---|
229 | nick[strlen(nick)] = '_'; |
---|
230 | } else { |
---|
231 | /* We've got no more space for underscores, |
---|
232 | so truncate it and replace the last three |
---|
233 | chars with a random "_XX" suffix */ |
---|
234 | int len = truncate_utf8(nick, MAX_NICK_LENGTH - 3); |
---|
235 | nick[len] = '_'; |
---|
236 | g_snprintf(nick + len + 1, 3, "%2x", rand()); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | void nick_dedupe(bee_user_t *bu, char nick[MAX_NICK_LENGTH + 1]) |
---|
241 | { |
---|
242 | irc_t *irc = (irc_t *) bu->bee->ui_data; |
---|
243 | int inf_protection = 256; |
---|
244 | irc_user_t *iu; |
---|
245 | |
---|
246 | /* Now, find out if the nick is already in use at the moment, and make |
---|
247 | subtle changes to make it unique. */ |
---|
248 | while (!nick_ok(irc, nick) || |
---|
249 | ((iu = irc_user_by_name(irc, nick)) && iu->bu != bu)) { |
---|
250 | |
---|
251 | underscore_dedupe(nick); |
---|
252 | |
---|
253 | if (inf_protection-- == 0) { |
---|
254 | g_snprintf(nick, MAX_NICK_LENGTH + 1, "xx%x", rand()); |
---|
255 | |
---|
256 | irc_rootmsg(irc, "Warning: Something went wrong while trying " |
---|
257 | "to generate a nickname for contact %s on %s.", |
---|
258 | bu->handle, bu->ic->acc->tag); |
---|
259 | irc_rootmsg(irc, "This might be a bug in BitlBee, or the result " |
---|
260 | "of a faulty nick_format setting. Will use %s " |
---|
261 | "instead.", nick); |
---|
262 | |
---|
263 | break; |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | /* Just check if there is a nickname set for this buddy or if we'd have to |
---|
269 | generate one. */ |
---|
270 | int nick_saved(bee_user_t *bu) |
---|
271 | { |
---|
272 | char *store_handle, *found; |
---|
273 | |
---|
274 | store_handle = clean_handle(bu->handle); |
---|
275 | found = g_hash_table_lookup(bu->ic->acc->nicks, store_handle); |
---|
276 | g_free(store_handle); |
---|
277 | |
---|
278 | return found != NULL; |
---|
279 | } |
---|
280 | |
---|
281 | void nick_del(bee_user_t *bu) |
---|
282 | { |
---|
283 | g_hash_table_remove(bu->ic->acc->nicks, bu->handle); |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | void nick_strip(irc_t *irc, char *nick) |
---|
288 | { |
---|
289 | int len = 0; |
---|
290 | |
---|
291 | if (irc && (irc->status & IRC_UTF8_NICKS)) { |
---|
292 | gunichar c; |
---|
293 | char *p = nick, *n, tmp[strlen(nick) + 1]; |
---|
294 | |
---|
295 | while (p && *p) { |
---|
296 | c = g_utf8_get_char_validated(p, -1); |
---|
297 | n = g_utf8_find_next_char(p, NULL); |
---|
298 | |
---|
299 | if ((c < 0x7f && !(strchr(nick_lc_chars, c) || |
---|
300 | strchr(nick_uc_chars, c))) || |
---|
301 | !g_unichar_isgraph(c)) { |
---|
302 | strcpy(tmp, n); |
---|
303 | strcpy(p, tmp); |
---|
304 | } else { |
---|
305 | p = n; |
---|
306 | } |
---|
307 | } |
---|
308 | if (p) { |
---|
309 | len = p - nick; |
---|
310 | } |
---|
311 | } else { |
---|
312 | int i; |
---|
313 | |
---|
314 | for (i = len = 0; nick[i] && len < MAX_NICK_LENGTH; i++) { |
---|
315 | if (strchr(nick_lc_chars, nick[i]) || |
---|
316 | strchr(nick_uc_chars, nick[i])) { |
---|
317 | nick[len] = nick[i]; |
---|
318 | len++; |
---|
319 | } |
---|
320 | } |
---|
321 | } |
---|
322 | if (g_ascii_isdigit(nick[0])) { |
---|
323 | char *orig; |
---|
324 | |
---|
325 | /* First character of a nick can't be a digit, so insert an |
---|
326 | underscore if necessary. */ |
---|
327 | orig = g_strdup(nick); |
---|
328 | g_snprintf(nick, MAX_NICK_LENGTH, "_%s", orig); |
---|
329 | g_free(orig); |
---|
330 | len++; |
---|
331 | } |
---|
332 | while (len <= MAX_NICK_LENGTH) { |
---|
333 | nick[len++] = '\0'; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | gboolean nick_ok(irc_t *irc, const char *nick) |
---|
338 | { |
---|
339 | const char *s; |
---|
340 | |
---|
341 | /* Empty/long nicks are not allowed, nor numbers at [0] */ |
---|
342 | if (!*nick || g_ascii_isdigit(nick[0]) || strlen(nick) > MAX_NICK_LENGTH) { |
---|
343 | return 0; |
---|
344 | } |
---|
345 | |
---|
346 | if (irc && (irc->status & IRC_UTF8_NICKS)) { |
---|
347 | gunichar c; |
---|
348 | const char *p = nick, *n; |
---|
349 | |
---|
350 | while (p && *p) { |
---|
351 | c = g_utf8_get_char_validated(p, -1); |
---|
352 | n = g_utf8_find_next_char(p, NULL); |
---|
353 | |
---|
354 | if ((c < 0x7f && !(strchr(nick_lc_chars, c) || |
---|
355 | strchr(nick_uc_chars, c))) || |
---|
356 | !g_unichar_isgraph(c)) { |
---|
357 | return FALSE; |
---|
358 | } |
---|
359 | p = n; |
---|
360 | } |
---|
361 | } else { |
---|
362 | for (s = nick; *s; s++) { |
---|
363 | if (!strchr(nick_lc_chars, *s) && !strchr(nick_uc_chars, *s)) { |
---|
364 | return FALSE; |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | return TRUE; |
---|
370 | } |
---|
371 | |
---|
372 | int nick_lc(irc_t *irc, char *nick) |
---|
373 | { |
---|
374 | static char tab[128] = { 0 }; |
---|
375 | int i; |
---|
376 | |
---|
377 | if (tab['A'] == 0) { |
---|
378 | /* initialize table so nonchars are mapped to themselves */ |
---|
379 | for (i = 0; i < sizeof(tab); i++) { |
---|
380 | tab[i] = i; |
---|
381 | } |
---|
382 | /* replace uppercase chars with lowercase chars */ |
---|
383 | for (i = 0; nick_lc_chars[i]; i++) { |
---|
384 | tab[(int) nick_uc_chars[i]] = nick_lc_chars[i]; |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | if (irc && (irc->status & IRC_UTF8_NICKS)) { |
---|
389 | gchar *down = g_utf8_strdown(nick, -1); |
---|
390 | if (strlen(down) > strlen(nick)) { |
---|
391 | truncate_utf8(down, strlen(nick)); |
---|
392 | } |
---|
393 | strcpy(nick, down); |
---|
394 | g_free(down); |
---|
395 | } |
---|
396 | |
---|
397 | for (i = 0; nick[i]; i++) { |
---|
398 | if (((guchar) nick[i]) < 0x7f) { |
---|
399 | nick[i] = tab[(guchar) nick[i]]; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | return nick_ok(irc, nick); |
---|
404 | } |
---|
405 | |
---|
406 | int nick_cmp(irc_t *irc, const char *a, const char *b) |
---|
407 | { |
---|
408 | char aa[1024] = "", bb[1024] = ""; |
---|
409 | |
---|
410 | strncpy(aa, a, sizeof(aa) - 1); |
---|
411 | strncpy(bb, b, sizeof(bb) - 1); |
---|
412 | if (nick_lc(irc, aa) && nick_lc(irc, bb)) { |
---|
413 | return(strcmp(aa, bb)); |
---|
414 | } else { |
---|
415 | return(-1); /* Hmm... Not a clear answer.. :-/ */ |
---|
416 | } |
---|
417 | } |
---|