1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Misc. stuff * |
---|
5 | * * |
---|
6 | * Copyright 2006 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 | |
---|
26 | static unsigned int next_id = 1; |
---|
27 | |
---|
28 | char *set_eval_priority( set_t *set, char *value ) |
---|
29 | { |
---|
30 | account_t *acc = set->data; |
---|
31 | int i; |
---|
32 | |
---|
33 | if( sscanf( value, "%d", &i ) == 1 ) |
---|
34 | { |
---|
35 | /* Priority is a signed 8-bit integer, according to RFC 3921. */ |
---|
36 | if( i < -128 || i > 127 ) |
---|
37 | return NULL; |
---|
38 | } |
---|
39 | else |
---|
40 | return NULL; |
---|
41 | |
---|
42 | /* Only run this stuff if the account is online ATM, |
---|
43 | and if the setting seems to be acceptable. */ |
---|
44 | if( acc->ic ) |
---|
45 | { |
---|
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 | struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children ) |
---|
75 | { |
---|
76 | struct xt_node *node; |
---|
77 | |
---|
78 | node = xt_new_node( name, NULL, children ); |
---|
79 | |
---|
80 | if( type ) |
---|
81 | xt_add_attr( node, "type", type ); |
---|
82 | if( to ) |
---|
83 | xt_add_attr( node, "to", to ); |
---|
84 | |
---|
85 | /* IQ packets should always have an ID, so let's generate one. It |
---|
86 | might get overwritten by jabber_cache_add() if this packet has |
---|
87 | to be saved until we receive a response. Cached packets get |
---|
88 | slightly different IDs so we can recognize them. */ |
---|
89 | if( strcmp( name, "iq" ) == 0 ) |
---|
90 | { |
---|
91 | char *id = g_strdup_printf( "%s%05x", JABBER_PACKET_ID, ( next_id++ ) & 0xfffff ); |
---|
92 | xt_add_attr( node, "id", id ); |
---|
93 | g_free( id ); |
---|
94 | } |
---|
95 | |
---|
96 | return node; |
---|
97 | } |
---|
98 | |
---|
99 | struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code ) |
---|
100 | { |
---|
101 | struct xt_node *node, *c; |
---|
102 | char *to; |
---|
103 | |
---|
104 | /* Create the "defined-condition" tag. */ |
---|
105 | c = xt_new_node( err_cond, NULL, NULL ); |
---|
106 | xt_add_attr( c, "xmlns", XMLNS_STANZA_ERROR ); |
---|
107 | |
---|
108 | /* Put it in an <error> tag. */ |
---|
109 | c = xt_new_node( "error", NULL, c ); |
---|
110 | xt_add_attr( c, "type", err_type ); |
---|
111 | |
---|
112 | /* Add the error code, if present */ |
---|
113 | if (err_code) |
---|
114 | xt_add_attr( c, "code", err_code ); |
---|
115 | |
---|
116 | /* To make the actual error packet, we copy the original packet and |
---|
117 | add our <error>/type="error" tag. Including the original packet |
---|
118 | is recommended, so let's just do it. */ |
---|
119 | node = xt_dup( orig ); |
---|
120 | xt_add_child( node, c ); |
---|
121 | xt_add_attr( node, "type", "error" ); |
---|
122 | |
---|
123 | /* Return to sender. */ |
---|
124 | if( ( to = xt_find_attr( node, "from" ) ) ) |
---|
125 | { |
---|
126 | xt_add_attr( node, "to", to ); |
---|
127 | xt_remove_attr( node, "from" ); |
---|
128 | } |
---|
129 | |
---|
130 | return node; |
---|
131 | } |
---|
132 | |
---|
133 | /* Cache a node/packet for later use. Mainly useful for IQ packets if you need |
---|
134 | them when you receive the response. Use this BEFORE sending the packet so |
---|
135 | it'll get a new id= tag, and do NOT free() the packet after sending it! */ |
---|
136 | void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func ) |
---|
137 | { |
---|
138 | struct jabber_data *jd = ic->proto_data; |
---|
139 | struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 ); |
---|
140 | char *id; |
---|
141 | |
---|
142 | id = g_strdup_printf( "%s%05x", jd->cached_id_prefix, ( next_id++ ) & 0xfffff ); |
---|
143 | xt_add_attr( node, "id", id ); |
---|
144 | g_free( id ); |
---|
145 | |
---|
146 | entry->node = node; |
---|
147 | entry->func = func; |
---|
148 | entry->saved_at = time( NULL ); |
---|
149 | g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry ); |
---|
150 | } |
---|
151 | |
---|
152 | void jabber_cache_entry_free( gpointer data ) |
---|
153 | { |
---|
154 | struct jabber_cache_entry *entry = data; |
---|
155 | |
---|
156 | xt_free_node( entry->node ); |
---|
157 | g_free( entry ); |
---|
158 | } |
---|
159 | |
---|
160 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer ); |
---|
161 | |
---|
162 | /* This one should be called from time to time (from keepalive, in this case) |
---|
163 | to make sure things don't stay in the node cache forever. By marking nodes |
---|
164 | during the first run and deleting marked nodes during a next run, every |
---|
165 | node should be available in the cache for at least a minute (assuming the |
---|
166 | function is indeed called every minute). */ |
---|
167 | void jabber_cache_clean( struct im_connection *ic ) |
---|
168 | { |
---|
169 | struct jabber_data *jd = ic->proto_data; |
---|
170 | time_t threshold = time( NULL ) - JABBER_CACHE_MAX_AGE; |
---|
171 | |
---|
172 | g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, &threshold ); |
---|
173 | } |
---|
174 | |
---|
175 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer threshold_ ) |
---|
176 | { |
---|
177 | struct jabber_cache_entry *entry = entry_; |
---|
178 | time_t *threshold = threshold_; |
---|
179 | |
---|
180 | return entry->saved_at < *threshold; |
---|
181 | } |
---|
182 | |
---|
183 | xt_status jabber_cache_handle_packet( struct im_connection *ic, struct xt_node *node ) |
---|
184 | { |
---|
185 | struct jabber_data *jd = ic->proto_data; |
---|
186 | struct jabber_cache_entry *entry; |
---|
187 | char *s; |
---|
188 | |
---|
189 | if( ( s = xt_find_attr( node, "id" ) ) == NULL || |
---|
190 | strncmp( s, jd->cached_id_prefix, strlen( jd->cached_id_prefix ) ) != 0 ) |
---|
191 | { |
---|
192 | /* Silently ignore it, without an ID (or a non-cache |
---|
193 | ID) we don't know how to handle the packet and we |
---|
194 | probably don't have to. */ |
---|
195 | return XT_HANDLED; |
---|
196 | } |
---|
197 | |
---|
198 | entry = g_hash_table_lookup( jd->node_cache, s ); |
---|
199 | |
---|
200 | if( entry == NULL ) |
---|
201 | { |
---|
202 | imcb_log( ic, "Warning: Received %s-%s packet with unknown/expired ID %s!", |
---|
203 | node->name, xt_find_attr( node, "type" ) ? : "(no type)", s ); |
---|
204 | } |
---|
205 | else if( entry->func ) |
---|
206 | { |
---|
207 | return entry->func( ic, node, entry->node ); |
---|
208 | } |
---|
209 | |
---|
210 | return XT_HANDLED; |
---|
211 | } |
---|
212 | |
---|
213 | const struct jabber_away_state jabber_away_state_list[] = |
---|
214 | { |
---|
215 | { "away", "Away" }, |
---|
216 | { "chat", "Free for Chat" }, |
---|
217 | { "dnd", "Do not Disturb" }, |
---|
218 | { "xa", "Extended Away" }, |
---|
219 | { "", "Online" }, |
---|
220 | { "", NULL } |
---|
221 | }; |
---|
222 | |
---|
223 | const struct jabber_away_state *jabber_away_state_by_code( char *code ) |
---|
224 | { |
---|
225 | int i; |
---|
226 | |
---|
227 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
228 | if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 ) |
---|
229 | return jabber_away_state_list + i; |
---|
230 | |
---|
231 | return NULL; |
---|
232 | } |
---|
233 | |
---|
234 | const struct jabber_away_state *jabber_away_state_by_name( char *name ) |
---|
235 | { |
---|
236 | int i; |
---|
237 | |
---|
238 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
239 | if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 ) |
---|
240 | return jabber_away_state_list + i; |
---|
241 | |
---|
242 | return NULL; |
---|
243 | } |
---|
244 | |
---|
245 | struct jabber_buddy_ask_data |
---|
246 | { |
---|
247 | struct im_connection *ic; |
---|
248 | char *handle; |
---|
249 | char *realname; |
---|
250 | }; |
---|
251 | |
---|
252 | static void jabber_buddy_ask_yes( void *data ) |
---|
253 | { |
---|
254 | struct jabber_buddy_ask_data *bla = data; |
---|
255 | |
---|
256 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
257 | |
---|
258 | if( imcb_find_buddy( bla->ic, bla->handle ) == NULL ) |
---|
259 | imcb_ask_add( bla->ic, bla->handle, NULL ); |
---|
260 | |
---|
261 | g_free( bla->handle ); |
---|
262 | g_free( bla ); |
---|
263 | } |
---|
264 | |
---|
265 | static void jabber_buddy_ask_no( void *data ) |
---|
266 | { |
---|
267 | struct jabber_buddy_ask_data *bla = data; |
---|
268 | |
---|
269 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
270 | |
---|
271 | g_free( bla->handle ); |
---|
272 | g_free( bla ); |
---|
273 | } |
---|
274 | |
---|
275 | void jabber_buddy_ask( struct im_connection *ic, char *handle ) |
---|
276 | { |
---|
277 | struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 ); |
---|
278 | char *buf; |
---|
279 | |
---|
280 | bla->ic = ic; |
---|
281 | bla->handle = g_strdup( handle ); |
---|
282 | |
---|
283 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle ); |
---|
284 | imcb_ask( ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no ); |
---|
285 | g_free( buf ); |
---|
286 | } |
---|
287 | |
---|
288 | /* Returns a new string. Don't leak it! */ |
---|
289 | char *jabber_normalize( const char *orig ) |
---|
290 | { |
---|
291 | int len, i; |
---|
292 | char *new; |
---|
293 | |
---|
294 | len = strlen( orig ); |
---|
295 | new = g_new( char, len + 1 ); |
---|
296 | for( i = 0; i < len; i ++ ) |
---|
297 | { |
---|
298 | /* don't normalize the resource */ |
---|
299 | if( orig[i] == '/' ) |
---|
300 | break; |
---|
301 | new[i] = tolower( orig[i] ); |
---|
302 | } |
---|
303 | for( ; i < len; i ++ ) |
---|
304 | new[i] = orig[i]; |
---|
305 | |
---|
306 | new[i] = 0; |
---|
307 | return new; |
---|
308 | } |
---|
309 | |
---|
310 | /* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a |
---|
311 | FULL jid or if we already have this buddy/resource. XXX: No, great, actually |
---|
312 | buddies from transports don't (usually) have resources. So we'll really have |
---|
313 | to deal with that properly. Set their ->resource property to NULL. Do *NOT* |
---|
314 | allow to mix this stuff, though... */ |
---|
315 | struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid_ ) |
---|
316 | { |
---|
317 | struct jabber_data *jd = ic->proto_data; |
---|
318 | struct jabber_buddy *bud, *new, *bi; |
---|
319 | char *s, *full_jid; |
---|
320 | |
---|
321 | full_jid = jabber_normalize( full_jid_ ); |
---|
322 | |
---|
323 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
324 | *s = 0; |
---|
325 | |
---|
326 | new = g_new0( struct jabber_buddy, 1 ); |
---|
327 | |
---|
328 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
329 | { |
---|
330 | /* If this is a transport buddy or whatever, it can't have more |
---|
331 | than one instance, so this is always wrong: */ |
---|
332 | if( s == NULL || bud->resource == NULL ) |
---|
333 | { |
---|
334 | if( s ) *s = '/'; |
---|
335 | g_free( new ); |
---|
336 | g_free( full_jid ); |
---|
337 | return NULL; |
---|
338 | } |
---|
339 | |
---|
340 | new->bare_jid = bud->bare_jid; |
---|
341 | |
---|
342 | /* We already have another resource for this buddy, add the |
---|
343 | new one to the list. */ |
---|
344 | for( bi = bud; bi; bi = bi->next ) |
---|
345 | { |
---|
346 | /* Check for dupes. */ |
---|
347 | if( g_strcasecmp( bi->resource, s + 1 ) == 0 ) |
---|
348 | { |
---|
349 | *s = '/'; |
---|
350 | g_free( new ); |
---|
351 | g_free( full_jid ); |
---|
352 | return NULL; |
---|
353 | } |
---|
354 | /* Append the new item to the list. */ |
---|
355 | else if( bi->next == NULL ) |
---|
356 | { |
---|
357 | bi->next = new; |
---|
358 | break; |
---|
359 | } |
---|
360 | } |
---|
361 | } |
---|
362 | else |
---|
363 | { |
---|
364 | /* Keep in mind that full_jid currently isn't really |
---|
365 | a full JID... */ |
---|
366 | new->bare_jid = g_strdup( full_jid ); |
---|
367 | g_hash_table_insert( jd->buddies, new->bare_jid, new ); |
---|
368 | } |
---|
369 | |
---|
370 | if( s ) |
---|
371 | { |
---|
372 | *s = '/'; |
---|
373 | new->full_jid = full_jid; |
---|
374 | new->resource = strchr( new->full_jid, '/' ) + 1; |
---|
375 | } |
---|
376 | else |
---|
377 | { |
---|
378 | /* Let's waste some more bytes of RAM instead of to make |
---|
379 | memory management a total disaster here. And it saves |
---|
380 | me one g_free() call in this function. :-P */ |
---|
381 | new->full_jid = full_jid; |
---|
382 | } |
---|
383 | |
---|
384 | return new; |
---|
385 | } |
---|
386 | |
---|
387 | /* Finds a buddy from our structures. Can find both full- and bare JIDs. When |
---|
388 | asked for a bare JID, it uses the "resource_select" setting to see which |
---|
389 | resource to pick. */ |
---|
390 | struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags ) |
---|
391 | { |
---|
392 | struct jabber_data *jd = ic->proto_data; |
---|
393 | struct jabber_buddy *bud; |
---|
394 | char *s, *jid; |
---|
395 | |
---|
396 | jid = jabber_normalize( jid_ ); |
---|
397 | |
---|
398 | if( ( s = strchr( jid, '/' ) ) ) |
---|
399 | { |
---|
400 | int none_found = 0; |
---|
401 | |
---|
402 | *s = 0; |
---|
403 | if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) ) |
---|
404 | { |
---|
405 | /* Just return the first one for this bare JID. */ |
---|
406 | if( flags & GET_BUDDY_FIRST ) |
---|
407 | { |
---|
408 | *s = '/'; |
---|
409 | g_free( jid ); |
---|
410 | return bud; |
---|
411 | } |
---|
412 | |
---|
413 | /* Is this one of those no-resource buddies? */ |
---|
414 | if( bud->resource == NULL ) |
---|
415 | { |
---|
416 | *s = '/'; |
---|
417 | g_free( jid ); |
---|
418 | return NULL; |
---|
419 | } |
---|
420 | |
---|
421 | /* See if there's an exact match. */ |
---|
422 | for( ; bud; bud = bud->next ) |
---|
423 | if( g_strcasecmp( bud->resource, s + 1 ) == 0 ) |
---|
424 | break; |
---|
425 | } |
---|
426 | else |
---|
427 | { |
---|
428 | /* This hack is there to make sure that O_CREAT will |
---|
429 | work if there's already another resouce present |
---|
430 | for this JID, even if it's an unknown buddy. This |
---|
431 | is done to handle conferences properly. */ |
---|
432 | none_found = 1; |
---|
433 | /* TODO(wilmer): Find out what I was thinking when I |
---|
434 | wrote this??? And then fix it. This makes me sad... */ |
---|
435 | } |
---|
436 | |
---|
437 | if( bud == NULL && ( flags & GET_BUDDY_CREAT ) && ( imcb_find_buddy( ic, jid ) || !none_found ) ) |
---|
438 | { |
---|
439 | *s = '/'; |
---|
440 | bud = jabber_buddy_add( ic, jid ); |
---|
441 | } |
---|
442 | |
---|
443 | g_free( jid ); |
---|
444 | return bud; |
---|
445 | } |
---|
446 | else |
---|
447 | { |
---|
448 | struct jabber_buddy *best_prio, *best_time; |
---|
449 | char *set; |
---|
450 | |
---|
451 | bud = g_hash_table_lookup( jd->buddies, jid ); |
---|
452 | |
---|
453 | g_free( jid ); |
---|
454 | |
---|
455 | if( bud == NULL ) |
---|
456 | /* No match. Create it now? */ |
---|
457 | return ( ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid_ ) ) ? |
---|
458 | jabber_buddy_add( ic, jid_ ) : NULL; |
---|
459 | else if( bud->resource && ( flags & GET_BUDDY_EXACT ) ) |
---|
460 | /* We want an exact match, so in thise case there shouldn't be a /resource. */ |
---|
461 | return NULL; |
---|
462 | else if( ( bud->resource == NULL || bud->next == NULL ) ) |
---|
463 | /* No need for selection if there's only one option. */ |
---|
464 | return bud; |
---|
465 | else if( flags & GET_BUDDY_FIRST ) |
---|
466 | /* Looks like the caller doesn't care about details. */ |
---|
467 | return bud; |
---|
468 | |
---|
469 | best_prio = best_time = bud; |
---|
470 | for( ; bud; bud = bud->next ) |
---|
471 | { |
---|
472 | if( bud->priority > best_prio->priority ) |
---|
473 | best_prio = bud; |
---|
474 | if( bud->last_act > best_time->last_act ) |
---|
475 | best_time = bud; |
---|
476 | } |
---|
477 | |
---|
478 | if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL ) |
---|
479 | return NULL; |
---|
480 | else if( strcmp( set, "activity" ) == 0 ) |
---|
481 | return best_time; |
---|
482 | else /* if( strcmp( set, "priority" ) == 0 ) */ |
---|
483 | return best_prio; |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | /* I'm keeping a separate ext_jid attribute to save a JID that makes sense |
---|
488 | to export to BitlBee. This is mainly for groupchats right now. It's |
---|
489 | a bit of a hack, but I just think having the user nickname in the hostname |
---|
490 | part of the hostmask doesn't look nice on IRC. Normally you can convert |
---|
491 | a normal JID to ext_jid by swapping the part before and after the / and |
---|
492 | replacing the / with a =. But there should be some stripping (@s are |
---|
493 | allowed in Jabber nicks...). */ |
---|
494 | struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags ) |
---|
495 | { |
---|
496 | struct jabber_buddy *bud; |
---|
497 | char *s, *jid; |
---|
498 | |
---|
499 | jid = jabber_normalize( jid_ ); |
---|
500 | |
---|
501 | if( ( s = strchr( jid, '=' ) ) == NULL ) |
---|
502 | return NULL; |
---|
503 | |
---|
504 | for( bud = jabber_buddy_by_jid( ic, s + 1, GET_BUDDY_FIRST ); bud; bud = bud->next ) |
---|
505 | { |
---|
506 | /* Hmmm, could happen if not all people in the chat are anonymized? */ |
---|
507 | if( bud->ext_jid == NULL ) |
---|
508 | continue; |
---|
509 | |
---|
510 | if( strcmp( bud->ext_jid, jid ) == 0 ) |
---|
511 | break; |
---|
512 | } |
---|
513 | |
---|
514 | g_free( jid ); |
---|
515 | |
---|
516 | return bud; |
---|
517 | } |
---|
518 | |
---|
519 | /* Remove one specific full JID from our list. Use this when a buddy goes |
---|
520 | off-line (because (s)he can still be online from a different location. |
---|
521 | XXX: See above, we should accept bare JIDs too... */ |
---|
522 | int jabber_buddy_remove( struct im_connection *ic, char *full_jid_ ) |
---|
523 | { |
---|
524 | struct jabber_data *jd = ic->proto_data; |
---|
525 | struct jabber_buddy *bud, *prev, *bi; |
---|
526 | char *s, *full_jid; |
---|
527 | |
---|
528 | full_jid = jabber_normalize( full_jid_ ); |
---|
529 | |
---|
530 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
531 | *s = 0; |
---|
532 | |
---|
533 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
534 | { |
---|
535 | /* If there's only one item in the list (and if the resource |
---|
536 | matches), removing it is simple. (And the hash reference |
---|
537 | should be removed too!) */ |
---|
538 | if( bud->next == NULL && ( ( s == NULL || bud->resource == NULL ) || g_strcasecmp( bud->resource, s + 1 ) == 0 ) ) |
---|
539 | { |
---|
540 | g_hash_table_remove( jd->buddies, bud->bare_jid ); |
---|
541 | g_free( bud->bare_jid ); |
---|
542 | g_free( bud->ext_jid ); |
---|
543 | g_free( bud->full_jid ); |
---|
544 | g_free( bud->away_message ); |
---|
545 | g_free( bud ); |
---|
546 | |
---|
547 | g_free( full_jid ); |
---|
548 | |
---|
549 | return 1; |
---|
550 | } |
---|
551 | else if( s == NULL || bud->resource == NULL ) |
---|
552 | { |
---|
553 | /* Tried to remove a bare JID while this JID does seem |
---|
554 | to have resources... (Or the opposite.) *sigh* */ |
---|
555 | g_free( full_jid ); |
---|
556 | return 0; |
---|
557 | } |
---|
558 | else |
---|
559 | { |
---|
560 | for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next ) |
---|
561 | if( g_strcasecmp( bi->resource, s + 1 ) == 0 ) |
---|
562 | break; |
---|
563 | |
---|
564 | g_free( full_jid ); |
---|
565 | |
---|
566 | if( bi ) |
---|
567 | { |
---|
568 | if( prev ) |
---|
569 | prev->next = bi->next; |
---|
570 | else |
---|
571 | /* The hash table should point at the second |
---|
572 | item, because we're removing the first. */ |
---|
573 | g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next ); |
---|
574 | |
---|
575 | g_free( bi->ext_jid ); |
---|
576 | g_free( bi->full_jid ); |
---|
577 | g_free( bi->away_message ); |
---|
578 | g_free( bi ); |
---|
579 | |
---|
580 | return 1; |
---|
581 | } |
---|
582 | else |
---|
583 | { |
---|
584 | return 0; |
---|
585 | } |
---|
586 | } |
---|
587 | } |
---|
588 | else |
---|
589 | { |
---|
590 | g_free( full_jid ); |
---|
591 | return 0; |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | /* Remove a buddy completely; removes all resources that belong to the |
---|
596 | specified bare JID. Use this when removing someone from the contact |
---|
597 | list, for example. */ |
---|
598 | int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid ) |
---|
599 | { |
---|
600 | struct jabber_data *jd = ic->proto_data; |
---|
601 | struct jabber_buddy *bud, *next; |
---|
602 | |
---|
603 | if( strchr( bare_jid, '/' ) ) |
---|
604 | return 0; |
---|
605 | |
---|
606 | if( ( bud = jabber_buddy_by_jid( ic, bare_jid, GET_BUDDY_FIRST ) ) ) |
---|
607 | { |
---|
608 | /* Most important: Remove the hash reference. We don't know |
---|
609 | this buddy anymore. */ |
---|
610 | g_hash_table_remove( jd->buddies, bud->bare_jid ); |
---|
611 | g_free( bud->bare_jid ); |
---|
612 | |
---|
613 | /* Deallocate the linked list of resources. */ |
---|
614 | while( bud ) |
---|
615 | { |
---|
616 | /* ext_jid && anonymous means that this buddy is |
---|
617 | specific to one groupchat (the one we're |
---|
618 | currently cleaning up) so it can be deleted |
---|
619 | completely. */ |
---|
620 | if( bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS ) |
---|
621 | imcb_remove_buddy( ic, bud->ext_jid, NULL ); |
---|
622 | |
---|
623 | next = bud->next; |
---|
624 | g_free( bud->ext_jid ); |
---|
625 | g_free( bud->full_jid ); |
---|
626 | g_free( bud->away_message ); |
---|
627 | g_free( bud ); |
---|
628 | bud = next; |
---|
629 | } |
---|
630 | |
---|
631 | return 1; |
---|
632 | } |
---|
633 | else |
---|
634 | { |
---|
635 | return 0; |
---|
636 | } |
---|
637 | } |
---|
638 | |
---|
639 | time_t jabber_get_timestamp( struct xt_node *xt ) |
---|
640 | { |
---|
641 | struct tm tp, utc; |
---|
642 | struct xt_node *c; |
---|
643 | time_t res, tres; |
---|
644 | char *s = NULL; |
---|
645 | |
---|
646 | for( c = xt->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) |
---|
647 | { |
---|
648 | if( ( s = xt_find_attr( c, "xmlns" ) ) && strcmp( s, XMLNS_DELAY ) == 0 ) |
---|
649 | break; |
---|
650 | } |
---|
651 | |
---|
652 | if( !c || !( s = xt_find_attr( c, "stamp" ) ) ) |
---|
653 | return 0; |
---|
654 | |
---|
655 | memset( &tp, 0, sizeof( tp ) ); |
---|
656 | if( sscanf( s, "%4d%2d%2dT%2d:%2d:%2d", &tp.tm_year, &tp.tm_mon, &tp.tm_mday, |
---|
657 | &tp.tm_hour, &tp.tm_min, &tp.tm_sec ) != 6 ) |
---|
658 | return 0; |
---|
659 | |
---|
660 | tp.tm_year -= 1900; |
---|
661 | tp.tm_mon --; |
---|
662 | tp.tm_isdst = -1; /* GRRRRRRRRRRR */ |
---|
663 | |
---|
664 | res = mktime( &tp ); |
---|
665 | /* Problem is, mktime() just gave us the GMT timestamp for the |
---|
666 | given local time... While the given time WAS NOT local. So |
---|
667 | we should fix this now. |
---|
668 | |
---|
669 | Now I could choose between messing with environment variables |
---|
670 | (kludgy) or using timegm() (not portable)... Or doing the |
---|
671 | following, which I actually prefer... */ |
---|
672 | gmtime_r( &res, &utc ); |
---|
673 | utc.tm_isdst = -1; /* Once more: GRRRRRRRRRRRRRRRRRR!!! */ |
---|
674 | if( utc.tm_hour == tp.tm_hour && utc.tm_min == tp.tm_min ) |
---|
675 | /* Sweet! We're in UTC right now... */ |
---|
676 | return res; |
---|
677 | |
---|
678 | tres = mktime( &utc ); |
---|
679 | res += res - tres; |
---|
680 | |
---|
681 | /* Yes, this is a hack. And it will go wrong around DST changes. |
---|
682 | BUT this is more likely to be threadsafe than messing with |
---|
683 | environment variables, and possibly more portable... */ |
---|
684 | |
---|
685 | return res; |
---|
686 | } |
---|
687 | |
---|
688 | struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns ) |
---|
689 | { |
---|
690 | struct jabber_error *err; |
---|
691 | struct xt_node *c; |
---|
692 | char *s; |
---|
693 | |
---|
694 | if( node == NULL ) |
---|
695 | return NULL; |
---|
696 | |
---|
697 | err = g_new0( struct jabber_error, 1 ); |
---|
698 | err->type = xt_find_attr( node, "type" ); |
---|
699 | |
---|
700 | for( c = node->children; c; c = c->next ) |
---|
701 | { |
---|
702 | if( !( s = xt_find_attr( c, "xmlns" ) ) || |
---|
703 | strcmp( s, xmlns ) != 0 ) |
---|
704 | continue; |
---|
705 | |
---|
706 | if( strcmp( c->name, "text" ) != 0 ) |
---|
707 | { |
---|
708 | err->code = c->name; |
---|
709 | } |
---|
710 | /* Only use the text if it doesn't have an xml:lang attribute, |
---|
711 | if it's empty or if it's set to something English. */ |
---|
712 | else if( !( s = xt_find_attr( c, "xml:lang" ) ) || |
---|
713 | !*s || strncmp( s, "en", 2 ) == 0 ) |
---|
714 | { |
---|
715 | err->text = c->text; |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | return err; |
---|
720 | } |
---|
721 | |
---|
722 | void jabber_error_free( struct jabber_error *err ) |
---|
723 | { |
---|
724 | g_free( err ); |
---|
725 | } |
---|