source: protocols/jabber/jabber_util.c @ 608f8cf

Last change on this file since 608f8cf was 608f8cf, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-11-24T18:02:39Z

Added some random hash to the id= for cached XMPP packets so that packets
from other BitlBees won't be picked up accidentally. Might also want to
randomize the per-packet IDs because they're still predictable.

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