source: protocols/jabber/jabber_util.c @ 6a1128d

Last change on this file since 6a1128d was 6a1128d, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-09T18:19:05Z

The module now keeps track of all resources available for a buddy. This
means the buddy won't show up offline when one resource goes down (while
there are still others available). It also remembers away state
information for every separate resource. Later this system will be used
to keep track of client capability information (Typing notices, yay...)
and who knows what else.

  • Property mode set to 100644
File size: 9.1 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        char *ret;
32       
33        ret = set_eval_int( set, value );
34       
35        /* Only run this stuff if the account is online ATM,
36           and if the setting seems to be acceptable. */
37        if( acc->gc && ret )
38        {
39                /* Although set_eval functions usually are very nice and
40                   convenient, they have one disadvantage: If I would just
41                   call p_s_u() now to send the new prio setting, it would
42                   send the old setting because the set->value gets changed
43                   when the eval returns a non-NULL value.
44                   
45                   So now I can choose between implementing post-set
46                   functions next to evals, or just do this little hack: */
47               
48                g_free( set->value );
49                set->value = g_strdup( ret );
50               
51                /* (Yes, sorry, I prefer the hack. :-P) */
52               
53                presence_send_update( acc->gc );
54        }
55       
56        return ret;
57}
58
59char *set_eval_tls( set_t *set, char *value )
60{
61        if( g_strcasecmp( value, "try" ) == 0 )
62                return value;
63        else
64                return set_eval_bool( set, value );
65}
66
67struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children )
68{
69        struct xt_node *node;
70       
71        node = xt_new_node( name, NULL, children );
72       
73        if( type )
74                xt_add_attr( node, "type", type );
75        if( to )
76                xt_add_attr( node, "to", to );
77       
78        return node;
79}
80
81/* Cache a node/packet for later use. Mainly useful for IQ packets if you need
82   them when you receive the response. Use this BEFORE sending the packet so
83   it'll get an id= tag, and do NOT free() the packet after writing it! */
84void jabber_cache_add( struct gaim_connection *gc, struct xt_node *node, jabber_cache_event func )
85{
86        struct jabber_data *jd = gc->proto_data;
87        char *id = g_strdup_printf( "BeeX%04x", next_id++ );
88        struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 );
89       
90        xt_add_attr( node, "id", id );
91        g_free( id );
92       
93        entry->node = node;
94        entry->func = func;
95        g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry );
96}
97
98void jabber_cache_entry_free( gpointer data )
99{
100        struct jabber_cache_entry *entry = data;
101       
102        xt_free_node( entry->node );
103        g_free( entry );
104}
105
106gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer );
107
108/* This one should be called from time to time (from keepalive, in this case)
109   to make sure things don't stay in the node cache forever. By marking nodes
110   during the first run and deleting marked nodes during a next run, every
111   node should be available in the cache for at least a minute (assuming the
112   function is indeed called every minute). */
113void jabber_cache_clean( struct gaim_connection *gc )
114{
115        struct jabber_data *jd = gc->proto_data;
116       
117        g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, NULL );
118}
119
120gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer nullpointer )
121{
122        struct jabber_cache_entry *entry = entry_;
123        struct xt_node *node = entry->node;
124       
125        if( node->flags & XT_SEEN )
126                return TRUE;
127        else
128        {
129                node->flags |= XT_SEEN;
130                return FALSE;
131        }
132}
133
134const struct jabber_away_state jabber_away_state_list[] =
135{
136        { "away",  "Away" },
137        { "chat",  "Free for Chat" },
138        { "dnd",   "Do not Disturb" },
139        { "xa",    "Extended Away" },
140        { "",      "Online" },
141        { "",      NULL }
142};
143
144const struct jabber_away_state *jabber_away_state_by_code( char *code )
145{
146        int i;
147       
148        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
149                if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 )
150                        return jabber_away_state_list + i;
151       
152        return NULL;
153}
154
155const struct jabber_away_state *jabber_away_state_by_name( char *name )
156{
157        int i;
158       
159        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
160                if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 )
161                        return jabber_away_state_list + i;
162       
163        return NULL;
164}
165
166struct jabber_buddy_ask_data
167{
168        struct gaim_connection *gc;
169        char *handle;
170        char *realname;
171};
172
173static void jabber_buddy_ask_yes( gpointer w, struct jabber_buddy_ask_data *bla )
174{
175        presence_send_request( bla->gc, bla->handle, "subscribed" );
176       
177        if( find_buddy( bla->gc, bla->handle ) == NULL )
178                show_got_added( bla->gc, bla->handle, NULL );
179       
180        g_free( bla->handle );
181        g_free( bla );
182}
183
184static void jabber_buddy_ask_no( gpointer w, struct jabber_buddy_ask_data *bla )
185{
186        presence_send_request( bla->gc, bla->handle, "subscribed" );
187       
188        g_free( bla->handle );
189        g_free( bla );
190}
191
192void jabber_buddy_ask( struct gaim_connection *gc, char *handle )
193{
194        struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 );
195        char *buf;
196       
197        bla->gc = gc;
198        bla->handle = g_strdup( handle );
199       
200        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle );
201        do_ask_dialog( gc, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no );
202        g_free( buf );
203}
204
205/* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a
206   FULL jid or if we already have this buddy/resource. */
207struct jabber_buddy *jabber_buddy_add( struct gaim_connection *gc, char *full_jid )
208{
209        struct jabber_data *jd = gc->proto_data;
210        struct jabber_buddy *bud, *new, *bi;
211        char *s;
212       
213        if( !( s = strchr( full_jid, '/' ) ) )
214                return NULL;
215       
216        new = g_new0( struct jabber_buddy, 1 );
217       
218        *s = 0;
219        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
220        {
221                new->handle = bud->handle;
222               
223                /* We already have another resource for this buddy, add the
224                   new one to the list. */
225                for( bi = bud; bi; bi = bi->next )
226                {
227                        /* Check for dupes. Resource seem to be case sensitive. */
228                        if( strcmp( bi->resource, s + 1 ) == 0 )
229                        {
230                                *s = '/';
231                                g_free( new );
232                                return NULL;
233                        }
234                        /* Append the new item to the list. */
235                        else if( bi->next == NULL )
236                        {
237                                bi->next = new;
238                                break;
239                        }
240                }
241        }
242        else
243        {
244                new->handle = g_strdup( full_jid );
245                g_hash_table_insert( jd->buddies, new->handle, new );
246        }
247       
248        *s = '/';
249        new->resource = g_strdup( s + 1 );
250       
251        return new;
252}
253
254struct jabber_buddy *jabber_buddy_by_jid( struct gaim_connection *gc, char *jid )
255{
256        struct jabber_data *jd = gc->proto_data;
257        struct jabber_buddy *bud;
258        char *s;
259       
260        if( ( s = strchr( jid, '/' ) ) )
261        {
262                *s = 0;
263                if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) )
264                        for( ; bud; bud = bud->next )
265                                if( strcmp( bud->resource, s + 1 ) == 0 )
266                                        break;
267        }
268        else
269        {
270                /* TODO: Add selection. */
271                return g_hash_table_lookup( jd->buddies, jid );
272        }
273       
274        *s = '/';
275        return bud;
276}
277
278int jabber_buddy_remove( struct gaim_connection *gc, char *full_jid )
279{
280        struct jabber_data *jd = gc->proto_data;
281        struct jabber_buddy *bud, *prev, *bi;
282        char *s;
283       
284        if( !( s = strchr( full_jid, '/' ) ) )
285                return 0;
286       
287        *s = 0;
288        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
289        {
290                /* If there's only one item in the list (and if the resource
291                   matches), removing it is simple. (And the hash reference
292                   should be removed too!) */
293                if( bud->next == NULL && strcmp( bud->resource, s + 1 ) == 0 )
294                {
295                        g_hash_table_remove( jd->buddies, bud->handle );
296                        g_free( bud->handle );
297                        g_free( bud->resource );
298                        g_free( bud->away_message );
299                        g_free( bud );
300                }
301                else
302                {
303                        for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next )
304                                if( strcmp( bi->resource, s + 1 ) == 0 )
305                                        break;
306                       
307                        if( bi )
308                        {
309                                if( prev )
310                                        prev->next = bi->next;
311                                else
312                                        /* The hash table should point at the second
313                                           item, because we're removing the first. */
314                                        g_hash_table_replace( jd->buddies, bi->handle, bi->next );
315                               
316                                g_free( bi->resource );
317                                g_free( bi->away_message );
318                                g_free( bi );
319                        }
320                        else
321                        {
322                                *s = '/';
323                                return 0;
324                        }
325                }
326               
327                *s = '/';
328                return 1;
329        }
330        else
331        {
332                *s = '/';
333                return 0;
334        }
335}
Note: See TracBrowser for help on using the repository browser.