Ignore:
Timestamp:
2015-02-20T22:50:54Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
0b9daac, 3d45471, 7733b8c
Parents:
af359b4
git-author:
Indent <please@…> (19-02-15 05:47:20)
git-committer:
dequis <dx@…> (20-02-15 22:50:54)
Message:

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/jabber/jabber_util.c

    raf359b4 r5ebff60  
    44*  Jabber module - Misc. stuff                                              *
    55*                                                                           *
    6 *  Copyright 2006-2010 Wilmer van der Gaast <wilmer@gaast.net>             
     6*  Copyright 2006-2010 Wilmer van der Gaast <wilmer@gaast.net>
    77*                                                                           *
    88*  This program is free software; you can redistribute it and/or modify     *
     
    2828static unsigned int next_id = 1;
    2929
    30 char *set_eval_priority( set_t *set, char *value )
     30char *set_eval_priority(set_t *set, char *value)
    3131{
    3232        account_t *acc = set->data;
    3333        int i;
    34        
    35         if( sscanf( value, "%d", &i ) == 1 )
    36         {
     34
     35        if (sscanf(value, "%d", &i) == 1) {
    3736                /* Priority is a signed 8-bit integer, according to RFC 3921. */
    38                 if( i < -128 || i > 127 )
     37                if (i < -128 || i > 127) {
    3938                        return SET_INVALID;
    40         }
    41         else
     39                }
     40        } else {
    4241                return SET_INVALID;
    43        
     42        }
     43
    4444        /* Only run this stuff if the account is online ATM,
    4545           and if the setting seems to be acceptable. */
    46         if( acc->ic )
    47         {
     46        if (acc->ic) {
    4847                /* Although set_eval functions usually are very nice and
    4948                   convenient, they have one disadvantage: If I would just
     
    5150                   send the old setting because the set->value gets changed
    5251                   after the (this) eval returns a non-NULL value.
    53                    
     52
    5453                   So now I can choose between implementing post-set
    5554                   functions next to evals, or just do this little hack: */
    56                
    57                 g_free( set->value );
    58                 set->value = g_strdup( value );
    59                
     55
     56                g_free(set->value);
     57                set->value = g_strdup(value);
     58
    6059                /* (Yes, sorry, I prefer the hack. :-P) */
    61                
    62                 presence_send_update( acc->ic );
    63         }
    64        
     60
     61                presence_send_update(acc->ic);
     62        }
     63
    6564        return value;
    6665}
    6766
    68 char *set_eval_tls( set_t *set, char *value )
    69 {
    70         if( g_strcasecmp( value, "try" ) == 0 )
     67char *set_eval_tls(set_t *set, char *value)
     68{
     69        if (g_strcasecmp(value, "try") == 0) {
    7170                return value;
    72         else
    73                 return set_eval_bool( set, value );
    74 }
    75 
    76 struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children )
     71        } else {
     72                return set_eval_bool(set, value);
     73        }
     74}
     75
     76struct xt_node *jabber_make_packet(char *name, char *type, char *to, struct xt_node *children)
    7777{
    7878        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        
     79
     80        node = xt_new_node(name, NULL, children);
     81
     82        if (type) {
     83                xt_add_attr(node, "type", type);
     84        }
     85        if (to) {
     86                xt_add_attr(node, "to", to);
     87        }
     88
    8789        /* IQ packets should always have an ID, so let's generate one. It
    8890           might get overwritten by jabber_cache_add() if this packet has
    8991           to be saved until we receive a response. Cached packets get
    9092           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        
     93        if (strcmp(name, "iq") == 0) {
     94                char *id = g_strdup_printf("%s%05x", JABBER_PACKET_ID, (next_id++) & 0xfffff);
     95                xt_add_attr(node, "id", id);
     96                g_free(id);
     97        }
     98
    9899        return node;
    99100}
    100101
    101 struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code )
     102struct xt_node *jabber_make_error_packet(struct xt_node *orig, char *err_cond, char *err_type, char *err_code)
    102103{
    103104        struct xt_node *node, *c;
    104105        char *to;
    105        
     106
    106107        /* Create the "defined-condition" tag. */
    107         c = xt_new_node( err_cond, NULL, NULL );
    108         xt_add_attr( c, "xmlns", XMLNS_STANZA_ERROR );
    109        
     108        c = xt_new_node(err_cond, NULL, NULL);
     109        xt_add_attr(c, "xmlns", XMLNS_STANZA_ERROR);
     110
    110111        /* Put it in an <error> tag. */
    111         c = xt_new_node( "error", NULL, c );
    112         xt_add_attr( c, "type", err_type );
    113        
     112        c = xt_new_node("error", NULL, c);
     113        xt_add_attr(c, "type", err_type);
     114
    114115        /* Add the error code, if present */
    115         if (err_code)
    116                 xt_add_attr( c, "code", err_code );
    117        
     116        if (err_code) {
     117                xt_add_attr(c, "code", err_code);
     118        }
     119
    118120        /* To make the actual error packet, we copy the original packet and
    119121           add our <error>/type="error" tag. Including the original packet
    120122           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        
     123        node = xt_dup(orig);
     124        xt_add_child(node, c);
     125        xt_add_attr(node, "type", "error");
     126
    125127        /* 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                
     128        if ((to = xt_find_attr(node, "from"))) {
     129                xt_add_attr(node, "to", to);
     130                xt_remove_attr(node, "from");
     131        }
     132
    132133        return node;
    133134}
     
    136137   them when you receive the response. Use this BEFORE sending the packet so
    137138   it'll get a new id= tag, and do NOT free() the packet after sending it! */
    138 void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func )
     139void jabber_cache_add(struct im_connection *ic, struct xt_node *node, jabber_cache_event func)
    139140{
    140141        struct jabber_data *jd = ic->proto_data;
    141         struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 );
     142        struct jabber_cache_entry *entry = g_new0(struct jabber_cache_entry, 1);
    142143        md5_state_t id_hash;
    143144        md5_byte_t id_sum[16];
    144145        char *id, *asc_hash;
    145        
    146         next_id ++;
    147        
     146
     147        next_id++;
     148
    148149        id_hash = jd->cached_id_prefix;
    149         md5_append( &id_hash, (md5_byte_t*) &next_id, sizeof( next_id ) );
    150         md5_digest_keep( &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        
     150        md5_append(&id_hash, (md5_byte_t *) &next_id, sizeof(next_id));
     151        md5_digest_keep(&id_hash, id_sum);
     152        asc_hash = base64_encode(id_sum, 12);
     153
     154        id = g_strdup_printf("%s%s", JABBER_CACHED_ID, asc_hash);
     155        xt_add_attr(node, "id", id);
     156        g_free(id);
     157        g_free(asc_hash);
     158
    158159        entry->node = node;
    159160        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 
    164 void jabber_cache_entry_free( gpointer data )
     161        entry->saved_at = time(NULL);
     162        g_hash_table_insert(jd->node_cache, xt_find_attr(node, "id"), entry);
     163}
     164
     165void jabber_cache_entry_free(gpointer data)
    165166{
    166167        struct jabber_cache_entry *entry = data;
    167        
    168         xt_free_node( entry->node );
    169         g_free( entry );
    170 }
    171 
    172 gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer );
     168
     169        xt_free_node(entry->node);
     170        g_free(entry);
     171}
     172
     173gboolean jabber_cache_clean_entry(gpointer key, gpointer entry, gpointer nullpointer);
    173174
    174175/* This one should be called from time to time (from keepalive, in this case)
     
    177178   node should be available in the cache for at least a minute (assuming the
    178179   function is indeed called every minute). */
    179 void jabber_cache_clean( struct im_connection *ic )
     180void jabber_cache_clean(struct im_connection *ic)
    180181{
    181182        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 
    187 gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer threshold_ )
     183        time_t threshold = time(NULL) - JABBER_CACHE_MAX_AGE;
     184
     185        g_hash_table_foreach_remove(jd->node_cache, jabber_cache_clean_entry, &threshold);
     186}
     187
     188gboolean jabber_cache_clean_entry(gpointer key, gpointer entry_, gpointer threshold_)
    188189{
    189190        struct jabber_cache_entry *entry = entry_;
    190191        time_t *threshold = threshold_;
    191        
     192
    192193        return entry->saved_at < *threshold;
    193194}
    194195
    195 xt_status jabber_cache_handle_packet( struct im_connection *ic, struct xt_node *node )
     196xt_status jabber_cache_handle_packet(struct im_connection *ic, struct xt_node *node)
    196197{
    197198        struct jabber_data *jd = ic->proto_data;
    198199        struct jabber_cache_entry *entry;
    199200        char *s;
    200        
    201         if( ( s = xt_find_attr( node, "id" ) ) == NULL ||
    202             strncmp( s, JABBER_CACHED_ID, strlen( JABBER_CACHED_ID ) ) != 0 )
    203         {
     201
     202        if ((s = xt_find_attr(node, "id")) == NULL ||
     203            strncmp(s, JABBER_CACHED_ID, strlen(JABBER_CACHED_ID)) != 0) {
    204204                /* Silently ignore it, without an ID (or a non-cache
    205205                   ID) we don't know how to handle the packet and we
     
    207207                return XT_HANDLED;
    208208        }
    209        
    210         entry = g_hash_table_lookup( jd->node_cache, s );
    211        
    212         if( entry == NULL )
    213         {
     209
     210        entry = g_hash_table_lookup(jd->node_cache, s);
     211
     212        if (entry == NULL) {
    214213                /*
    215214                There's no longer an easy way to see if we generated this
    216215                one or someone else, and there's a ten-minute timeout anyway,
    217216                so meh.
    218                
     217
    219218                imcb_log( ic, "Warning: Received %s-%s packet with unknown/expired ID %s!",
    220219                              node->name, xt_find_attr( node, "type" ) ? : "(no type)", s );
    221220                */
    222         }
    223         else if( entry->func )
    224         {
    225                 return entry->func( ic, node, entry->node );
    226         }
    227        
     221        } else if (entry->func) {
     222                return entry->func(ic, node, entry->node);
     223        }
     224
    228225        return XT_HANDLED;
    229226}
     
    238235};
    239236
    240 const struct jabber_away_state *jabber_away_state_by_code( char *code )
     237const struct jabber_away_state *jabber_away_state_by_code(char *code)
    241238{
    242239        int i;
    243        
    244         if( code == NULL )
     240
     241        if (code == NULL) {
    245242                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 )
     243        }
     244
     245        for (i = 0; jabber_away_state_list[i].full_name; i++) {
     246                if (g_strcasecmp(jabber_away_state_list[i].code, code) == 0) {
    249247                        return jabber_away_state_list + i;
    250        
     248                }
     249        }
     250
    251251        return NULL;
    252252}
    253253
    254 const struct jabber_away_state *jabber_away_state_by_name( char *name )
     254const struct jabber_away_state *jabber_away_state_by_name(char *name)
    255255{
    256256        int i;
    257        
    258         if( name == NULL )
     257
     258        if (name == NULL) {
    259259                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 )
     260        }
     261
     262        for (i = 0; jabber_away_state_list[i].full_name; i++) {
     263                if (g_strcasecmp(jabber_away_state_list[i].full_name, name) == 0) {
    263264                        return jabber_away_state_list + i;
    264        
     265                }
     266        }
     267
    265268        return NULL;
    266269}
    267270
    268 struct jabber_buddy_ask_data
    269 {
     271struct jabber_buddy_ask_data {
    270272        struct im_connection *ic;
    271273        char *handle;
     
    273275};
    274276
    275 static void jabber_buddy_ask_yes( void *data )
     277static void jabber_buddy_ask_yes(void *data)
    276278{
    277279        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 
    287 static void jabber_buddy_ask_no( void *data )
     280
     281        presence_send_request(bla->ic, bla->handle, "subscribed");
     282
     283        imcb_ask_add(bla->ic, bla->handle, NULL);
     284
     285        g_free(bla->handle);
     286        g_free(bla);
     287}
     288
     289static void jabber_buddy_ask_no(void *data)
    288290{
    289291        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 
    297 void 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 );
     292
     293        presence_send_request(bla->ic, bla->handle, "unsubscribed");
     294
     295        g_free(bla->handle);
     296        g_free(bla);
     297}
     298
     299void jabber_buddy_ask(struct im_connection *ic, char *handle)
     300{
     301        struct jabber_buddy_ask_data *bla = g_new0(struct jabber_buddy_ask_data, 1);
    300302        char *buf;
    301        
     303
    302304        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 );
     305        bla->handle = g_strdup(handle);
     306
     307        buf = g_strdup_printf("The user %s wants to add you to his/her buddy list.", handle);
     308        imcb_ask(ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no);
     309        g_free(buf);
    308310}
    309311
    310312/* Compares just the bare portions of two Jabber IDs. */
    311 int jabber_compare_jid( const char *jid1, const char *jid2 )
     313int jabber_compare_jid(const char *jid1, const char *jid2)
    312314{
    313315        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] == '/' ) )
     316
     317        for (i = 0;; i++) {
     318                if (jid1[i] == '\0' || jid1[i] == '/' || jid2[i] == '\0' || jid2[i] == '/') {
     319                        if ((jid1[i] == '\0' || jid1[i] == '/') && (jid2[i] == '\0' || jid2[i] == '/')) {
    320320                                break;
     321                        }
    321322                        return FALSE;
    322323                }
    323                 if( g_ascii_tolower( jid1[i] ) != g_ascii_tolower( jid2[i] ) )
    324                 {
     324                if (g_ascii_tolower(jid1[i]) != g_ascii_tolower(jid2[i])) {
    325325                        return FALSE;
    326326                }
    327327        }
    328        
     328
    329329        return TRUE;
    330330}
     
    332332/* The /resource part is case sensitive. This stops once we see a slash.
    333333   Returns a new string. Don't leak it! */
    334 char *jabber_normalize( const char *orig )
     334char *jabber_normalize(const char *orig)
    335335{
    336336        char *lower, *new, *s;
    337337
    338         if ( ! ( s = strchr( orig, '/' ) ) )
    339                 return g_utf8_strdown( orig, -1 );
    340 
    341         lower = g_utf8_strdown( orig, (s - orig) );  /* stop in s */
    342         new = g_strconcat( lower, s, NULL );
    343         g_free( lower );
     338        if (!(s = strchr(orig, '/'))) {
     339                return g_utf8_strdown(orig, -1);
     340        }
     341
     342        lower = g_utf8_strdown(orig, (s - orig));    /* stop in s */
     343        new = g_strconcat(lower, s, NULL);
     344        g_free(lower);
    344345        return new;
    345346}
     
    347348/* Similar to jabber_normalize, but works with addresses in the form
    348349 * resource=chatroom@example.com */
    349 char *jabber_normalize_ext( const char *orig )
     350char *jabber_normalize_ext(const char *orig)
    350351{
    351352        char *lower, *new, *s;
    352353
    353         if ( ! ( s = strchr( orig, '=' ) ) )
    354                 return g_utf8_strdown( orig, -1 );
    355 
    356         lower = g_utf8_strdown( s, -1 ); /* start in s */
     354        if (!(s = strchr(orig, '='))) {
     355                return g_utf8_strdown(orig, -1);
     356        }
     357
     358        lower = g_utf8_strdown(s, -1);   /* start in s */
    357359
    358360        *s = 0;
    359         new = g_strconcat( orig, lower, NULL );
     361        new = g_strconcat(orig, lower, NULL);
    360362        *s = '=';
    361363
    362         g_free( lower );
     364        g_free(lower);
    363365        return new;
    364366}
     
    369371   to deal with that properly. Set their ->resource property to NULL. Do *NOT*
    370372   allow to mix this stuff, though... */
    371 struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid_ )
     373struct jabber_buddy *jabber_buddy_add(struct im_connection *ic, char *full_jid_)
    372374{
    373375        struct jabber_data *jd = ic->proto_data;
    374376        struct jabber_buddy *bud, *new, *bi;
    375377        char *s, *full_jid;
    376        
    377         full_jid = jabber_normalize( full_jid_ );
    378        
    379         if( ( s = strchr( full_jid, '/' ) ) )
     378
     379        full_jid = jabber_normalize(full_jid_);
     380
     381        if ((s = strchr(full_jid, '/'))) {
    380382                *s = 0;
    381        
    382         new = g_new0( struct jabber_buddy, 1 );
    383        
    384         if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
    385         {
     383        }
     384
     385        new = g_new0(struct jabber_buddy, 1);
     386
     387        if ((bud = g_hash_table_lookup(jd->buddies, full_jid))) {
    386388                /* The first entry is always a bare JID. If there are more, we
    387389                   should ignore the first one here. */
    388                 if( bud->next )
     390                if (bud->next) {
    389391                        bud = bud->next;
    390                
     392                }
     393
    391394                /* If this is a transport buddy or whatever, it can't have more
    392395                   than one instance, so this is always wrong: */
    393                 if( s == NULL || bud->resource == NULL )
    394                 {
    395                         if( s ) *s = '/';
    396                         g_free( new );
    397                         g_free( full_jid );
     396                if (s == NULL || bud->resource == NULL) {
     397                        if (s) {
     398                                *s = '/';
     399                        }
     400                        g_free(new);
     401                        g_free(full_jid);
    398402                        return NULL;
    399403                }
    400                
     404
    401405                new->bare_jid = bud->bare_jid;
    402                
     406
    403407                /* We already have another resource for this buddy, add the
    404408                   new one to the list. */
    405                 for( bi = bud; bi; bi = bi->next )
    406                 {
     409                for (bi = bud; bi; bi = bi->next) {
    407410                        /* Check for dupes. */
    408                         if( strcmp( bi->resource, s + 1 ) == 0 )
    409                         {
     411                        if (strcmp(bi->resource, s + 1) == 0) {
    410412                                *s = '/';
    411                                 g_free( new );
    412                                 g_free( full_jid );
     413                                g_free(new);
     414                                g_free(full_jid);
    413415                                return NULL;
    414416                        }
    415417                        /* Append the new item to the list. */
    416                         else if( bi->next == NULL )
    417                         {
     418                        else if (bi->next == NULL) {
    418419                                bi->next = new;
    419420                                break;
    420421                        }
    421422                }
    422         }
    423         else
    424         {
    425                 new->full_jid = new->bare_jid = g_strdup( full_jid );
    426                 g_hash_table_insert( jd->buddies, new->bare_jid, new );
    427                
    428                 if( s )
    429                 {
    430                         new->next = g_new0( struct jabber_buddy, 1 );
     423        } else {
     424                new->full_jid = new->bare_jid = g_strdup(full_jid);
     425                g_hash_table_insert(jd->buddies, new->bare_jid, new);
     426
     427                if (s) {
     428                        new->next = g_new0(struct jabber_buddy, 1);
    431429                        new->next->bare_jid = new->bare_jid;
    432430                        new = new->next;
    433431                }
    434432        }
    435        
    436         if( s )
    437         {
     433
     434        if (s) {
    438435                *s = '/';
    439436                new->full_jid = full_jid;
    440                 new->resource = strchr( new->full_jid, '/' ) + 1;
    441         }
    442         else
    443         {
     437                new->resource = strchr(new->full_jid, '/') + 1;
     438        } else {
    444439                /* Let's waste some more bytes of RAM instead of to make
    445440                   memory management a total disaster here. And it saves
     
    447442                new->full_jid = full_jid;
    448443        }
    449        
     444
    450445        return new;
    451446}
     
    454449   asked for a bare JID, it uses the "resource_select" setting to see which
    455450   resource to pick. */
    456 struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags )
     451struct jabber_buddy *jabber_buddy_by_jid(struct im_connection *ic, char *jid_, get_buddy_flags_t flags)
    457452{
    458453        struct jabber_data *jd = ic->proto_data;
    459454        struct jabber_buddy *bud, *head;
    460455        char *s, *jid;
    461        
    462         jid = jabber_normalize( jid_ );
    463        
    464         if( ( s = strchr( jid, '/' ) ) )
    465         {
     456
     457        jid = jabber_normalize(jid_);
     458
     459        if ((s = strchr(jid, '/'))) {
    466460                int bare_exists = 0;
    467                
     461
    468462                *s = 0;
    469                 if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) )
    470                 {
     463                if ((bud = g_hash_table_lookup(jd->buddies, jid))) {
    471464                        bare_exists = 1;
    472                        
    473                         if( bud->next )
     465
     466                        if (bud->next) {
    474467                                bud = bud->next;
    475                        
     468                        }
     469
    476470                        /* Just return the first one for this bare JID. */
    477                         if( flags & GET_BUDDY_FIRST )
    478                         {
     471                        if (flags & GET_BUDDY_FIRST) {
    479472                                *s = '/';
    480                                 g_free( jid );
     473                                g_free(jid);
    481474                                return bud;
    482475                        }
    483                        
     476
    484477                        /* Is this one of those no-resource buddies? */
    485                         if( bud->resource == NULL )
    486                         {
     478                        if (bud->resource == NULL) {
    487479                                *s = '/';
    488                                 g_free( jid );
     480                                g_free(jid);
    489481                                return NULL;
    490482                        }
    491                        
     483
    492484                        /* See if there's an exact match. */
    493                         for( ; bud; bud = bud->next )
    494                                 if( strcmp( bud->resource, s + 1 ) == 0 )
     485                        for (; bud; bud = bud->next) {
     486                                if (strcmp(bud->resource, s + 1) == 0) {
    495487                                        break;
    496                 }
    497                
    498                 if( bud == NULL && ( flags & GET_BUDDY_CREAT ) &&
    499                     ( bare_exists || bee_user_by_handle( ic->bee, ic, jid ) ) )
    500                 {
     488                                }
     489                        }
     490                }
     491
     492                if (bud == NULL && (flags & GET_BUDDY_CREAT) &&
     493                    (bare_exists || bee_user_by_handle(ic->bee, ic, jid))) {
    501494                        *s = '/';
    502                         bud = jabber_buddy_add( ic, jid );
    503                 }
    504                
    505                 g_free( jid );
     495                        bud = jabber_buddy_add(ic, jid);
     496                }
     497
     498                g_free(jid);
    506499                return bud;
    507         }
    508         else
    509         {
     500        } else {
    510501                struct jabber_buddy *best_prio, *best_time;
    511502                char *set;
    512                
    513                 head = g_hash_table_lookup( jd->buddies, jid );
    514                 bud = ( head && head->next ) ? head->next : head;
    515                
    516                 g_free( jid );
    517                
    518                 if( bud == NULL )
     503
     504                head = g_hash_table_lookup(jd->buddies, jid);
     505                bud = (head && head->next) ? head->next : head;
     506
     507                g_free(jid);
     508
     509                if (bud == NULL) {
    519510                        /* No match. Create it now? */
    520                         return ( ( flags & GET_BUDDY_CREAT ) &&
    521                                  bee_user_by_handle( ic->bee, ic, jid_ ) ) ?
    522                                    jabber_buddy_add( ic, jid_ ) : NULL;
    523                 else if( bud->resource && ( flags & GET_BUDDY_EXACT ) )
     511                        return ((flags & GET_BUDDY_CREAT) &&
     512                                bee_user_by_handle(ic->bee, ic, jid_)) ?
     513                               jabber_buddy_add(ic, jid_) : NULL;
     514                } else if (bud->resource && (flags & GET_BUDDY_EXACT)) {
    524515                        /* We want an exact match, so in thise case there shouldn't be a /resource. */
    525516                        return NULL;
    526                 else if( bud->resource == NULL || bud->next == NULL )
     517                } else if (bud->resource == NULL || bud->next == NULL) {
    527518                        /* No need for selection if there's only one option. */
    528519                        return bud;
    529                 else if( flags & GET_BUDDY_FIRST )
     520                } else if (flags & GET_BUDDY_FIRST) {
    530521                        /* Looks like the caller doesn't care about details. */
    531522                        return bud;
    532                 else if( flags & GET_BUDDY_BARE )
     523                } else if (flags & GET_BUDDY_BARE) {
    533524                        return head;
    534                
     525                }
     526
    535527                best_prio = best_time = bud;
    536                 for( ; bud; bud = bud->next )
    537                 {
    538                         if( bud->priority > best_prio->priority )
     528                for (; bud; bud = bud->next) {
     529                        if (bud->priority > best_prio->priority) {
    539530                                best_prio = bud;
    540                         if( bud->last_msg > best_time->last_msg )
     531                        }
     532                        if (bud->last_msg > best_time->last_msg) {
    541533                                best_time = bud;
    542                 }
    543                
    544                 if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL )
     534                        }
     535                }
     536
     537                if ((set = set_getstr(&ic->acc->set, "resource_select")) == NULL) {
    545538                        return NULL;
    546                 else if( strcmp( set, "priority" ) == 0 )
     539                } else if (strcmp(set, "priority") == 0) {
    547540                        return best_prio;
    548                 else if( flags & GET_BUDDY_BARE_OK ) /* && strcmp( set, "activity" ) == 0 */
    549                 {
    550                         if( best_time->last_msg + set_getint( &ic->acc->set, "activity_timeout" ) >= time( NULL ) )
     541                } else if (flags & GET_BUDDY_BARE_OK) { /* && strcmp( set, "activity" ) == 0 */
     542                        if (best_time->last_msg + set_getint(&ic->acc->set, "activity_timeout") >= time(NULL)) {
    551543                                return best_time;
    552                         else
     544                        } else {
    553545                                return head;
    554                 }
    555                 else
     546                        }
     547                } else {
    556548                        return best_time;
     549                }
    557550        }
    558551}
     
    565558   replacing the / with a =. But there should be some stripping (@s are
    566559   allowed in Jabber nicks...). */
    567 struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags )
     560struct jabber_buddy *jabber_buddy_by_ext_jid(struct im_connection *ic, char *jid_, get_buddy_flags_t flags)
    568561{
    569562        struct jabber_buddy *bud;
    570563        char *s, *jid;
    571        
    572         jid = jabber_normalize_ext( jid_ );
    573        
    574         if( ( s = strchr( jid, '=' ) ) == NULL )
     564
     565        jid = jabber_normalize_ext(jid_);
     566
     567        if ((s = strchr(jid, '=')) == NULL) {
    575568                return NULL;
    576        
    577         for( bud = jabber_buddy_by_jid( ic, s + 1, GET_BUDDY_FIRST ); bud; bud = bud->next )
    578         {
     569        }
     570
     571        for (bud = jabber_buddy_by_jid(ic, s + 1, GET_BUDDY_FIRST); bud; bud = bud->next) {
    579572                /* Hmmm, could happen if not all people in the chat are anonymized? */
    580                 if( bud->ext_jid == NULL )
     573                if (bud->ext_jid == NULL) {
    581574                        continue;
    582                
    583                 if( strcmp( bud->ext_jid, jid ) == 0 )
     575                }
     576
     577                if (strcmp(bud->ext_jid, jid) == 0) {
    584578                        break;
    585         }
    586        
    587         g_free( jid );
    588        
     579                }
     580        }
     581
     582        g_free(jid);
     583
    589584        return bud;
    590585}
     
    593588   off-line (because (s)he can still be online from a different location.
    594589   XXX: See above, we should accept bare JIDs too... */
    595 int jabber_buddy_remove( struct im_connection *ic, char *full_jid_ )
     590int jabber_buddy_remove(struct im_connection *ic, char *full_jid_)
    596591{
    597592        struct jabber_data *jd = ic->proto_data;
    598593        struct jabber_buddy *bud, *prev = NULL, *bi;
    599594        char *s, *full_jid;
    600        
    601         full_jid = jabber_normalize( full_jid_ );
    602        
    603         if( ( s = strchr( full_jid, '/' ) ) )
     595
     596        full_jid = jabber_normalize(full_jid_);
     597
     598        if ((s = strchr(full_jid, '/'))) {
    604599                *s = 0;
    605        
    606         if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
    607         {
    608                 if( bud->next )
    609                         bud = (prev=bud)->next;
    610                
     600        }
     601
     602        if ((bud = g_hash_table_lookup(jd->buddies, full_jid))) {
     603                if (bud->next) {
     604                        bud = (prev = bud)->next;
     605                }
     606
    611607                /* If there's only one item in the list (and if the resource
    612608                   matches), removing it is simple. (And the hash reference
    613609                   should be removed too!) */
    614                 if( bud->next == NULL &&
    615                     ( ( s == NULL && bud->resource == NULL ) ||
    616                       ( bud->resource && s && strcmp( bud->resource, s + 1 ) == 0 ) ) )
    617                 {
    618                         int st = jabber_buddy_remove_bare( ic, full_jid );
    619                         g_free( full_jid );
     610                if (bud->next == NULL &&
     611                    ((s == NULL && bud->resource == NULL) ||
     612                     (bud->resource && s && strcmp(bud->resource, s + 1) == 0))) {
     613                        int st = jabber_buddy_remove_bare(ic, full_jid);
     614                        g_free(full_jid);
    620615                        return st;
    621                 }
    622                 else if( s == NULL || bud->resource == NULL )
    623                 {
     616                } else if (s == NULL || bud->resource == NULL) {
    624617                        /* Tried to remove a bare JID while this JID does seem
    625618                           to have resources... (Or the opposite.) *sigh* */
    626                         g_free( full_jid );
     619                        g_free(full_jid);
    627620                        return 0;
    628                 }
    629                 else
    630                 {
    631                         for( bi = bud; bi; bi = (prev=bi)->next )
    632                                 if( strcmp( bi->resource, s + 1 ) == 0 )
     621                } else {
     622                        for (bi = bud; bi; bi = (prev = bi)->next) {
     623                                if (strcmp(bi->resource, s + 1) == 0) {
    633624                                        break;
    634                        
    635                         g_free( full_jid );
    636                        
    637                         if( bi )
    638                         {
    639                                 if( prev )
     625                                }
     626                        }
     627
     628                        g_free(full_jid);
     629
     630                        if (bi) {
     631                                if (prev) {
    640632                                        prev->next = bi->next;
    641                                 else
     633                                } else {
    642634                                        /* Don't think this should ever happen anymore. */
    643                                         g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next );
    644                                
    645                                 g_free( bi->ext_jid );
    646                                 g_free( bi->full_jid );
    647                                 g_free( bi->away_message );
    648                                 g_free( bi );
    649                                
     635                                        g_hash_table_replace(jd->buddies, bi->bare_jid, bi->next);
     636                                }
     637
     638                                g_free(bi->ext_jid);
     639                                g_free(bi->full_jid);
     640                                g_free(bi->away_message);
     641                                g_free(bi);
     642
    650643                                return 1;
    651                         }
    652                         else
    653                         {
     644                        } else {
    654645                                return 0;
    655646                        }
    656647                }
    657         }
    658         else
    659         {
    660                 g_free( full_jid );
     648        } else {
     649                g_free(full_jid);
    661650                return 0;
    662651        }
     
    666655   specified bare JID. Use this when removing someone from the contact
    667656   list, for example. */
    668 int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid )
     657int jabber_buddy_remove_bare(struct im_connection *ic, char *bare_jid)
    669658{
    670659        struct jabber_data *jd = ic->proto_data;
    671660        struct jabber_buddy *bud, *next;
    672        
    673         if( strchr( bare_jid, '/' ) )
     661
     662        if (strchr(bare_jid, '/')) {
    674663                return 0;
    675        
    676         if( ( bud = jabber_buddy_by_jid( ic, bare_jid, GET_BUDDY_FIRST ) ) )
    677         {
     664        }
     665
     666        if ((bud = jabber_buddy_by_jid(ic, bare_jid, GET_BUDDY_FIRST))) {
    678667                /* Most important: Remove the hash reference. We don't know
    679668                   this buddy anymore. */
    680                 g_hash_table_remove( jd->buddies, bud->bare_jid );
    681                 g_free( bud->bare_jid );
    682                
     669                g_hash_table_remove(jd->buddies, bud->bare_jid);
     670                g_free(bud->bare_jid);
     671
    683672                /* Deallocate the linked list of resources. */
    684                 while( bud )
    685                 {
     673                while (bud) {
    686674                        /* ext_jid && anonymous means that this buddy is
    687675                           specific to one groupchat (the one we're
    688676                           currently cleaning up) so it can be deleted
    689677                           completely. */
    690                         if( bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS )
    691                                 imcb_remove_buddy( ic, bud->ext_jid, NULL );
    692                        
     678                        if (bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS) {
     679                                imcb_remove_buddy(ic, bud->ext_jid, NULL);
     680                        }
     681
    693682                        next = bud->next;
    694                         g_free( bud->ext_jid );
    695                         g_free( bud->full_jid );
    696                         g_free( bud->away_message );
    697                         g_free( bud );
     683                        g_free(bud->ext_jid);
     684                        g_free(bud->full_jid);
     685                        g_free(bud->away_message);
     686                        g_free(bud);
    698687                        bud = next;
    699688                }
    700                
     689
    701690                return 1;
    702         }
    703         else
    704         {
     691        } else {
    705692                return 0;
    706693        }
    707694}
    708695
    709 static gboolean jabber_buddy_remove_all_cb( gpointer key, gpointer value, gpointer data )
     696static gboolean jabber_buddy_remove_all_cb(gpointer key, gpointer value, gpointer data)
    710697{
    711698        struct jabber_buddy *bud, *next;
    712        
     699
    713700        bud = value;
    714         if( bud->bare_jid != bud->full_jid )
    715                 g_free( bud->bare_jid );
    716         while( bud )
    717         {
     701        if (bud->bare_jid != bud->full_jid) {
     702                g_free(bud->bare_jid);
     703        }
     704        while (bud) {
    718705                next = bud->next;
    719                 g_free( bud->ext_jid );
    720                 g_free( bud->full_jid );
    721                 g_free( bud->away_message );
    722                 g_free( bud );
     706                g_free(bud->ext_jid);
     707                g_free(bud->full_jid);
     708                g_free(bud->away_message);
     709                g_free(bud);
    723710                bud = next;
    724711        }
    725        
     712
    726713        return TRUE;
    727714}
    728715
    729 void jabber_buddy_remove_all( struct im_connection *ic )
     716void jabber_buddy_remove_all(struct im_connection *ic)
    730717{
    731718        struct jabber_data *jd = ic->proto_data;
    732        
    733         g_hash_table_foreach_remove( jd->buddies, jabber_buddy_remove_all_cb, NULL );
    734         g_hash_table_destroy( jd->buddies );
    735 }
    736 
    737 time_t jabber_get_timestamp( struct xt_node *xt )
     719
     720        g_hash_table_foreach_remove(jd->buddies, jabber_buddy_remove_all_cb, NULL);
     721        g_hash_table_destroy(jd->buddies);
     722}
     723
     724time_t jabber_get_timestamp(struct xt_node *xt)
    738725{
    739726        struct xt_node *c;
     
    744731
    745732        /* XEP-0091 has <x> */
    746         c = xt_find_node_by_attr( xt->children, "x", "xmlns", XMLNS_DELAY_OLD );
    747 
    748         if( !c || !( s = xt_find_attr( c, "stamp" ) ) ) {
     733        c = xt_find_node_by_attr(xt->children, "x", "xmlns", XMLNS_DELAY_OLD);
     734
     735        if (!c || !(s = xt_find_attr(c, "stamp"))) {
    749736                is_old = FALSE;
    750737
    751738                /* XEP-0203 has <delay> */
    752                 c = xt_find_node_by_attr( xt->children, "delay", "xmlns", XMLNS_DELAY );
    753                 if( !c || !( s = xt_find_attr( c, "stamp" ) ) ) {
     739                c = xt_find_node_by_attr(xt->children, "delay", "xmlns", XMLNS_DELAY);
     740                if (!c || !(s = xt_find_attr(c, "stamp"))) {
    754741                        return 0;
    755742                }
    756743        }
    757        
    758         memset( &tp, 0, sizeof( tp ) );
     744
     745        memset(&tp, 0, sizeof(tp));
    759746
    760747        /* The other main difference between XEPs is the timestamp format */
    761748        format = (is_old) ? "%4d%2d%2dT%2d:%2d:%2d" : "%4d-%2d-%2dT%2d:%2d:%2dZ";
    762749
    763         if( sscanf( s, format, &tp.tm_year, &tp.tm_mon, &tp.tm_mday,
    764                                &tp.tm_hour, &tp.tm_min, &tp.tm_sec ) != 6 )
     750        if (sscanf(s, format, &tp.tm_year, &tp.tm_mon, &tp.tm_mday,
     751                   &tp.tm_hour, &tp.tm_min, &tp.tm_sec) != 6) {
    765752                return 0;
    766        
     753        }
     754
    767755        tp.tm_year -= 1900;
    768         tp.tm_mon --;
    769        
    770         return mktime_utc( &tp );
    771 }
    772 
    773 struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns )
     756        tp.tm_mon--;
     757
     758        return mktime_utc(&tp);
     759}
     760
     761struct jabber_error *jabber_error_parse(struct xt_node *node, char *xmlns)
    774762{
    775763        struct jabber_error *err;
    776764        struct xt_node *c;
    777765        char *s;
    778        
    779         if( node == NULL )
     766
     767        if (node == NULL) {
    780768                return NULL;
    781        
    782         err = g_new0( struct jabber_error, 1 );
    783         err->type = xt_find_attr( node, "type" );
    784        
    785         for( c = node->children; c; c = c->next )
    786         {
    787                 if( !( s = xt_find_attr( c, "xmlns" ) ) ||
    788                     strcmp( s, xmlns ) != 0 )
     769        }
     770
     771        err = g_new0(struct jabber_error, 1);
     772        err->type = xt_find_attr(node, "type");
     773
     774        for (c = node->children; c; c = c->next) {
     775                if (!(s = xt_find_attr(c, "xmlns")) ||
     776                    strcmp(s, xmlns) != 0) {
    789777                        continue;
    790                
    791                 if( strcmp( c->name, "text" ) != 0 )
    792                 {
     778                }
     779
     780                if (strcmp(c->name, "text") != 0) {
    793781                        err->code = c->name;
    794782                }
    795783                /* Only use the text if it doesn't have an xml:lang attribute,
    796784                   if it's empty or if it's set to something English. */
    797                 else if( !( s = xt_find_attr( c, "xml:lang" ) ) ||
    798                          !*s || strncmp( s, "en", 2 ) == 0 )
    799                 {
     785                else if (!(s = xt_find_attr(c, "xml:lang")) ||
     786                         !*s || strncmp(s, "en", 2) == 0) {
    800787                        err->text = c->text;
    801788                }
    802789        }
    803        
     790
    804791        return err;
    805792}
    806793
    807 void jabber_error_free( struct jabber_error *err )
    808 {
    809         g_free( err );
    810 }
    811 
    812 gboolean jabber_set_me( struct im_connection *ic, const char *me )
     794void jabber_error_free(struct jabber_error *err)
     795{
     796        g_free(err);
     797}
     798
     799gboolean jabber_set_me(struct im_connection *ic, const char *me)
    813800{
    814801        struct jabber_data *jd = ic->proto_data;
    815        
    816         if( strchr( me, '@' ) == NULL )
     802
     803        if (strchr(me, '@') == NULL) {
    817804                return FALSE;
    818        
    819         g_free( jd->username );
    820         g_free( jd->me );
    821        
    822         jd->me = jabber_normalize( me );
    823         jd->server = strchr( jd->me, '@' );
    824         jd->username = g_strndup( jd->me, jd->server - jd->me );
    825         jd->server ++;
     805        }
     806
     807        g_free(jd->username);
     808        g_free(jd->me);
     809
     810        jd->me = jabber_normalize(me);
     811        jd->server = strchr(jd->me, '@');
     812        jd->username = g_strndup(jd->me, jd->server - jd->me);
     813        jd->server++;
    826814
    827815        /* Set the "internal" account username, for groupchats */
    828         g_free( jd->internal_jid );
    829         jd->internal_jid = g_strdup( jd->me );
    830        
     816        g_free(jd->internal_jid);
     817        jd->internal_jid = g_strdup(jd->me);
     818
    831819        return TRUE;
    832820}
Note: See TracChangeset for help on using the changeset viewer.