source: protocols/jabber/jabber_util.c @ 757515a

Last change on this file since 757515a was 757515a, checked in by dequis <dx@…>, at 2014-07-24T03:51:07Z

Added jabber_compare_jid to fix JID comparison case sensitivity

  • Property mode set to 100644
File size: 21.0 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - Misc. stuff                                              *
5*                                                                           *
6*  Copyright 2006-2010 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#include "md5.h"
26#include "base64.h"
27
28static unsigned int next_id = 1;
29
30char *set_eval_priority( set_t *set, char *value )
31{
32        account_t *acc = set->data;
33        int i;
34       
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 )
39                        return SET_INVALID;
40        }
41        else
42                return SET_INVALID;
43       
44        /* Only run this stuff if the account is online ATM,
45           and if the setting seems to be acceptable. */
46        if( acc->ic )
47        {
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
52                   after the (this) eval returns a non-NULL value.
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 );
58                set->value = g_strdup( value );
59               
60                /* (Yes, sorry, I prefer the hack. :-P) */
61               
62                presence_send_update( acc->ic );
63        }
64       
65        return value;
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}
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       
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       
98        return node;
99}
100
101struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code )
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 );
108        xt_add_attr( c, "xmlns", XMLNS_STANZA_ERROR );
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        /* Add the error code, if present */
115        if (err_code)
116                xt_add_attr( c, "code", err_code );
117       
118        /* To make the actual error packet, we copy the original packet and
119           add our <error>/type="error" tag. Including the original packet
120           is recommended, so let's just do it. */
121        node = xt_dup( orig );
122        xt_add_child( node, c );
123        xt_add_attr( node, "type", "error" );
124       
125        /* Return to sender. */
126        if( ( to = xt_find_attr( node, "from" ) ) )
127        {
128                xt_add_attr( node, "to", to );
129                xt_remove_attr( node, "from" );
130        }
131               
132        return node;
133}
134
135/* Cache a node/packet for later use. Mainly useful for IQ packets if you need
136   them when you receive the response. Use this BEFORE sending the packet so
137   it'll get a new id= tag, and do NOT free() the packet after sending it! */
138void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func )
139{
140        struct jabber_data *jd = ic->proto_data;
141        struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 );
142        md5_state_t id_hash;
143        md5_byte_t id_sum[16];
144        char *id, *asc_hash;
145       
146        next_id ++;
147       
148        id_hash = jd->cached_id_prefix;
149        md5_append( &id_hash, (md5_byte_t*) &next_id, sizeof( next_id ) );
150        md5_finish( &id_hash, id_sum );
151        asc_hash = base64_encode( id_sum, 12 );
152       
153        id = g_strdup_printf( "%s%s", JABBER_CACHED_ID, asc_hash );
154        xt_add_attr( node, "id", id );
155        g_free( id );
156        g_free( asc_hash );
157       
158        entry->node = node;
159        entry->func = func;
160        entry->saved_at = time( NULL );
161        g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry );
162}
163
164void jabber_cache_entry_free( gpointer data )
165{
166        struct jabber_cache_entry *entry = data;
167       
168        xt_free_node( entry->node );
169        g_free( entry );
170}
171
172gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer );
173
174/* This one should be called from time to time (from keepalive, in this case)
175   to make sure things don't stay in the node cache forever. By marking nodes
176   during the first run and deleting marked nodes during a next run, every
177   node should be available in the cache for at least a minute (assuming the
178   function is indeed called every minute). */
179void jabber_cache_clean( struct im_connection *ic )
180{
181        struct jabber_data *jd = ic->proto_data;
182        time_t threshold = time( NULL ) - JABBER_CACHE_MAX_AGE;
183       
184        g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, &threshold );
185}
186
187gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer threshold_ )
188{
189        struct jabber_cache_entry *entry = entry_;
190        time_t *threshold = threshold_;
191       
192        return entry->saved_at < *threshold;
193}
194
195xt_status jabber_cache_handle_packet( struct im_connection *ic, struct xt_node *node )
196{
197        struct jabber_data *jd = ic->proto_data;
198        struct jabber_cache_entry *entry;
199        char *s;
200       
201        if( ( s = xt_find_attr( node, "id" ) ) == NULL ||
202            strncmp( s, JABBER_CACHED_ID, strlen( JABBER_CACHED_ID ) ) != 0 )
203        {
204                /* Silently ignore it, without an ID (or a non-cache
205                   ID) we don't know how to handle the packet and we
206                   probably don't have to. */
207                return XT_HANDLED;
208        }
209       
210        entry = g_hash_table_lookup( jd->node_cache, s );
211       
212        if( entry == NULL )
213        {
214                /*
215                There's no longer an easy way to see if we generated this
216                one or someone else, and there's a ten-minute timeout anyway,
217                so meh.
218               
219                imcb_log( ic, "Warning: Received %s-%s packet with unknown/expired ID %s!",
220                              node->name, xt_find_attr( node, "type" ) ? : "(no type)", s );
221                */
222        }
223        else if( entry->func )
224        {
225                return entry->func( ic, node, entry->node );
226        }
227       
228        return XT_HANDLED;
229}
230
231const struct jabber_away_state jabber_away_state_list[] =
232{
233        { "away",  "Away" },
234        { "chat",  "Free for Chat" },   /* WTF actually uses this? */
235        { "dnd",   "Do not Disturb" },
236        { "xa",    "Extended Away" },
237        { "",      NULL }
238};
239
240const struct jabber_away_state *jabber_away_state_by_code( char *code )
241{
242        int i;
243       
244        if( code == NULL )
245                return NULL;
246       
247        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
248                if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 )
249                        return jabber_away_state_list + i;
250       
251        return NULL;
252}
253
254const struct jabber_away_state *jabber_away_state_by_name( char *name )
255{
256        int i;
257       
258        if( name == NULL )
259                return NULL;
260       
261        for( i = 0; jabber_away_state_list[i].full_name; i ++ )
262                if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 )
263                        return jabber_away_state_list + i;
264       
265        return NULL;
266}
267
268struct jabber_buddy_ask_data
269{
270        struct im_connection *ic;
271        char *handle;
272        char *realname;
273};
274
275static void jabber_buddy_ask_yes( void *data )
276{
277        struct jabber_buddy_ask_data *bla = data;
278       
279        presence_send_request( bla->ic, bla->handle, "subscribed" );
280       
281        imcb_ask_add( bla->ic, bla->handle, NULL );
282       
283        g_free( bla->handle );
284        g_free( bla );
285}
286
287static void jabber_buddy_ask_no( void *data )
288{
289        struct jabber_buddy_ask_data *bla = data;
290       
291        presence_send_request( bla->ic, bla->handle, "unsubscribed" );
292       
293        g_free( bla->handle );
294        g_free( bla );
295}
296
297void jabber_buddy_ask( struct im_connection *ic, char *handle )
298{
299        struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 );
300        char *buf;
301       
302        bla->ic = ic;
303        bla->handle = g_strdup( handle );
304       
305        buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle );
306        imcb_ask( ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no );
307        g_free( buf );
308}
309
310/* Compares two Jabber IDs to check for match. */
311int jabber_compare_jid( const char *jid1, const char *jid2 )
312{
313        int i;
314       
315        for( i = 0; ; i ++ )
316        {
317                if( jid1[i] == '\0' || jid1[i] == '/' || jid2[i] == '\0' || jid2[i] == '/' )
318                {
319                        if( ( jid1[i] == '\0' || jid1[i] == '/' ) && ( jid2[i] == '\0' || jid2[i] == '/' ) )
320                                break;
321                        return FALSE;
322                }
323                if( tolower( jid1[i] ) != tolower( jid2[i] ) )
324                {
325                        return FALSE;
326                }
327        }
328       
329        return TRUE;
330}
331
332/* Returns a new string. Don't leak it! */
333char *jabber_normalize( const char *orig )
334{
335        int len, i;
336        char *new;
337       
338        len = strlen( orig );
339        new = g_new( char, len + 1 );
340       
341        /* So it turns out the /resource part is case sensitive. Yeah, and
342           it's Unicode but feck Unicode. :-P So stop once we see a slash. */
343        for( i = 0; i < len && orig[i] != '/' ; i ++ )
344                new[i] = tolower( orig[i] );
345        for( ; orig[i]; i ++ )
346                new[i] = orig[i];
347       
348        new[i] = 0;
349        return new;
350}
351
352/* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a
353   FULL jid or if we already have this buddy/resource. XXX: No, great, actually
354   buddies from transports don't (usually) have resources. So we'll really have
355   to deal with that properly. Set their ->resource property to NULL. Do *NOT*
356   allow to mix this stuff, though... */
357struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid_ )
358{
359        struct jabber_data *jd = ic->proto_data;
360        struct jabber_buddy *bud, *new, *bi;
361        char *s, *full_jid;
362       
363        full_jid = jabber_normalize( full_jid_ );
364       
365        if( ( s = strchr( full_jid, '/' ) ) )
366                *s = 0;
367       
368        new = g_new0( struct jabber_buddy, 1 );
369       
370        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
371        {
372                /* The first entry is always a bare JID. If there are more, we
373                   should ignore the first one here. */
374                if( bud->next )
375                        bud = bud->next;
376               
377                /* If this is a transport buddy or whatever, it can't have more
378                   than one instance, so this is always wrong: */
379                if( s == NULL || bud->resource == NULL )
380                {
381                        if( s ) *s = '/';
382                        g_free( new );
383                        g_free( full_jid );
384                        return NULL;
385                }
386               
387                new->bare_jid = bud->bare_jid;
388               
389                /* We already have another resource for this buddy, add the
390                   new one to the list. */
391                for( bi = bud; bi; bi = bi->next )
392                {
393                        /* Check for dupes. */
394                        if( strcmp( bi->resource, s + 1 ) == 0 )
395                        {
396                                *s = '/';
397                                g_free( new );
398                                g_free( full_jid );
399                                return NULL;
400                        }
401                        /* Append the new item to the list. */
402                        else if( bi->next == NULL )
403                        {
404                                bi->next = new;
405                                break;
406                        }
407                }
408        }
409        else
410        {
411                new->full_jid = new->bare_jid = g_strdup( full_jid );
412                g_hash_table_insert( jd->buddies, new->bare_jid, new );
413               
414                if( s )
415                {
416                        new->next = g_new0( struct jabber_buddy, 1 );
417                        new->next->bare_jid = new->bare_jid;
418                        new = new->next;
419                }
420        }
421       
422        if( s )
423        {
424                *s = '/';
425                new->full_jid = full_jid;
426                new->resource = strchr( new->full_jid, '/' ) + 1;
427        }
428        else
429        {
430                /* Let's waste some more bytes of RAM instead of to make
431                   memory management a total disaster here. And it saves
432                   me one g_free() call in this function. :-P */
433                new->full_jid = full_jid;
434        }
435       
436        return new;
437}
438
439/* Finds a buddy from our structures. Can find both full- and bare JIDs. When
440   asked for a bare JID, it uses the "resource_select" setting to see which
441   resource to pick. */
442struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags )
443{
444        struct jabber_data *jd = ic->proto_data;
445        struct jabber_buddy *bud, *head;
446        char *s, *jid;
447       
448        jid = jabber_normalize( jid_ );
449       
450        if( ( s = strchr( jid, '/' ) ) )
451        {
452                int bare_exists = 0;
453               
454                *s = 0;
455                if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) )
456                {
457                        bare_exists = 1;
458                       
459                        if( bud->next )
460                                bud = bud->next;
461                       
462                        /* Just return the first one for this bare JID. */
463                        if( flags & GET_BUDDY_FIRST )
464                        {
465                                *s = '/';
466                                g_free( jid );
467                                return bud;
468                        }
469                       
470                        /* Is this one of those no-resource buddies? */
471                        if( bud->resource == NULL )
472                        {
473                                *s = '/';
474                                g_free( jid );
475                                return NULL;
476                        }
477                       
478                        /* See if there's an exact match. */
479                        for( ; bud; bud = bud->next )
480                                if( strcmp( bud->resource, s + 1 ) == 0 )
481                                        break;
482                }
483               
484                if( bud == NULL && ( flags & GET_BUDDY_CREAT ) &&
485                    ( bare_exists || bee_user_by_handle( ic->bee, ic, jid ) ) )
486                {
487                        *s = '/';
488                        bud = jabber_buddy_add( ic, jid );
489                }
490               
491                g_free( jid );
492                return bud;
493        }
494        else
495        {
496                struct jabber_buddy *best_prio, *best_time;
497                char *set;
498               
499                head = g_hash_table_lookup( jd->buddies, jid );
500                bud = ( head && head->next ) ? head->next : head;
501               
502                g_free( jid );
503               
504                if( bud == NULL )
505                        /* No match. Create it now? */
506                        return ( ( flags & GET_BUDDY_CREAT ) &&
507                                 bee_user_by_handle( ic->bee, ic, jid_ ) ) ?
508                                   jabber_buddy_add( ic, jid_ ) : NULL;
509                else if( bud->resource && ( flags & GET_BUDDY_EXACT ) )
510                        /* We want an exact match, so in thise case there shouldn't be a /resource. */
511                        return NULL;
512                else if( bud->resource == NULL || bud->next == NULL )
513                        /* No need for selection if there's only one option. */
514                        return bud;
515                else if( flags & GET_BUDDY_FIRST )
516                        /* Looks like the caller doesn't care about details. */
517                        return bud;
518                else if( flags & GET_BUDDY_BARE )
519                        return head;
520               
521                best_prio = best_time = bud;
522                for( ; bud; bud = bud->next )
523                {
524                        if( bud->priority > best_prio->priority )
525                                best_prio = bud;
526                        if( bud->last_msg > best_time->last_msg )
527                                best_time = bud;
528                }
529               
530                if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL )
531                        return NULL;
532                else if( strcmp( set, "priority" ) == 0 )
533                        return best_prio;
534                else if( flags & GET_BUDDY_BARE_OK ) /* && strcmp( set, "activity" ) == 0 */
535                {
536                        if( best_time->last_msg + set_getint( &ic->acc->set, "activity_timeout" ) >= time( NULL ) )
537                                return best_time;
538                        else
539                                return head;
540                }
541                else
542                        return best_time;
543        }
544}
545
546/* I'm keeping a separate ext_jid attribute to save a JID that makes sense
547   to export to BitlBee. This is mainly for groupchats right now. It's
548   a bit of a hack, but I just think having the user nickname in the hostname
549   part of the hostmask doesn't look nice on IRC. Normally you can convert
550   a normal JID to ext_jid by swapping the part before and after the / and
551   replacing the / with a =. But there should be some stripping (@s are
552   allowed in Jabber nicks...). */
553struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags )
554{
555        struct jabber_buddy *bud;
556        char *s, *jid;
557       
558        jid = jabber_normalize( jid_ );
559       
560        if( ( s = strchr( jid, '=' ) ) == NULL )
561                return NULL;
562       
563        for( bud = jabber_buddy_by_jid( ic, s + 1, GET_BUDDY_FIRST ); bud; bud = bud->next )
564        {
565                /* Hmmm, could happen if not all people in the chat are anonymized? */
566                if( bud->ext_jid == NULL )
567                        continue;
568               
569                if( strcmp( bud->ext_jid, jid ) == 0 )
570                        break;
571        }
572       
573        g_free( jid );
574       
575        return bud;
576}
577
578/* Remove one specific full JID from our list. Use this when a buddy goes
579   off-line (because (s)he can still be online from a different location.
580   XXX: See above, we should accept bare JIDs too... */
581int jabber_buddy_remove( struct im_connection *ic, char *full_jid_ )
582{
583        struct jabber_data *jd = ic->proto_data;
584        struct jabber_buddy *bud, *prev = NULL, *bi;
585        char *s, *full_jid;
586       
587        full_jid = jabber_normalize( full_jid_ );
588       
589        if( ( s = strchr( full_jid, '/' ) ) )
590                *s = 0;
591       
592        if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
593        {
594                if( bud->next )
595                        bud = (prev=bud)->next;
596               
597                /* If there's only one item in the list (and if the resource
598                   matches), removing it is simple. (And the hash reference
599                   should be removed too!) */
600                if( bud->next == NULL &&
601                    ( ( s == NULL && bud->resource == NULL ) ||
602                      ( bud->resource && s && strcmp( bud->resource, s + 1 ) == 0 ) ) )
603                {
604                        int st = jabber_buddy_remove_bare( ic, full_jid );
605                        g_free( full_jid );
606                        return st;
607                }
608                else if( s == NULL || bud->resource == NULL )
609                {
610                        /* Tried to remove a bare JID while this JID does seem
611                           to have resources... (Or the opposite.) *sigh* */
612                        g_free( full_jid );
613                        return 0;
614                }
615                else
616                {
617                        for( bi = bud; bi; bi = (prev=bi)->next )
618                                if( strcmp( bi->resource, s + 1 ) == 0 )
619                                        break;
620                       
621                        g_free( full_jid );
622                       
623                        if( bi )
624                        {
625                                if( prev )
626                                        prev->next = bi->next;
627                                else
628                                        /* Don't think this should ever happen anymore. */
629                                        g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next );
630                               
631                                g_free( bi->ext_jid );
632                                g_free( bi->full_jid );
633                                g_free( bi->away_message );
634                                g_free( bi );
635                               
636                                return 1;
637                        }
638                        else
639                        {
640                                return 0;
641                        }
642                }
643        }
644        else
645        {
646                g_free( full_jid );
647                return 0;
648        }
649}
650
651/* Remove a buddy completely; removes all resources that belong to the
652   specified bare JID. Use this when removing someone from the contact
653   list, for example. */
654int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid )
655{
656        struct jabber_data *jd = ic->proto_data;
657        struct jabber_buddy *bud, *next;
658       
659        if( strchr( bare_jid, '/' ) )
660                return 0;
661       
662        if( ( bud = jabber_buddy_by_jid( ic, bare_jid, GET_BUDDY_FIRST ) ) )
663        {
664                /* Most important: Remove the hash reference. We don't know
665                   this buddy anymore. */
666                g_hash_table_remove( jd->buddies, bud->bare_jid );
667                g_free( bud->bare_jid );
668               
669                /* Deallocate the linked list of resources. */
670                while( bud )
671                {
672                        /* ext_jid && anonymous means that this buddy is
673                           specific to one groupchat (the one we're
674                           currently cleaning up) so it can be deleted
675                           completely. */
676                        if( bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS )
677                                imcb_remove_buddy( ic, bud->ext_jid, NULL );
678                       
679                        next = bud->next;
680                        g_free( bud->ext_jid );
681                        g_free( bud->full_jid );
682                        g_free( bud->away_message );
683                        g_free( bud );
684                        bud = next;
685                }
686               
687                return 1;
688        }
689        else
690        {
691                return 0;
692        }
693}
694
695static gboolean jabber_buddy_remove_all_cb( gpointer key, gpointer value, gpointer data )
696{
697        struct jabber_buddy *bud, *next;
698       
699        bud = value;
700        if( bud->bare_jid != bud->full_jid )
701                g_free( bud->bare_jid );
702        while( bud )
703        {
704                next = bud->next;
705                g_free( bud->ext_jid );
706                g_free( bud->full_jid );
707                g_free( bud->away_message );
708                g_free( bud );
709                bud = next;
710        }
711       
712        return TRUE;
713}
714
715void jabber_buddy_remove_all( struct im_connection *ic )
716{
717        struct jabber_data *jd = ic->proto_data;
718       
719        g_hash_table_foreach_remove( jd->buddies, jabber_buddy_remove_all_cb, NULL );
720        g_hash_table_destroy( jd->buddies );
721}
722
723time_t jabber_get_timestamp( struct xt_node *xt )
724{
725        struct xt_node *c;
726        char *s = NULL;
727        struct tm tp;
728       
729        for( c = xt->children; ( c = xt_find_node( c, "x" ) ); c = c->next )
730        {
731                if( ( s = xt_find_attr( c, "xmlns" ) ) && strcmp( s, XMLNS_DELAY ) == 0 )
732                        break;
733        }
734       
735        if( !c || !( s = xt_find_attr( c, "stamp" ) ) )
736                return 0;
737       
738        memset( &tp, 0, sizeof( tp ) );
739        if( sscanf( s, "%4d%2d%2dT%2d:%2d:%2d", &tp.tm_year, &tp.tm_mon, &tp.tm_mday,
740                                                &tp.tm_hour, &tp.tm_min, &tp.tm_sec ) != 6 )
741                return 0;
742       
743        tp.tm_year -= 1900;
744        tp.tm_mon --;
745       
746        return mktime_utc( &tp );
747}
748
749struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns )
750{
751        struct jabber_error *err;
752        struct xt_node *c;
753        char *s;
754       
755        if( node == NULL )
756                return NULL;
757       
758        err = g_new0( struct jabber_error, 1 );
759        err->type = xt_find_attr( node, "type" );
760       
761        for( c = node->children; c; c = c->next )
762        {
763                if( !( s = xt_find_attr( c, "xmlns" ) ) ||
764                    strcmp( s, xmlns ) != 0 )
765                        continue;
766               
767                if( strcmp( c->name, "text" ) != 0 )
768                {
769                        err->code = c->name;
770                }
771                /* Only use the text if it doesn't have an xml:lang attribute,
772                   if it's empty or if it's set to something English. */
773                else if( !( s = xt_find_attr( c, "xml:lang" ) ) ||
774                         !*s || strncmp( s, "en", 2 ) == 0 )
775                {
776                        err->text = c->text;
777                }
778        }
779       
780        return err;
781}
782
783void jabber_error_free( struct jabber_error *err )
784{
785        g_free( err );
786}
787
788gboolean jabber_set_me( struct im_connection *ic, const char *me )
789{
790        struct jabber_data *jd = ic->proto_data;
791       
792        if( strchr( me, '@' ) == NULL )
793                return FALSE;
794       
795        g_free( jd->username );
796        g_free( jd->me );
797       
798        jd->me = jabber_normalize( me );
799        jd->server = strchr( jd->me, '@' );
800        jd->username = g_strndup( jd->me, jd->server - jd->me );
801        jd->server ++;
802       
803        return TRUE;
804}
Note: See TracBrowser for help on using the repository browser.