source: protocols/jabber/jabber_util.c @ 788a1af

Last change on this file since 788a1af was 788a1af, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-15T20:24:01Z

Proper cleanup of jabber buddy structures when removing a buddy from the
list, proper checking (and handling) of events related to buddies that
aren't "hashed" yet, limit checks on priorityto setting, renamed JEP85
to XEP85, support for more XEP85 states.

  • Property mode set to 100644
File size: 11.7 KB
Line 
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
26static int next_id = 1;
27
28char *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->gc )
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                   when the 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->gc );
61        }
62       
63        return value;
64}
65
66char *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
74struct 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        return node;
86}
87
88struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type )
89{
90        struct xt_node *node, *c;
91        char *to;
92       
93        /* Create the "defined-condition" tag. */
94        c = xt_new_node( err_cond, NULL, NULL );
95        xt_add_attr( c, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas" );
96       
97        /* Put it in an <error> tag. */
98        c = xt_new_node( "error", NULL, c );
99        xt_add_attr( c, "type", err_type );
100       
101        /* To make the actual error packet, we copy the original packet and
102           add our <error>/type="error" tag. Including the original packet
103           is recommended, so let's just do it. */
104        node = xt_dup( orig );
105        xt_add_child( node, c );
106        xt_add_attr( node, "type", "error" );
107       
108        /* Return to sender. */
109        if( ( to = xt_find_attr( node, "from" ) ) )
110        {
111                xt_add_attr( node, "to", to );
112                xt_remove_attr( node, "from" );
113        }
114               
115        return node;
116}
117
118/* Cache a node/epacket for later use. Mainly useful for IQ packets if you need
119   them when you receive the response. Use this BEFORE sending the packet so
120   it'll get an id= tag, and do NOT free() the packet after writing it! */
121void jabber_cache_add( struct gaim_connection *gc, struct xt_node *node, jabber_cache_event func )
122{
123        struct jabber_data *jd = gc->proto_data;
124        char *id = g_strdup_printf( "BeeX%04x", next_id++ );
125        struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 );
126       
127        xt_add_attr( node, "id", id );
128        g_free( id );
129       
130        entry->node = node;
131        entry->func = func;
132        g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry );
133}
134
135void jabber_cache_entry_free( gpointer data )
136{
137        struct jabber_cache_entry *entry = data;
138       
139        xt_free_node( entry->node );
140        g_free( entry );
141}
142
143gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer );
144
145/* This one should be called from time to time (from keepalive, in this case)
146   to make sure things don't stay in the node cache forever. By marking nodes
147   during the first run and deleting marked nodes during a next run, every
148   node should be available in the cache for at least a minute (assuming the
149   function is indeed called every minute). */
150void jabber_cache_clean( struct gaim_connection *gc )
151{
152        struct jabber_data *jd = gc->proto_data;
153       
154        g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, NULL );
155}
156
157gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer nullpointer )
158{
159        struct jabber_cache_entry *entry = entry_;
160        struct xt_node *node = entry->node;
161       
162        if( node->flags & XT_SEEN )
163                return TRUE;
164        else
165        {
166                node->flags |= XT_SEEN;
167                return FALSE;
168        }
169}
170
171const struct jabber_away_state jabber_away_state_list[] =
172{
173        { "away",  "Away" },
174        { "chat",  "Free for Chat" },
175        { "dnd",   "Do not Disturb" },
176        { "xa",    "Extended Away" },
177        { "",      "Online" },
178        { "",      NULL }
179};
180
181const struct jabber_away_state *jabber_away_state_by_code( char *code )
182{
183        int i;
184       
185        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
186                if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 )
187                        return jabber_away_state_list + i;
188       
189        return NULL;
190}
191
192const struct jabber_away_state *jabber_away_state_by_name( char *name )
193{
194        int i;
195       
196        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
197                if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 )
198                        return jabber_away_state_list + i;
199       
200        return NULL;
201}
202
203struct jabber_buddy_ask_data
204{
205        struct gaim_connection *gc;
206        char *handle;
207        char *realname;
208};
209
210static void jabber_buddy_ask_yes( gpointer w, struct jabber_buddy_ask_data *bla )
211{
212        presence_send_request( bla->gc, bla->handle, "subscribed" );
213       
214        if( find_buddy( bla->gc, bla->handle ) == NULL )
215                show_got_added( bla->gc, bla->handle, NULL );
216       
217        g_free( bla->handle );
218        g_free( bla );
219}
220
221static void jabber_buddy_ask_no( gpointer w, struct jabber_buddy_ask_data *bla )
222{
223        presence_send_request( bla->gc, bla->handle, "subscribed" );
224       
225        g_free( bla->handle );
226        g_free( bla );
227}
228
229void jabber_buddy_ask( struct gaim_connection *gc, char *handle )
230{
231        struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 );
232        char *buf;
233       
234        bla->gc = gc;
235        bla->handle = g_strdup( handle );
236       
237        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle );
238        do_ask_dialog( gc, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no );
239        g_free( buf );
240}
241
242/* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a
243   FULL jid or if we already have this buddy/resource. */
244struct jabber_buddy *jabber_buddy_add( struct gaim_connection *gc, char *full_jid )
245{
246        struct jabber_data *jd = gc->proto_data;
247        struct jabber_buddy *bud, *new, *bi;
248        char *s;
249       
250        if( !( s = strchr( full_jid, '/' ) ) )
251                return NULL;
252       
253        new = g_new0( struct jabber_buddy, 1 );
254       
255        *s = 0;
256        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
257        {
258                new->handle = bud->handle;
259               
260                /* We already have another resource for this buddy, add the
261                   new one to the list. */
262                for( bi = bud; bi; bi = bi->next )
263                {
264                        /* Check for dupes. Resource seem to be case sensitive. */
265                        if( strcmp( bi->resource, s + 1 ) == 0 )
266                        {
267                                *s = '/';
268                                g_free( new );
269                                return NULL;
270                        }
271                        /* Append the new item to the list. */
272                        else if( bi->next == NULL )
273                        {
274                                bi->next = new;
275                                break;
276                        }
277                }
278        }
279        else
280        {
281                new->handle = g_strdup( full_jid );
282                g_hash_table_insert( jd->buddies, new->handle, new );
283        }
284       
285        *s = '/';
286        new->full_jid = g_strdup( full_jid );
287        new->resource = strchr( new->full_jid, '/' ) + 1;
288       
289        return new;
290}
291
292/* Finds a buddy from our structures. Can find both full- and bare JIDs. When
293   asked for a bare JID, it uses the "resource_select" setting to see which
294   resource to pick. */
295struct jabber_buddy *jabber_buddy_by_jid( struct gaim_connection *gc, char *jid )
296{
297        struct jabber_data *jd = gc->proto_data;
298        struct jabber_buddy *bud;
299        char *s;
300       
301        if( ( s = strchr( jid, '/' ) ) )
302        {
303                *s = 0;
304                if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) )
305                        for( ; bud; bud = bud->next )
306                                if( strcmp( bud->resource, s + 1 ) == 0 )
307                                        break;
308        }
309        else
310        {
311                struct jabber_buddy *best_prio, *best_time;
312                char *set;
313               
314                best_prio = best_time = bud = g_hash_table_lookup( jd->buddies, jid );
315                for( ; bud; bud = bud->next )
316                {
317                        if( bud->priority > best_prio->priority )
318                                best_prio = bud;
319                        if( bud->last_act > best_time->last_act )
320                                best_time = bud;
321                }
322               
323                if( ( set = set_getstr( &gc->acc->set, "resource_select" ) ) == NULL )
324                        return NULL;
325                else if( strcmp( set, "activity" ) == 0 )
326                        return best_time;
327                else /* if( strcmp( set, "priority" ) == 0 ) */
328                        return best_prio;
329        }
330       
331        *s = '/';
332        return bud;
333}
334
335/* Remove one specific full JID from our list. Use this when a buddy goes
336   off-line (because (s)he can still be online from a different location. */
337int jabber_buddy_remove( struct gaim_connection *gc, char *full_jid )
338{
339        struct jabber_data *jd = gc->proto_data;
340        struct jabber_buddy *bud, *prev, *bi;
341        char *s;
342       
343        if( !( s = strchr( full_jid, '/' ) ) )
344                return 0;
345       
346        *s = 0;
347        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
348        {
349                /* If there's only one item in the list (and if the resource
350                   matches), removing it is simple. (And the hash reference
351                   should be removed too!) */
352                if( bud->next == NULL && strcmp( bud->resource, s + 1 ) == 0 )
353                {
354                        g_hash_table_remove( jd->buddies, bud->handle );
355                        g_free( bud->handle );
356                        g_free( bud->full_jid );
357                        g_free( bud->away_message );
358                        g_free( bud );
359                }
360                else
361                {
362                        for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next )
363                                if( strcmp( bi->resource, s + 1 ) == 0 )
364                                        break;
365                       
366                        if( bi )
367                        {
368                                if( prev )
369                                        prev->next = bi->next;
370                                else
371                                        /* The hash table should point at the second
372                                           item, because we're removing the first. */
373                                        g_hash_table_replace( jd->buddies, bi->handle, bi->next );
374                               
375                                g_free( bi->full_jid );
376                                g_free( bi->away_message );
377                                g_free( bi );
378                        }
379                        else
380                        {
381                                *s = '/';
382                                return 0;
383                        }
384                }
385               
386                *s = '/';
387                return 1;
388        }
389        else
390        {
391                *s = '/';
392                return 0;
393        }
394}
395
396/* Remove a buddy completely; removes all resources that belong to the
397   specified bare JID. Use this when removing someone from the contact
398   list, for example. */
399int jabber_buddy_remove_bare( struct gaim_connection *gc, char *bare_jid )
400{
401        struct jabber_data *jd = gc->proto_data;
402        struct jabber_buddy *bud, *next;
403       
404        if( strchr( bare_jid, '/' ) )
405                return 0;
406       
407        if( ( bud = g_hash_table_lookup( jd->buddies, bare_jid ) ) )
408        {
409                /* Most important: Remove the hash reference. We don't know
410                   this buddy anymore. */
411                g_hash_table_remove( jd->buddies, bud->handle );
412               
413                /* Deallocate the linked list of resources. */
414                while( bud )
415                {
416                        next = bud->next;
417                        g_free( bud->full_jid );
418                        g_free( bud->away_message );
419                        g_free( bud );
420                        bud = next;
421                }
422               
423                return 1;
424        }
425        else
426        {
427                return 0;
428        }
429}
Note: See TracBrowser for help on using the repository browser.