source: protocols/jabber/jabber_util.c @ 20e830b

Last change on this file since 20e830b was 20e830b, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-11-19T14:58:30Z

Fixed a facepalm kind of NULL pointer dereference bug.

  • Property mode set to 100644
File size: 20.2 KB
RevLine 
[f06894d]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
[21167d2]4*  Jabber module - Misc. stuff                                              *
[f06894d]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"
[89d736a]25#include "md5.h"
26#include "base64.h"
[f06894d]27
[dfa41a4]28static unsigned int next_id = 1;
[21167d2]29
[ebe7b36]30char *set_eval_priority( set_t *set, char *value )
[f06894d]31{
32        account_t *acc = set->data;
[788a1af]33        int i;
[f06894d]34       
[788a1af]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 )
[7125cb3]39                        return SET_INVALID;
[788a1af]40        }
41        else
[7125cb3]42                return SET_INVALID;
[172a73f1]43       
44        /* Only run this stuff if the account is online ATM,
45           and if the setting seems to be acceptable. */
[0da65d5]46        if( acc->ic )
[f06894d]47        {
[ebe7b36]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
[e35d1a1]52                   after the (this) eval returns a non-NULL value.
[ebe7b36]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 );
[788a1af]58                set->value = g_strdup( value );
[ebe7b36]59               
60                /* (Yes, sorry, I prefer the hack. :-P) */
61               
[0da65d5]62                presence_send_update( acc->ic );
[f06894d]63        }
64       
[788a1af]65        return value;
[f06894d]66}
67
68char *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}
[21167d2]75
76struct 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       
[dfa41a4]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       
[fe7a554]98        return node;
99}
100
[259edd4]101struct 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 );
[47d3ac4]108        xt_add_attr( c, "xmlns", XMLNS_STANZA_ERROR );
[259edd4]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
[dfa41a4]131/* Cache a node/packet for later use. Mainly useful for IQ packets if you need
[fe7a554]132   them when you receive the response. Use this BEFORE sending the packet so
[e35d1a1]133   it'll get a new id= tag, and do NOT free() the packet after sending it! */
[0da65d5]134void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func )
[fe7a554]135{
[0da65d5]136        struct jabber_data *jd = ic->proto_data;
[038d17f]137        struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 );
[89d736a]138        md5_state_t id_hash;
139        md5_byte_t id_sum[16];
140        char *id, *asc_hash;
[fe7a554]141       
[89d736a]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 );
[fe7a554]150        xt_add_attr( node, "id", id );
[21167d2]151        g_free( id );
[89d736a]152        g_free( asc_hash );
[038d17f]153       
154        entry->node = node;
[861c199]155        entry->func = func;
[979cfb4]156        entry->saved_at = time( NULL );
[038d17f]157        g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry );
[fe7a554]158}
159
[038d17f]160void jabber_cache_entry_free( gpointer data )
161{
162        struct jabber_cache_entry *entry = data;
[21167d2]163       
[038d17f]164        xt_free_node( entry->node );
165        g_free( entry );
166}
167
168gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer );
169
[861c199]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). */
[0da65d5]175void jabber_cache_clean( struct im_connection *ic )
[038d17f]176{
[0da65d5]177        struct jabber_data *jd = ic->proto_data;
[979cfb4]178        time_t threshold = time( NULL ) - JABBER_CACHE_MAX_AGE;
[038d17f]179       
[979cfb4]180        g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, &threshold );
[038d17f]181}
182
[979cfb4]183gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer threshold_ )
[038d17f]184{
185        struct jabber_cache_entry *entry = entry_;
[979cfb4]186        time_t *threshold = threshold_;
[038d17f]187       
[979cfb4]188        return entry->saved_at < *threshold;
[21167d2]189}
[5e202b0]190
[4306d8b]191xt_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 ||
[89d736a]198            strncmp( s, JABBER_CACHED_ID, strlen( JABBER_CACHED_ID ) ) != 0 )
[4306d8b]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        {
[89d736a]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               
[43462708]215                imcb_log( ic, "Warning: Received %s-%s packet with unknown/expired ID %s!",
[4306d8b]216                              node->name, xt_find_attr( node, "type" ) ? : "(no type)", s );
[89d736a]217                */
[4306d8b]218        }
219        else if( entry->func )
220        {
221                return entry->func( ic, node, entry->node );
222        }
223       
224        return XT_HANDLED;
225}
226
[5e202b0]227const 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
237const 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
248const 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}
[8e5e2e9]258
259struct jabber_buddy_ask_data
260{
[0da65d5]261        struct im_connection *ic;
[8e5e2e9]262        char *handle;
263        char *realname;
264};
265
[9143aeb]266static void jabber_buddy_ask_yes( void *data )
[8e5e2e9]267{
[9143aeb]268        struct jabber_buddy_ask_data *bla = data;
269       
[0da65d5]270        presence_send_request( bla->ic, bla->handle, "subscribed" );
[8e5e2e9]271       
[f0cb961]272        if( imcb_find_buddy( bla->ic, bla->handle ) == NULL )
[84b045d]273                imcb_ask_add( bla->ic, bla->handle, NULL );
[8e5e2e9]274       
275        g_free( bla->handle );
276        g_free( bla );
277}
278
[9143aeb]279static void jabber_buddy_ask_no( void *data )
[8e5e2e9]280{
[9143aeb]281        struct jabber_buddy_ask_data *bla = data;
282       
[0da65d5]283        presence_send_request( bla->ic, bla->handle, "subscribed" );
[8e5e2e9]284       
285        g_free( bla->handle );
286        g_free( bla );
287}
288
[0da65d5]289void jabber_buddy_ask( struct im_connection *ic, char *handle )
[8e5e2e9]290{
291        struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 );
292        char *buf;
293       
[0da65d5]294        bla->ic = ic;
[8e5e2e9]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 );
[84b045d]298        imcb_ask( ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no );
[6266fca]299        g_free( buf );
[8e5e2e9]300}
[6a1128d]301
[0d3f30f]302/* Returns a new string. Don't leak it! */
[e35d1a1]303char *jabber_normalize( const char *orig )
[0d3f30f]304{
305        int len, i;
306        char *new;
307       
308        len = strlen( orig );
309        new = g_new( char, len + 1 );
[98de2cc]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 ++ )
[0d3f30f]314                new[i] = tolower( orig[i] );
[98de2cc]315        for( ; orig[i]; i ++ )
316                new[i] = orig[i];
[0d3f30f]317       
318        new[i] = 0;
319        return new;
320}
321
[6a1128d]322/* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a
[0d3f30f]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... */
[0da65d5]327struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid_ )
[6a1128d]328{
[0da65d5]329        struct jabber_data *jd = ic->proto_data;
[6a1128d]330        struct jabber_buddy *bud, *new, *bi;
[0d3f30f]331        char *s, *full_jid;
[6a1128d]332       
[0d3f30f]333        full_jid = jabber_normalize( full_jid_ );
334       
335        if( ( s = strchr( full_jid, '/' ) ) )
336                *s = 0;
[6a1128d]337       
338        new = g_new0( struct jabber_buddy, 1 );
339       
340        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
341        {
[76c85b4c]342                /* The first entry is always a bare JID. If there are more, we
343                   should ignore the first one here. */
344                if( bud->next )
345                        bud = bud->next;
346               
[0d3f30f]347                /* If this is a transport buddy or whatever, it can't have more
348                   than one instance, so this is always wrong: */
349                if( s == NULL || bud->resource == NULL )
350                {
351                        if( s ) *s = '/';
352                        g_free( new );
353                        g_free( full_jid );
354                        return NULL;
355                }
356               
357                new->bare_jid = bud->bare_jid;
[6a1128d]358               
359                /* We already have another resource for this buddy, add the
360                   new one to the list. */
361                for( bi = bud; bi; bi = bi->next )
362                {
[0d3f30f]363                        /* Check for dupes. */
[98de2cc]364                        if( strcmp( bi->resource, s + 1 ) == 0 )
[6a1128d]365                        {
366                                *s = '/';
367                                g_free( new );
[0d3f30f]368                                g_free( full_jid );
[6a1128d]369                                return NULL;
370                        }
371                        /* Append the new item to the list. */
372                        else if( bi->next == NULL )
373                        {
374                                bi->next = new;
375                                break;
376                        }
377                }
378        }
379        else
380        {
[76c85b4c]381                new->full_jid = new->bare_jid = g_strdup( full_jid );
[0d3f30f]382                g_hash_table_insert( jd->buddies, new->bare_jid, new );
[76c85b4c]383               
384                if( s )
385                {
386                        new->next = g_new0( struct jabber_buddy, 1 );
387                        new->next->bare_jid = new->bare_jid;
388                        new = new->next;
389                }
[6a1128d]390        }
391       
[0d3f30f]392        if( s )
393        {
394                *s = '/';
395                new->full_jid = full_jid;
396                new->resource = strchr( new->full_jid, '/' ) + 1;
397        }
398        else
399        {
400                /* Let's waste some more bytes of RAM instead of to make
[b9f8b87]401                   memory management a total disaster here. And it saves
402                   me one g_free() call in this function. :-P */
[0d3f30f]403                new->full_jid = full_jid;
404        }
[6a1128d]405       
406        return new;
407}
408
[788a1af]409/* Finds a buddy from our structures. Can find both full- and bare JIDs. When
410   asked for a bare JID, it uses the "resource_select" setting to see which
411   resource to pick. */
[0da65d5]412struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags )
[6a1128d]413{
[0da65d5]414        struct jabber_data *jd = ic->proto_data;
[76c85b4c]415        struct jabber_buddy *bud, *head;
[0d3f30f]416        char *s, *jid;
417       
418        jid = jabber_normalize( jid_ );
[6a1128d]419       
420        if( ( s = strchr( jid, '/' ) ) )
421        {
422                *s = 0;
423                if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) )
[0d3f30f]424                {
[76c85b4c]425                        if( bud->next )
426                                bud = bud->next;
427                       
[0adce21]428                        /* Just return the first one for this bare JID. */
429                        if( flags & GET_BUDDY_FIRST )
430                        {
431                                *s = '/';
432                                g_free( jid );
433                                return bud;
434                        }
435                       
[0d3f30f]436                        /* Is this one of those no-resource buddies? */
437                        if( bud->resource == NULL )
438                        {
[0adce21]439                                *s = '/';
[16b5f86]440                                g_free( jid );
441                                return NULL;
[0d3f30f]442                        }
[0adce21]443                       
444                        /* See if there's an exact match. */
445                        for( ; bud; bud = bud->next )
[98de2cc]446                                if( strcmp( bud->resource, s + 1 ) == 0 )
[0adce21]447                                        break;
[0d3f30f]448                }
449               
[76c85b4c]450                if( bud == NULL && ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid ) )
[16b5f86]451                {
452                        *s = '/';
[0da65d5]453                        bud = jabber_buddy_add( ic, jid );
[16b5f86]454                }
[0d3f30f]455               
456                g_free( jid );
457                return bud;
[6a1128d]458        }
459        else
460        {
[a21a8ac]461                struct jabber_buddy *best_prio, *best_time;
462                char *set;
463               
[76c85b4c]464                head = g_hash_table_lookup( jd->buddies, jid );
[20e830b]465                bud = ( head && head->next ) ? head->next : head;
[0d3f30f]466               
467                g_free( jid );
468               
469                if( bud == NULL )
[16b5f86]470                        /* No match. Create it now? */
[f0cb961]471                        return ( ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid_ ) ) ?
[0da65d5]472                                   jabber_buddy_add( ic, jid_ ) : NULL;
[16b5f86]473                else if( bud->resource && ( flags & GET_BUDDY_EXACT ) )
474                        /* We want an exact match, so in thise case there shouldn't be a /resource. */
475                        return NULL;
[98de2cc]476                else if( bud->resource == NULL || bud->next == NULL )
[16b5f86]477                        /* No need for selection if there's only one option. */
[0d3f30f]478                        return bud;
[0adce21]479                else if( flags & GET_BUDDY_FIRST )
480                        /* Looks like the caller doesn't care about details. */
481                        return bud;
[76c85b4c]482                else if( flags & GET_BUDDY_BARE )
483                        return head;
[0d3f30f]484               
485                best_prio = best_time = bud;
[a21a8ac]486                for( ; bud; bud = bud->next )
487                {
488                        if( bud->priority > best_prio->priority )
489                                best_prio = bud;
[76c85b4c]490                        if( bud->last_msg > best_time->last_msg )
[a21a8ac]491                                best_time = bud;
492                }
493               
[0da65d5]494                if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL )
[a21a8ac]495                        return NULL;
[76c85b4c]496                else if( strcmp( set, "priority" ) == 0 )
[a21a8ac]497                        return best_prio;
[76c85b4c]498                else if( flags & GET_BUDDY_BARE_OK ) /* && strcmp( set, "activity" ) == 0 */
499                {
500                        if( best_time->last_msg + set_getint( &ic->acc->set, "activity_timeout" ) >= time( NULL ) )
501                                return best_time;
502                        else
503                                return head;
504                }
505                else
506                        return best_time;
[6a1128d]507        }
508}
509
[b9f8b87]510/* I'm keeping a separate ext_jid attribute to save a JID that makes sense
511   to export to BitlBee. This is mainly for groupchats right now. It's
512   a bit of a hack, but I just think having the user nickname in the hostname
513   part of the hostmask doesn't look nice on IRC. Normally you can convert
514   a normal JID to ext_jid by swapping the part before and after the / and
515   replacing the / with a =. But there should be some stripping (@s are
516   allowed in Jabber nicks...). */
517struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags )
518{
519        struct jabber_buddy *bud;
520        char *s, *jid;
521       
522        jid = jabber_normalize( jid_ );
523       
524        if( ( s = strchr( jid, '=' ) ) == NULL )
525                return NULL;
526       
527        for( bud = jabber_buddy_by_jid( ic, s + 1, GET_BUDDY_FIRST ); bud; bud = bud->next )
528        {
529                /* Hmmm, could happen if not all people in the chat are anonymized? */
530                if( bud->ext_jid == NULL )
531                        continue;
532               
533                if( strcmp( bud->ext_jid, jid ) == 0 )
534                        break;
535        }
536       
537        g_free( jid );
538       
539        return bud;
540}
541
[788a1af]542/* Remove one specific full JID from our list. Use this when a buddy goes
[0d3f30f]543   off-line (because (s)he can still be online from a different location.
544   XXX: See above, we should accept bare JIDs too... */
[0da65d5]545int jabber_buddy_remove( struct im_connection *ic, char *full_jid_ )
[6a1128d]546{
[0da65d5]547        struct jabber_data *jd = ic->proto_data;
[20e830b]548        struct jabber_buddy *bud, *prev, *bi;
[0d3f30f]549        char *s, *full_jid;
[6a1128d]550       
[0d3f30f]551        full_jid = jabber_normalize( full_jid_ );
552       
553        if( ( s = strchr( full_jid, '/' ) ) )
554                *s = 0;
[6a1128d]555       
[20e830b]556        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
[6a1128d]557        {
[20e830b]558                if( bud->next )
559                        bud = bud->next;
[76c85b4c]560               
[6a1128d]561                /* If there's only one item in the list (and if the resource
562                   matches), removing it is simple. (And the hash reference
563                   should be removed too!) */
[3e6764a]564                if( bud->next == NULL &&
565                    ( ( s == NULL && bud->resource == NULL ) ||
[98de2cc]566                      ( bud->resource && s && strcmp( bud->resource, s + 1 ) == 0 ) ) )
[6a1128d]567                {
[76c85b4c]568                        return jabber_buddy_remove_bare( ic, full_jid );
[0d3f30f]569                }
570                else if( s == NULL || bud->resource == NULL )
571                {
572                        /* Tried to remove a bare JID while this JID does seem
573                           to have resources... (Or the opposite.) *sigh* */
574                        g_free( full_jid );
575                        return 0;
[6a1128d]576                }
577                else
578                {
579                        for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next )
[98de2cc]580                                if( strcmp( bi->resource, s + 1 ) == 0 )
[6a1128d]581                                        break;
582                       
[0d3f30f]583                        g_free( full_jid );
584                       
[6a1128d]585                        if( bi )
586                        {
587                                if( prev )
588                                        prev->next = bi->next;
589                                else
590                                        /* The hash table should point at the second
591                                           item, because we're removing the first. */
[0d3f30f]592                                        g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next );
[6a1128d]593                               
[6286f80]594                                g_free( bi->ext_jid );
[a21a8ac]595                                g_free( bi->full_jid );
[6a1128d]596                                g_free( bi->away_message );
597                                g_free( bi );
[0d3f30f]598                               
599                                return 1;
[6a1128d]600                        }
601                        else
602                        {
603                                return 0;
604                        }
605                }
606        }
607        else
608        {
[0d3f30f]609                g_free( full_jid );
[6a1128d]610                return 0;
611        }
612}
[788a1af]613
614/* Remove a buddy completely; removes all resources that belong to the
615   specified bare JID. Use this when removing someone from the contact
616   list, for example. */
[9da0bbf]617int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid )
[788a1af]618{
[0da65d5]619        struct jabber_data *jd = ic->proto_data;
[788a1af]620        struct jabber_buddy *bud, *next;
621       
[9da0bbf]622        if( strchr( bare_jid, '/' ) )
[788a1af]623                return 0;
624       
[9da0bbf]625        if( ( bud = jabber_buddy_by_jid( ic, bare_jid, GET_BUDDY_FIRST ) ) )
[788a1af]626        {
627                /* Most important: Remove the hash reference. We don't know
628                   this buddy anymore. */
[0d3f30f]629                g_hash_table_remove( jd->buddies, bud->bare_jid );
[9da0bbf]630                g_free( bud->bare_jid );
[788a1af]631               
632                /* Deallocate the linked list of resources. */
633                while( bud )
634                {
[9da0bbf]635                        /* ext_jid && anonymous means that this buddy is
636                           specific to one groupchat (the one we're
637                           currently cleaning up) so it can be deleted
638                           completely. */
639                        if( bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS )
640                                imcb_remove_buddy( ic, bud->ext_jid, NULL );
641                       
[788a1af]642                        next = bud->next;
[6286f80]643                        g_free( bud->ext_jid );
[788a1af]644                        g_free( bud->full_jid );
645                        g_free( bud->away_message );
646                        g_free( bud );
647                        bud = next;
648                }
649               
650                return 1;
651        }
652        else
653        {
654                return 0;
655        }
656}
[e35d1a1]657
[43671b9]658time_t jabber_get_timestamp( struct xt_node *xt )
659{
660        struct tm tp, utc;
661        struct xt_node *c;
662        time_t res, tres;
663        char *s = NULL;
664       
665        for( c = xt->children; ( c = xt_find_node( c, "x" ) ); c = c->next )
666        {
667                if( ( s = xt_find_attr( c, "xmlns" ) ) && strcmp( s, XMLNS_DELAY ) == 0 )
668                        break;
669        }
670       
671        if( !c || !( s = xt_find_attr( c, "stamp" ) ) )
672                return 0;
673       
674        memset( &tp, 0, sizeof( tp ) );
675        if( sscanf( s, "%4d%2d%2dT%2d:%2d:%2d", &tp.tm_year, &tp.tm_mon, &tp.tm_mday,
676                                                &tp.tm_hour, &tp.tm_min, &tp.tm_sec ) != 6 )
677                return 0;
678       
679        tp.tm_year -= 1900;
680        tp.tm_mon --;
681        tp.tm_isdst = -1; /* GRRRRRRRRRRR */
682       
683        res = mktime( &tp );
684        /* Problem is, mktime() just gave us the GMT timestamp for the
685           given local time... While the given time WAS NOT local. So
686           we should fix this now.
687       
688           Now I could choose between messing with environment variables
689           (kludgy) or using timegm() (not portable)... Or doing the
690           following, which I actually prefer... */
691        gmtime_r( &res, &utc );
692        utc.tm_isdst = -1; /* Once more: GRRRRRRRRRRRRRRRRRR!!! */
693        if( utc.tm_hour == tp.tm_hour && utc.tm_min == tp.tm_min )
694                /* Sweet! We're in UTC right now... */
695                return res;
696       
697        tres = mktime( &utc );
698        res += res - tres;
699       
700        /* Yes, this is a hack. And it will go wrong around DST changes.
701           BUT this is more likely to be threadsafe than messing with
702           environment variables, and possibly more portable... */
703       
704        return res;
705}
[1baaef8]706
707struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns )
708{
[5bd21df]709        struct jabber_error *err;
[1baaef8]710        struct xt_node *c;
711        char *s;
712       
[5bd21df]713        if( node == NULL )
714                return NULL;
715       
716        err = g_new0( struct jabber_error, 1 );
[1baaef8]717        err->type = xt_find_attr( node, "type" );
718       
719        for( c = node->children; c; c = c->next )
720        {
721                if( !( s = xt_find_attr( c, "xmlns" ) ) ||
722                    strcmp( s, xmlns ) != 0 )
723                        continue;
724               
725                if( strcmp( c->name, "text" ) != 0 )
726                {
727                        err->code = c->name;
728                }
729                /* Only use the text if it doesn't have an xml:lang attribute,
730                   if it's empty or if it's set to something English. */
731                else if( !( s = xt_find_attr( c, "xml:lang" ) ) ||
732                         !*s || strncmp( s, "en", 2 ) == 0 )
733                {
734                        err->text = c->text;
735                }
736        }
737       
738        return err;
739}
740
741void jabber_error_free( struct jabber_error *err )
742{
743        g_free( err );
744}
Note: See TracBrowser for help on using the repository browser.