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