1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Misc. stuff * |
---|
5 | * * |
---|
6 | * Copyright 2006-2010 Wilmer van der Gaast <wilmer@gaast.net> |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #include "jabber.h" |
---|
25 | #include "base64.h" |
---|
26 | |
---|
27 | static unsigned int next_id = 1; |
---|
28 | |
---|
29 | char *set_eval_priority(set_t *set, char *value) |
---|
30 | { |
---|
31 | account_t *acc = set->data; |
---|
32 | int i; |
---|
33 | |
---|
34 | if (sscanf(value, "%d", &i) == 1) { |
---|
35 | /* Priority is a signed 8-bit integer, according to RFC 3921. */ |
---|
36 | if (i < -128 || i > 127) { |
---|
37 | return SET_INVALID; |
---|
38 | } |
---|
39 | } else { |
---|
40 | return SET_INVALID; |
---|
41 | } |
---|
42 | |
---|
43 | /* Only run this stuff if the account is online ATM, |
---|
44 | and if the setting seems to be acceptable. */ |
---|
45 | if (acc->ic) { |
---|
46 | /* Although set_eval functions usually are very nice and |
---|
47 | convenient, they have one disadvantage: If I would just |
---|
48 | call p_s_u() now to send the new prio setting, it would |
---|
49 | send the old setting because the set->value gets changed |
---|
50 | after the (this) eval returns a non-NULL value. |
---|
51 | |
---|
52 | So now I can choose between implementing post-set |
---|
53 | functions next to evals, or just do this little hack: */ |
---|
54 | |
---|
55 | g_free(set->value); |
---|
56 | set->value = g_strdup(value); |
---|
57 | |
---|
58 | /* (Yes, sorry, I prefer the hack. :-P) */ |
---|
59 | |
---|
60 | presence_send_update(acc->ic); |
---|
61 | } |
---|
62 | |
---|
63 | return value; |
---|
64 | } |
---|
65 | |
---|
66 | char *set_eval_tls(set_t *set, char *value) |
---|
67 | { |
---|
68 | if (g_strcasecmp(value, "try") == 0) { |
---|
69 | return value; |
---|
70 | } else { |
---|
71 | return set_eval_bool(set, value); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | struct xt_node *jabber_make_packet(char *name, char *type, char *to, struct xt_node *children) |
---|
76 | { |
---|
77 | struct xt_node *node; |
---|
78 | |
---|
79 | node = xt_new_node(name, NULL, children); |
---|
80 | |
---|
81 | if (type) { |
---|
82 | xt_add_attr(node, "type", type); |
---|
83 | } |
---|
84 | if (to) { |
---|
85 | xt_add_attr(node, "to", to); |
---|
86 | } |
---|
87 | |
---|
88 | /* IQ packets should always have an ID, so let's generate one. It |
---|
89 | might get overwritten by jabber_cache_add() if this packet has |
---|
90 | to be saved until we receive a response. Cached packets get |
---|
91 | slightly different IDs so we can recognize them. */ |
---|
92 | if (strcmp(name, "iq") == 0) { |
---|
93 | char *id = g_strdup_printf("%s%05x", JABBER_PACKET_ID, (next_id++) & 0xfffff); |
---|
94 | xt_add_attr(node, "id", id); |
---|
95 | g_free(id); |
---|
96 | } |
---|
97 | |
---|
98 | return node; |
---|
99 | } |
---|
100 | |
---|
101 | struct xt_node *jabber_make_error_packet(struct xt_node *orig, char *err_cond, char *err_type, char *err_code) |
---|
102 | { |
---|
103 | struct xt_node *node, *c; |
---|
104 | char *to; |
---|
105 | |
---|
106 | /* Create the "defined-condition" tag. */ |
---|
107 | c = xt_new_node(err_cond, NULL, NULL); |
---|
108 | xt_add_attr(c, "xmlns", XMLNS_STANZA_ERROR); |
---|
109 | |
---|
110 | /* Put it in an <error> tag. */ |
---|
111 | c = xt_new_node("error", NULL, c); |
---|
112 | xt_add_attr(c, "type", err_type); |
---|
113 | |
---|
114 | /* Add the error code, if present */ |
---|
115 | if (err_code) { |
---|
116 | xt_add_attr(c, "code", err_code); |
---|
117 | } |
---|
118 | |
---|
119 | /* To make the actual error packet, we copy the original packet and |
---|
120 | add our <error>/type="error" tag. Including the original packet |
---|
121 | is recommended, so let's just do it. */ |
---|
122 | node = xt_dup(orig); |
---|
123 | xt_add_child(node, c); |
---|
124 | xt_add_attr(node, "type", "error"); |
---|
125 | |
---|
126 | /* Return to sender. */ |
---|
127 | if ((to = xt_find_attr(node, "from"))) { |
---|
128 | xt_add_attr(node, "to", to); |
---|
129 | xt_remove_attr(node, "from"); |
---|
130 | } |
---|
131 | |
---|
132 | return node; |
---|
133 | } |
---|
134 | |
---|
135 | /* Cache a node/packet for later use. Mainly useful for IQ packets if you need |
---|
136 | them when you receive the response. Use this BEFORE sending the packet so |
---|
137 | it'll get a new id= tag, and do NOT free() the packet after sending it! */ |
---|
138 | void jabber_cache_add(struct im_connection *ic, struct xt_node *node, jabber_cache_event func) |
---|
139 | { |
---|
140 | struct jabber_data *jd = ic->proto_data; |
---|
141 | struct jabber_cache_entry *entry = g_new0(struct jabber_cache_entry, 1); |
---|
142 | GChecksum *id_hash; |
---|
143 | gsize digest_len = MD5_HASH_SIZE; |
---|
144 | guint8 id_sum[MD5_HASH_SIZE]; |
---|
145 | char *id, *asc_hash; |
---|
146 | |
---|
147 | next_id++; |
---|
148 | |
---|
149 | id_hash = jd->cached_id_prefix; |
---|
150 | g_checksum_update(id_hash, (guint8 *) &next_id, sizeof(next_id)); |
---|
151 | g_checksum_get_digest(id_hash, id_sum, &digest_len); |
---|
152 | g_checksum_free(id_hash); |
---|
153 | |
---|
154 | asc_hash = base64_encode(id_sum, 12); |
---|
155 | |
---|
156 | id = g_strdup_printf("%s%s", JABBER_CACHED_ID, asc_hash); |
---|
157 | xt_add_attr(node, "id", id); |
---|
158 | g_free(id); |
---|
159 | g_free(asc_hash); |
---|
160 | |
---|
161 | entry->node = node; |
---|
162 | entry->func = func; |
---|
163 | entry->saved_at = time(NULL); |
---|
164 | g_hash_table_insert(jd->node_cache, xt_find_attr(node, "id"), entry); |
---|
165 | } |
---|
166 | |
---|
167 | void jabber_cache_entry_free(gpointer data) |
---|
168 | { |
---|
169 | struct jabber_cache_entry *entry = data; |
---|
170 | |
---|
171 | xt_free_node(entry->node); |
---|
172 | g_free(entry); |
---|
173 | } |
---|
174 | |
---|
175 | gboolean jabber_cache_clean_entry(gpointer key, gpointer entry, gpointer nullpointer); |
---|
176 | |
---|
177 | /* This one should be called from time to time (from keepalive, in this case) |
---|
178 | to make sure things don't stay in the node cache forever. By marking nodes |
---|
179 | during the first run and deleting marked nodes during a next run, every |
---|
180 | node should be available in the cache for at least a minute (assuming the |
---|
181 | function is indeed called every minute). */ |
---|
182 | void jabber_cache_clean(struct im_connection *ic) |
---|
183 | { |
---|
184 | struct jabber_data *jd = ic->proto_data; |
---|
185 | time_t threshold = time(NULL) - JABBER_CACHE_MAX_AGE; |
---|
186 | |
---|
187 | g_hash_table_foreach_remove(jd->node_cache, jabber_cache_clean_entry, &threshold); |
---|
188 | } |
---|
189 | |
---|
190 | gboolean jabber_cache_clean_entry(gpointer key, gpointer entry_, gpointer threshold_) |
---|
191 | { |
---|
192 | struct jabber_cache_entry *entry = entry_; |
---|
193 | time_t *threshold = threshold_; |
---|
194 | |
---|
195 | return entry->saved_at < *threshold; |
---|
196 | } |
---|
197 | |
---|
198 | xt_status jabber_cache_handle_packet(struct im_connection *ic, struct xt_node *node) |
---|
199 | { |
---|
200 | struct jabber_data *jd = ic->proto_data; |
---|
201 | struct jabber_cache_entry *entry; |
---|
202 | char *s; |
---|
203 | |
---|
204 | if ((s = xt_find_attr(node, "id")) == NULL || |
---|
205 | strncmp(s, JABBER_CACHED_ID, strlen(JABBER_CACHED_ID)) != 0) { |
---|
206 | /* Silently ignore it, without an ID (or a non-cache |
---|
207 | ID) we don't know how to handle the packet and we |
---|
208 | probably don't have to. */ |
---|
209 | return XT_HANDLED; |
---|
210 | } |
---|
211 | |
---|
212 | entry = g_hash_table_lookup(jd->node_cache, s); |
---|
213 | |
---|
214 | if (entry == NULL) { |
---|
215 | /* |
---|
216 | There's no longer an easy way to see if we generated this |
---|
217 | one or someone else, and there's a ten-minute timeout anyway, |
---|
218 | so meh. |
---|
219 | |
---|
220 | imcb_log( ic, "Warning: Received %s-%s packet with unknown/expired ID %s!", |
---|
221 | node->name, xt_find_attr( node, "type" ) ? : "(no type)", s ); |
---|
222 | */ |
---|
223 | } else if (entry->func) { |
---|
224 | return entry->func(ic, node, entry->node); |
---|
225 | } |
---|
226 | |
---|
227 | return XT_HANDLED; |
---|
228 | } |
---|
229 | |
---|
230 | const struct jabber_away_state jabber_away_state_list[] = |
---|
231 | { |
---|
232 | { "away", "Away" }, |
---|
233 | { "chat", "Free for Chat" }, /* WTF actually uses this? */ |
---|
234 | { "dnd", "Do not Disturb" }, |
---|
235 | { "xa", "Extended Away" }, |
---|
236 | { "", NULL } |
---|
237 | }; |
---|
238 | |
---|
239 | const struct jabber_away_state *jabber_away_state_by_code(char *code) |
---|
240 | { |
---|
241 | int i; |
---|
242 | |
---|
243 | if (code == NULL) { |
---|
244 | return NULL; |
---|
245 | } |
---|
246 | |
---|
247 | for (i = 0; jabber_away_state_list[i].full_name; i++) { |
---|
248 | if (g_strcasecmp(jabber_away_state_list[i].code, code) == 0) { |
---|
249 | return jabber_away_state_list + i; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | return NULL; |
---|
254 | } |
---|
255 | |
---|
256 | const struct jabber_away_state *jabber_away_state_by_name(char *name) |
---|
257 | { |
---|
258 | int i; |
---|
259 | |
---|
260 | if (name == NULL) { |
---|
261 | return NULL; |
---|
262 | } |
---|
263 | |
---|
264 | for (i = 0; jabber_away_state_list[i].full_name; i++) { |
---|
265 | if (g_strcasecmp(jabber_away_state_list[i].full_name, name) == 0) { |
---|
266 | return jabber_away_state_list + i; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | return NULL; |
---|
271 | } |
---|
272 | |
---|
273 | struct jabber_buddy_ask_data { |
---|
274 | struct im_connection *ic; |
---|
275 | char *handle; |
---|
276 | char *realname; |
---|
277 | }; |
---|
278 | |
---|
279 | static void jabber_buddy_ask_yes(void *data) |
---|
280 | { |
---|
281 | struct jabber_buddy_ask_data *bla = data; |
---|
282 | |
---|
283 | presence_send_request(bla->ic, bla->handle, "subscribed"); |
---|
284 | |
---|
285 | imcb_ask_add(bla->ic, bla->handle, NULL); |
---|
286 | |
---|
287 | g_free(bla->handle); |
---|
288 | g_free(bla); |
---|
289 | } |
---|
290 | |
---|
291 | static void jabber_buddy_ask_no(void *data) |
---|
292 | { |
---|
293 | struct jabber_buddy_ask_data *bla = data; |
---|
294 | |
---|
295 | presence_send_request(bla->ic, bla->handle, "unsubscribed"); |
---|
296 | |
---|
297 | g_free(bla->handle); |
---|
298 | g_free(bla); |
---|
299 | } |
---|
300 | |
---|
301 | void jabber_buddy_ask(struct im_connection *ic, char *handle) |
---|
302 | { |
---|
303 | struct jabber_buddy_ask_data *bla = g_new0(struct jabber_buddy_ask_data, 1); |
---|
304 | char *buf; |
---|
305 | |
---|
306 | bla->ic = ic; |
---|
307 | bla->handle = g_strdup(handle); |
---|
308 | |
---|
309 | buf = g_strdup_printf("The user %s wants to add you to his/her buddy list.", handle); |
---|
310 | imcb_ask(ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no); |
---|
311 | g_free(buf); |
---|
312 | } |
---|
313 | |
---|
314 | /* Compares just the bare portions of two Jabber IDs. */ |
---|
315 | int jabber_compare_jid(const char *jid1, const char *jid2) |
---|
316 | { |
---|
317 | int i; |
---|
318 | |
---|
319 | if (!jid1 || !jid2) { |
---|
320 | return FALSE; |
---|
321 | } |
---|
322 | |
---|
323 | for (i = 0;; i++) { |
---|
324 | if (jid1[i] == '\0' || jid1[i] == '/' || jid2[i] == '\0' || jid2[i] == '/') { |
---|
325 | if ((jid1[i] == '\0' || jid1[i] == '/') && (jid2[i] == '\0' || jid2[i] == '/')) { |
---|
326 | break; |
---|
327 | } |
---|
328 | return FALSE; |
---|
329 | } |
---|
330 | if (g_ascii_tolower(jid1[i]) != g_ascii_tolower(jid2[i])) { |
---|
331 | return FALSE; |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | return TRUE; |
---|
336 | } |
---|
337 | |
---|
338 | /* The /resource part is case sensitive. This stops once we see a slash. |
---|
339 | Returns a new string. Don't leak it! */ |
---|
340 | char *jabber_normalize(const char *orig) |
---|
341 | { |
---|
342 | char *lower, *new, *s; |
---|
343 | |
---|
344 | if (!(s = strchr(orig, '/'))) { |
---|
345 | return g_utf8_strdown(orig, -1); |
---|
346 | } |
---|
347 | |
---|
348 | lower = g_utf8_strdown(orig, (s - orig)); /* stop in s */ |
---|
349 | new = g_strconcat(lower, s, NULL); |
---|
350 | g_free(lower); |
---|
351 | return new; |
---|
352 | } |
---|
353 | |
---|
354 | /* Similar to jabber_normalize, but works with addresses in the form |
---|
355 | * resource=chatroom@example.com */ |
---|
356 | char *jabber_normalize_ext(const char *orig) |
---|
357 | { |
---|
358 | char *lower, *new, *s; |
---|
359 | |
---|
360 | if (!(s = strchr(orig, '='))) { |
---|
361 | return g_utf8_strdown(orig, -1); |
---|
362 | } |
---|
363 | |
---|
364 | lower = g_utf8_strdown(s, -1); /* start in s */ |
---|
365 | |
---|
366 | *s = 0; |
---|
367 | new = g_strconcat(orig, lower, NULL); |
---|
368 | *s = '='; |
---|
369 | |
---|
370 | g_free(lower); |
---|
371 | return new; |
---|
372 | } |
---|
373 | |
---|
374 | /* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a |
---|
375 | FULL jid or if we already have this buddy/resource. XXX: No, great, actually |
---|
376 | buddies from transports don't (usually) have resources. So we'll really have |
---|
377 | to deal with that properly. Set their ->resource property to NULL. Do *NOT* |
---|
378 | allow to mix this stuff, though... */ |
---|
379 | struct jabber_buddy *jabber_buddy_add(struct im_connection *ic, char *full_jid_) |
---|
380 | { |
---|
381 | struct jabber_data *jd = ic->proto_data; |
---|
382 | struct jabber_buddy *bud, *new, *bi; |
---|
383 | char *s, *full_jid; |
---|
384 | |
---|
385 | full_jid = jabber_normalize(full_jid_); |
---|
386 | |
---|
387 | if ((s = strchr(full_jid, '/'))) { |
---|
388 | *s = 0; |
---|
389 | } |
---|
390 | |
---|
391 | new = g_new0(struct jabber_buddy, 1); |
---|
392 | |
---|
393 | if ((bud = g_hash_table_lookup(jd->buddies, full_jid))) { |
---|
394 | /* The first entry is always a bare JID. If there are more, we |
---|
395 | should ignore the first one here. */ |
---|
396 | if (bud->next) { |
---|
397 | bud = bud->next; |
---|
398 | } |
---|
399 | |
---|
400 | /* If this is a transport buddy or whatever, it can't have more |
---|
401 | than one instance, so this is always wrong: */ |
---|
402 | if (s == NULL || bud->resource == NULL) { |
---|
403 | if (s) { |
---|
404 | *s = '/'; |
---|
405 | } |
---|
406 | g_free(new); |
---|
407 | g_free(full_jid); |
---|
408 | return NULL; |
---|
409 | } |
---|
410 | |
---|
411 | new->bare_jid = bud->bare_jid; |
---|
412 | |
---|
413 | /* We already have another resource for this buddy, add the |
---|
414 | new one to the list. */ |
---|
415 | for (bi = bud; bi; bi = bi->next) { |
---|
416 | /* Check for dupes. */ |
---|
417 | if (strcmp(bi->resource, s + 1) == 0) { |
---|
418 | *s = '/'; |
---|
419 | g_free(new); |
---|
420 | g_free(full_jid); |
---|
421 | return NULL; |
---|
422 | } |
---|
423 | /* Append the new item to the list. */ |
---|
424 | else if (bi->next == NULL) { |
---|
425 | bi->next = new; |
---|
426 | break; |
---|
427 | } |
---|
428 | } |
---|
429 | } else { |
---|
430 | new->full_jid = new->bare_jid = g_strdup(full_jid); |
---|
431 | g_hash_table_insert(jd->buddies, new->bare_jid, new); |
---|
432 | |
---|
433 | if (s) { |
---|
434 | new->next = g_new0(struct jabber_buddy, 1); |
---|
435 | new->next->bare_jid = new->bare_jid; |
---|
436 | new = new->next; |
---|
437 | } |
---|
438 | } |
---|
439 | |
---|
440 | if (s) { |
---|
441 | *s = '/'; |
---|
442 | new->full_jid = full_jid; |
---|
443 | new->resource = strchr(new->full_jid, '/') + 1; |
---|
444 | } else { |
---|
445 | /* Let's waste some more bytes of RAM instead of to make |
---|
446 | memory management a total disaster here. And it saves |
---|
447 | me one g_free() call in this function. :-P */ |
---|
448 | new->full_jid = full_jid; |
---|
449 | } |
---|
450 | |
---|
451 | return new; |
---|
452 | } |
---|
453 | |
---|
454 | /* Finds a buddy from our structures. Can find both full- and bare JIDs. When |
---|
455 | asked for a bare JID, it uses the "resource_select" setting to see which |
---|
456 | resource to pick. */ |
---|
457 | struct jabber_buddy *jabber_buddy_by_jid(struct im_connection *ic, char *jid_, get_buddy_flags_t flags) |
---|
458 | { |
---|
459 | struct jabber_data *jd = ic->proto_data; |
---|
460 | struct jabber_buddy *bud, *head; |
---|
461 | char *s, *jid; |
---|
462 | |
---|
463 | jid = jabber_normalize(jid_); |
---|
464 | |
---|
465 | if ((s = strchr(jid, '/'))) { |
---|
466 | int bare_exists = 0; |
---|
467 | |
---|
468 | *s = 0; |
---|
469 | if ((bud = g_hash_table_lookup(jd->buddies, jid))) { |
---|
470 | bare_exists = 1; |
---|
471 | |
---|
472 | if (bud->next) { |
---|
473 | bud = bud->next; |
---|
474 | } |
---|
475 | |
---|
476 | /* Just return the first one for this bare JID. */ |
---|
477 | if (flags & GET_BUDDY_FIRST) { |
---|
478 | *s = '/'; |
---|
479 | g_free(jid); |
---|
480 | return bud; |
---|
481 | } |
---|
482 | |
---|
483 | /* Is this one of those no-resource buddies? */ |
---|
484 | if (bud->resource == NULL) { |
---|
485 | *s = '/'; |
---|
486 | g_free(jid); |
---|
487 | return NULL; |
---|
488 | } |
---|
489 | |
---|
490 | /* See if there's an exact match. */ |
---|
491 | for (; bud; bud = bud->next) { |
---|
492 | if (strcmp(bud->resource, s + 1) == 0) { |
---|
493 | break; |
---|
494 | } |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | if (bud == NULL && (flags & GET_BUDDY_CREAT) && |
---|
499 | (bare_exists || bee_user_by_handle(ic->bee, ic, jid))) { |
---|
500 | *s = '/'; |
---|
501 | bud = jabber_buddy_add(ic, jid); |
---|
502 | } |
---|
503 | |
---|
504 | g_free(jid); |
---|
505 | return bud; |
---|
506 | } else { |
---|
507 | struct jabber_buddy *best_prio, *best_time; |
---|
508 | char *set; |
---|
509 | |
---|
510 | head = g_hash_table_lookup(jd->buddies, jid); |
---|
511 | bud = (head && head->next) ? head->next : head; |
---|
512 | |
---|
513 | g_free(jid); |
---|
514 | |
---|
515 | if (bud == NULL) { |
---|
516 | /* No match. Create it now? */ |
---|
517 | return ((flags & GET_BUDDY_CREAT) && |
---|
518 | bee_user_by_handle(ic->bee, ic, jid_)) ? |
---|
519 | jabber_buddy_add(ic, jid_) : NULL; |
---|
520 | } else if (bud->resource && (flags & GET_BUDDY_EXACT)) { |
---|
521 | /* We want an exact match, so in thise case there shouldn't be a /resource. */ |
---|
522 | if (head != bud && head->resource == NULL) { |
---|
523 | return head; |
---|
524 | } else { |
---|
525 | return NULL; |
---|
526 | } |
---|
527 | } else if (bud->resource == NULL || bud->next == NULL) { |
---|
528 | /* No need for selection if there's only one option. */ |
---|
529 | return bud; |
---|
530 | } else if (flags & GET_BUDDY_FIRST) { |
---|
531 | /* Looks like the caller doesn't care about details. */ |
---|
532 | return bud; |
---|
533 | } else if (flags & GET_BUDDY_BARE) { |
---|
534 | return head; |
---|
535 | } |
---|
536 | |
---|
537 | best_prio = best_time = bud; |
---|
538 | for (; bud; bud = bud->next) { |
---|
539 | if (bud->priority > best_prio->priority) { |
---|
540 | best_prio = bud; |
---|
541 | } |
---|
542 | if (bud->last_msg > best_time->last_msg) { |
---|
543 | best_time = bud; |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | if ((set = set_getstr(&ic->acc->set, "resource_select")) == NULL) { |
---|
548 | return NULL; |
---|
549 | } else if (strcmp(set, "priority") == 0) { |
---|
550 | return best_prio; |
---|
551 | } else if (flags & GET_BUDDY_BARE_OK) { /* && strcmp( set, "activity" ) == 0 */ |
---|
552 | if (best_time->last_msg + set_getint(&ic->acc->set, "activity_timeout") >= time(NULL)) { |
---|
553 | return best_time; |
---|
554 | } else { |
---|
555 | return head; |
---|
556 | } |
---|
557 | } else { |
---|
558 | return best_time; |
---|
559 | } |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | /* I'm keeping a separate ext_jid attribute to save a JID that makes sense |
---|
564 | to export to BitlBee. This is mainly for groupchats right now. It's |
---|
565 | a bit of a hack, but I just think having the user nickname in the hostname |
---|
566 | part of the hostmask doesn't look nice on IRC. Normally you can convert |
---|
567 | a normal JID to ext_jid by swapping the part before and after the / and |
---|
568 | replacing the / with a =. But there should be some stripping (@s are |
---|
569 | allowed in Jabber nicks...). */ |
---|
570 | struct jabber_buddy *jabber_buddy_by_ext_jid(struct im_connection *ic, char *jid_, get_buddy_flags_t flags) |
---|
571 | { |
---|
572 | struct jabber_buddy *bud; |
---|
573 | char *s, *jid; |
---|
574 | |
---|
575 | jid = jabber_normalize_ext(jid_); |
---|
576 | |
---|
577 | if ((s = strchr(jid, '=')) == NULL) { |
---|
578 | g_free(jid); |
---|
579 | return NULL; |
---|
580 | } |
---|
581 | |
---|
582 | for (bud = jabber_buddy_by_jid(ic, s + 1, GET_BUDDY_FIRST); bud; bud = bud->next) { |
---|
583 | /* Hmmm, could happen if not all people in the chat are anonymized? */ |
---|
584 | if (bud->ext_jid == NULL) { |
---|
585 | continue; |
---|
586 | } |
---|
587 | |
---|
588 | if (strcmp(bud->ext_jid, jid) == 0) { |
---|
589 | break; |
---|
590 | } |
---|
591 | } |
---|
592 | |
---|
593 | g_free(jid); |
---|
594 | |
---|
595 | return bud; |
---|
596 | } |
---|
597 | |
---|
598 | /* Remove one specific full JID from our list. Use this when a buddy goes |
---|
599 | off-line (because (s)he can still be online from a different location. |
---|
600 | XXX: See above, we should accept bare JIDs too... */ |
---|
601 | int jabber_buddy_remove(struct im_connection *ic, char *full_jid_) |
---|
602 | { |
---|
603 | struct jabber_data *jd = ic->proto_data; |
---|
604 | struct jabber_buddy *bud, *prev = NULL, *bi; |
---|
605 | char *s, *full_jid; |
---|
606 | |
---|
607 | full_jid = jabber_normalize(full_jid_); |
---|
608 | |
---|
609 | if ((s = strchr(full_jid, '/'))) { |
---|
610 | *s = 0; |
---|
611 | } |
---|
612 | |
---|
613 | if ((bud = g_hash_table_lookup(jd->buddies, full_jid))) { |
---|
614 | if (bud->next) { |
---|
615 | bud = (prev = bud)->next; |
---|
616 | } |
---|
617 | |
---|
618 | /* If there's only one item in the list (and if the resource |
---|
619 | matches), removing it is simple. (And the hash reference |
---|
620 | should be removed too!) */ |
---|
621 | if (bud->next == NULL && |
---|
622 | ((s == NULL && bud->resource == NULL) || |
---|
623 | (bud->resource && s && strcmp(bud->resource, s + 1) == 0))) { |
---|
624 | int st = jabber_buddy_remove_bare(ic, full_jid); |
---|
625 | g_free(full_jid); |
---|
626 | return st; |
---|
627 | } else if (s == NULL || bud->resource == NULL) { |
---|
628 | /* Tried to remove a bare JID while this JID does seem |
---|
629 | to have resources... (Or the opposite.) *sigh* */ |
---|
630 | g_free(full_jid); |
---|
631 | return 0; |
---|
632 | } else { |
---|
633 | for (bi = bud; bi; bi = (prev = bi)->next) { |
---|
634 | if (strcmp(bi->resource, s + 1) == 0) { |
---|
635 | break; |
---|
636 | } |
---|
637 | } |
---|
638 | |
---|
639 | g_free(full_jid); |
---|
640 | |
---|
641 | if (bi) { |
---|
642 | if (prev) { |
---|
643 | prev->next = bi->next; |
---|
644 | } else { |
---|
645 | /* Don't think this should ever happen anymore. */ |
---|
646 | g_hash_table_replace(jd->buddies, bi->bare_jid, bi->next); |
---|
647 | } |
---|
648 | |
---|
649 | g_free(bi->ext_jid); |
---|
650 | g_free(bi->full_jid); |
---|
651 | g_free(bi->away_message); |
---|
652 | g_free(bi); |
---|
653 | |
---|
654 | return 1; |
---|
655 | } else { |
---|
656 | return 0; |
---|
657 | } |
---|
658 | } |
---|
659 | } else { |
---|
660 | g_free(full_jid); |
---|
661 | return 0; |
---|
662 | } |
---|
663 | } |
---|
664 | |
---|
665 | /* Remove a buddy completely; removes all resources that belong to the |
---|
666 | specified bare JID. Use this when removing someone from the contact |
---|
667 | list, for example. */ |
---|
668 | int jabber_buddy_remove_bare(struct im_connection *ic, char *bare_jid) |
---|
669 | { |
---|
670 | struct jabber_data *jd = ic->proto_data; |
---|
671 | struct jabber_buddy *bud, *next; |
---|
672 | |
---|
673 | if (strchr(bare_jid, '/')) { |
---|
674 | return 0; |
---|
675 | } |
---|
676 | |
---|
677 | if ((bud = jabber_buddy_by_jid(ic, bare_jid, GET_BUDDY_FIRST))) { |
---|
678 | /* Most important: Remove the hash reference. We don't know |
---|
679 | this buddy anymore. */ |
---|
680 | g_hash_table_remove(jd->buddies, bud->bare_jid); |
---|
681 | g_free(bud->bare_jid); |
---|
682 | |
---|
683 | /* Deallocate the linked list of resources. */ |
---|
684 | while (bud) { |
---|
685 | /* ext_jid && anonymous means that this buddy is |
---|
686 | specific to one groupchat (the one we're |
---|
687 | currently cleaning up) so it can be deleted |
---|
688 | completely. */ |
---|
689 | if (bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS) { |
---|
690 | imcb_remove_buddy(ic, bud->ext_jid, NULL); |
---|
691 | } |
---|
692 | |
---|
693 | next = bud->next; |
---|
694 | g_free(bud->ext_jid); |
---|
695 | g_free(bud->full_jid); |
---|
696 | g_free(bud->away_message); |
---|
697 | g_free(bud); |
---|
698 | bud = next; |
---|
699 | } |
---|
700 | |
---|
701 | return 1; |
---|
702 | } else { |
---|
703 | return 0; |
---|
704 | } |
---|
705 | } |
---|
706 | |
---|
707 | static gboolean jabber_buddy_remove_all_cb(gpointer key, gpointer value, gpointer data) |
---|
708 | { |
---|
709 | struct jabber_buddy *bud, *next; |
---|
710 | |
---|
711 | bud = value; |
---|
712 | if (bud->bare_jid != bud->full_jid) { |
---|
713 | g_free(bud->bare_jid); |
---|
714 | } |
---|
715 | while (bud) { |
---|
716 | next = bud->next; |
---|
717 | g_free(bud->ext_jid); |
---|
718 | g_free(bud->full_jid); |
---|
719 | g_free(bud->away_message); |
---|
720 | g_free(bud); |
---|
721 | bud = next; |
---|
722 | } |
---|
723 | |
---|
724 | return TRUE; |
---|
725 | } |
---|
726 | |
---|
727 | void jabber_buddy_remove_all(struct im_connection *ic) |
---|
728 | { |
---|
729 | struct jabber_data *jd = ic->proto_data; |
---|
730 | |
---|
731 | g_hash_table_foreach_remove(jd->buddies, jabber_buddy_remove_all_cb, NULL); |
---|
732 | g_hash_table_destroy(jd->buddies); |
---|
733 | } |
---|
734 | |
---|
735 | time_t jabber_get_timestamp(struct xt_node *xt) |
---|
736 | { |
---|
737 | struct xt_node *c; |
---|
738 | char *s = NULL; |
---|
739 | struct tm tp; |
---|
740 | gboolean is_old = TRUE; |
---|
741 | const char *format; |
---|
742 | |
---|
743 | /* XEP-0091 has <x> */ |
---|
744 | c = xt_find_node_by_attr(xt->children, "x", "xmlns", XMLNS_DELAY_OLD); |
---|
745 | |
---|
746 | if (!c || !(s = xt_find_attr(c, "stamp"))) { |
---|
747 | is_old = FALSE; |
---|
748 | |
---|
749 | /* XEP-0203 has <delay> */ |
---|
750 | c = xt_find_node_by_attr(xt->children, "delay", "xmlns", XMLNS_DELAY); |
---|
751 | if (!c || !(s = xt_find_attr(c, "stamp"))) { |
---|
752 | return 0; |
---|
753 | } |
---|
754 | } |
---|
755 | |
---|
756 | memset(&tp, 0, sizeof(tp)); |
---|
757 | |
---|
758 | /* The other main difference between XEPs is the timestamp format */ |
---|
759 | format = (is_old) ? "%4d%2d%2dT%2d:%2d:%2d" : "%4d-%2d-%2dT%2d:%2d:%2dZ"; |
---|
760 | |
---|
761 | if (sscanf(s, format, &tp.tm_year, &tp.tm_mon, &tp.tm_mday, |
---|
762 | &tp.tm_hour, &tp.tm_min, &tp.tm_sec) != 6) { |
---|
763 | return 0; |
---|
764 | } |
---|
765 | |
---|
766 | tp.tm_year -= 1900; |
---|
767 | tp.tm_mon--; |
---|
768 | |
---|
769 | return mktime_utc(&tp); |
---|
770 | } |
---|
771 | |
---|
772 | struct jabber_error *jabber_error_parse(struct xt_node *node, char *xmlns) |
---|
773 | { |
---|
774 | struct jabber_error *err; |
---|
775 | struct xt_node *c; |
---|
776 | char *s; |
---|
777 | |
---|
778 | if (node == NULL) { |
---|
779 | return NULL; |
---|
780 | } |
---|
781 | |
---|
782 | err = g_new0(struct jabber_error, 1); |
---|
783 | err->type = xt_find_attr(node, "type"); |
---|
784 | |
---|
785 | for (c = node->children; c; c = c->next) { |
---|
786 | if (!(s = xt_find_attr(c, "xmlns")) || |
---|
787 | strcmp(s, xmlns) != 0) { |
---|
788 | continue; |
---|
789 | } |
---|
790 | |
---|
791 | if (strcmp(c->name, "text") != 0) { |
---|
792 | err->code = c->name; |
---|
793 | } |
---|
794 | /* Only use the text if it doesn't have an xml:lang attribute, |
---|
795 | if it's empty or if it's set to something English. */ |
---|
796 | else if (!(s = xt_find_attr(c, "xml:lang")) || |
---|
797 | !*s || strncmp(s, "en", 2) == 0) { |
---|
798 | err->text = c->text; |
---|
799 | } |
---|
800 | } |
---|
801 | |
---|
802 | return err; |
---|
803 | } |
---|
804 | |
---|
805 | void jabber_error_free(struct jabber_error *err) |
---|
806 | { |
---|
807 | g_free(err); |
---|
808 | } |
---|
809 | |
---|
810 | gboolean jabber_set_me(struct im_connection *ic, const char *me) |
---|
811 | { |
---|
812 | struct jabber_data *jd = ic->proto_data; |
---|
813 | |
---|
814 | if (strchr(me, '@') == NULL) { |
---|
815 | return FALSE; |
---|
816 | } |
---|
817 | |
---|
818 | g_free(jd->username); |
---|
819 | g_free(jd->me); |
---|
820 | |
---|
821 | jd->me = jabber_normalize(me); |
---|
822 | jd->server = strchr(jd->me, '@'); |
---|
823 | jd->username = g_strndup(jd->me, jd->server - jd->me); |
---|
824 | jd->server++; |
---|
825 | |
---|
826 | /* Set the "internal" account username, for groupchats */ |
---|
827 | g_free(jd->internal_jid); |
---|
828 | jd->internal_jid = g_strdup(jd->me); |
---|
829 | |
---|
830 | return TRUE; |
---|
831 | } |
---|
832 | |
---|
833 | /* Returns new reference! g_free() afterwards. */ |
---|
834 | char *jabber_get_bare_jid(char *jid) |
---|
835 | { |
---|
836 | char *s = NULL; |
---|
837 | |
---|
838 | if (jid == NULL) { |
---|
839 | return NULL; |
---|
840 | } |
---|
841 | |
---|
842 | if ((s = strchr(jid, '/'))) { |
---|
843 | return g_strndup(jid, s - jid); |
---|
844 | } else { |
---|
845 | return g_strdup(jid); |
---|
846 | } |
---|
847 | } |
---|