source: protocols/jabber/jabber_util.c @ 08135df

Last change on this file since 08135df was 08135df, checked in by ulim <a.sporto+bee@…>, at 2007-12-04T01:08:43Z

Merged in current devel

Wilmer van der Gaast 2007-12-02 Imported setuid() patch from Simo Leone <simo@archlinux...> with some

Wilmer van der Gaast 2007-12-02 Forgot to return something in jabber_chat_join_failed().
Wilmer van der Gaast 2007-12-02 Merging a change I should've pulled before committing three other changes.
Wilmer van der Gaast 2007-12-02 Added charset checks on incoming msgs (from the IRC side) to prevent possible
Wilmer van der Gaast 2007-12-02 Handling of presence-error packets (only useful for groupchats now), moved
Wilmer van der Gaast 2007-12-02 Defining DEBUG via CFLAGS so that it'll always be there, even when a file
Wilmer van der Gaast 2007-12-02 Removed retarded printf() (ARGH) and moved the event handling handling of
Wilmer van der Gaast 2007-11-29 printf() in daemons considered harmful.
Wilmer van der Gaast 2007-11-28 Fixed the epoll+ForkDaemon combination. The libevent event handling

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