[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] | 26 | static unsigned int next_id = 1; |
---|
[21167d2] | 27 | |
---|
[ebe7b36] | 28 | char *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 |
---|
| 50 | when the eval returns a non-NULL value. |
---|
| 51 | |
---|
| 52 | So now I can choose between implementing post-set |
---|
| 53 | functions next to evals, or just do this little hack: */ |
---|
| 54 | |
---|
| 55 | g_free( set->value ); |
---|
[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 | |
---|
| 66 | char *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 | |
---|
| 74 | struct 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 | |
---|
[259edd4] | 99 | struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type ) |
---|
| 100 | { |
---|
| 101 | struct xt_node *node, *c; |
---|
| 102 | char *to; |
---|
| 103 | |
---|
| 104 | /* Create the "defined-condition" tag. */ |
---|
| 105 | c = xt_new_node( err_cond, NULL, NULL ); |
---|
[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 | |
---|
| 112 | /* To make the actual error packet, we copy the original packet and |
---|
| 113 | add our <error>/type="error" tag. Including the original packet |
---|
| 114 | is recommended, so let's just do it. */ |
---|
| 115 | node = xt_dup( orig ); |
---|
| 116 | xt_add_child( node, c ); |
---|
| 117 | xt_add_attr( node, "type", "error" ); |
---|
| 118 | |
---|
| 119 | /* Return to sender. */ |
---|
| 120 | if( ( to = xt_find_attr( node, "from" ) ) ) |
---|
| 121 | { |
---|
| 122 | xt_add_attr( node, "to", to ); |
---|
| 123 | xt_remove_attr( node, "from" ); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return node; |
---|
| 127 | } |
---|
| 128 | |
---|
[dfa41a4] | 129 | /* Cache a node/packet for later use. Mainly useful for IQ packets if you need |
---|
[fe7a554] | 130 | them when you receive the response. Use this BEFORE sending the packet so |
---|
[dfa41a4] | 131 | it'll get a new id= tag, and do NOT free() the packet after writing it! */ |
---|
[0da65d5] | 132 | void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func ) |
---|
[fe7a554] | 133 | { |
---|
[0da65d5] | 134 | struct jabber_data *jd = ic->proto_data; |
---|
[dfa41a4] | 135 | char *id = g_strdup_printf( "%s%05x", JABBER_CACHED_ID, ( next_id++ ) & 0xfffff ); |
---|
[038d17f] | 136 | struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 ); |
---|
[fe7a554] | 137 | |
---|
| 138 | xt_add_attr( node, "id", id ); |
---|
[21167d2] | 139 | g_free( id ); |
---|
[038d17f] | 140 | |
---|
| 141 | entry->node = node; |
---|
[861c199] | 142 | entry->func = func; |
---|
[038d17f] | 143 | g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry ); |
---|
[fe7a554] | 144 | } |
---|
| 145 | |
---|
[038d17f] | 146 | void jabber_cache_entry_free( gpointer data ) |
---|
| 147 | { |
---|
| 148 | struct jabber_cache_entry *entry = data; |
---|
[21167d2] | 149 | |
---|
[038d17f] | 150 | xt_free_node( entry->node ); |
---|
| 151 | g_free( entry ); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer ); |
---|
| 155 | |
---|
[861c199] | 156 | /* This one should be called from time to time (from keepalive, in this case) |
---|
| 157 | to make sure things don't stay in the node cache forever. By marking nodes |
---|
| 158 | during the first run and deleting marked nodes during a next run, every |
---|
| 159 | node should be available in the cache for at least a minute (assuming the |
---|
| 160 | function is indeed called every minute). */ |
---|
[0da65d5] | 161 | void jabber_cache_clean( struct im_connection *ic ) |
---|
[038d17f] | 162 | { |
---|
[0da65d5] | 163 | struct jabber_data *jd = ic->proto_data; |
---|
[038d17f] | 164 | |
---|
| 165 | g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, NULL ); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer nullpointer ) |
---|
| 169 | { |
---|
| 170 | struct jabber_cache_entry *entry = entry_; |
---|
| 171 | struct xt_node *node = entry->node; |
---|
| 172 | |
---|
| 173 | if( node->flags & XT_SEEN ) |
---|
| 174 | return TRUE; |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | node->flags |= XT_SEEN; |
---|
| 178 | return FALSE; |
---|
| 179 | } |
---|
[21167d2] | 180 | } |
---|
[5e202b0] | 181 | |
---|
| 182 | const struct jabber_away_state jabber_away_state_list[] = |
---|
| 183 | { |
---|
| 184 | { "away", "Away" }, |
---|
| 185 | { "chat", "Free for Chat" }, |
---|
| 186 | { "dnd", "Do not Disturb" }, |
---|
| 187 | { "xa", "Extended Away" }, |
---|
| 188 | { "", "Online" }, |
---|
| 189 | { "", NULL } |
---|
| 190 | }; |
---|
| 191 | |
---|
| 192 | const struct jabber_away_state *jabber_away_state_by_code( char *code ) |
---|
| 193 | { |
---|
| 194 | int i; |
---|
| 195 | |
---|
| 196 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 197 | if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 ) |
---|
| 198 | return jabber_away_state_list + i; |
---|
| 199 | |
---|
| 200 | return NULL; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | const struct jabber_away_state *jabber_away_state_by_name( char *name ) |
---|
| 204 | { |
---|
| 205 | int i; |
---|
| 206 | |
---|
| 207 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 208 | if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 ) |
---|
| 209 | return jabber_away_state_list + i; |
---|
| 210 | |
---|
| 211 | return NULL; |
---|
| 212 | } |
---|
[8e5e2e9] | 213 | |
---|
| 214 | struct jabber_buddy_ask_data |
---|
| 215 | { |
---|
[0da65d5] | 216 | struct im_connection *ic; |
---|
[8e5e2e9] | 217 | char *handle; |
---|
| 218 | char *realname; |
---|
| 219 | }; |
---|
| 220 | |
---|
| 221 | static void jabber_buddy_ask_yes( gpointer w, struct jabber_buddy_ask_data *bla ) |
---|
| 222 | { |
---|
[0da65d5] | 223 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
[8e5e2e9] | 224 | |
---|
[f0cb961] | 225 | if( imcb_find_buddy( bla->ic, bla->handle ) == NULL ) |
---|
[84b045d] | 226 | imcb_ask_add( bla->ic, bla->handle, NULL ); |
---|
[8e5e2e9] | 227 | |
---|
| 228 | g_free( bla->handle ); |
---|
| 229 | g_free( bla ); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | static void jabber_buddy_ask_no( gpointer w, struct jabber_buddy_ask_data *bla ) |
---|
| 233 | { |
---|
[0da65d5] | 234 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
[8e5e2e9] | 235 | |
---|
| 236 | g_free( bla->handle ); |
---|
| 237 | g_free( bla ); |
---|
| 238 | } |
---|
| 239 | |
---|
[0da65d5] | 240 | void jabber_buddy_ask( struct im_connection *ic, char *handle ) |
---|
[8e5e2e9] | 241 | { |
---|
| 242 | struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 ); |
---|
| 243 | char *buf; |
---|
| 244 | |
---|
[0da65d5] | 245 | bla->ic = ic; |
---|
[8e5e2e9] | 246 | bla->handle = g_strdup( handle ); |
---|
| 247 | |
---|
| 248 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle ); |
---|
[84b045d] | 249 | imcb_ask( ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no ); |
---|
[6266fca] | 250 | g_free( buf ); |
---|
[8e5e2e9] | 251 | } |
---|
[6a1128d] | 252 | |
---|
[0d3f30f] | 253 | /* Returns a new string. Don't leak it! */ |
---|
| 254 | char *jabber_normalize( char *orig ) |
---|
| 255 | { |
---|
| 256 | int len, i; |
---|
| 257 | char *new; |
---|
| 258 | |
---|
| 259 | len = strlen( orig ); |
---|
| 260 | new = g_new( char, len + 1 ); |
---|
| 261 | for( i = 0; i < len; i ++ ) |
---|
| 262 | new[i] = tolower( orig[i] ); |
---|
| 263 | |
---|
| 264 | new[i] = 0; |
---|
| 265 | return new; |
---|
| 266 | } |
---|
| 267 | |
---|
[6a1128d] | 268 | /* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a |
---|
[0d3f30f] | 269 | FULL jid or if we already have this buddy/resource. XXX: No, great, actually |
---|
| 270 | buddies from transports don't (usually) have resources. So we'll really have |
---|
| 271 | to deal with that properly. Set their ->resource property to NULL. Do *NOT* |
---|
| 272 | allow to mix this stuff, though... */ |
---|
[0da65d5] | 273 | struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid_ ) |
---|
[6a1128d] | 274 | { |
---|
[0da65d5] | 275 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 276 | struct jabber_buddy *bud, *new, *bi; |
---|
[0d3f30f] | 277 | char *s, *full_jid; |
---|
[6a1128d] | 278 | |
---|
[0d3f30f] | 279 | full_jid = jabber_normalize( full_jid_ ); |
---|
| 280 | |
---|
| 281 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
| 282 | *s = 0; |
---|
[6a1128d] | 283 | |
---|
| 284 | new = g_new0( struct jabber_buddy, 1 ); |
---|
| 285 | |
---|
| 286 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
| 287 | { |
---|
[0d3f30f] | 288 | /* If this is a transport buddy or whatever, it can't have more |
---|
| 289 | than one instance, so this is always wrong: */ |
---|
| 290 | if( s == NULL || bud->resource == NULL ) |
---|
| 291 | { |
---|
| 292 | if( s ) *s = '/'; |
---|
| 293 | g_free( new ); |
---|
| 294 | g_free( full_jid ); |
---|
| 295 | return NULL; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | new->bare_jid = bud->bare_jid; |
---|
[6a1128d] | 299 | |
---|
| 300 | /* We already have another resource for this buddy, add the |
---|
| 301 | new one to the list. */ |
---|
| 302 | for( bi = bud; bi; bi = bi->next ) |
---|
| 303 | { |
---|
[0d3f30f] | 304 | /* Check for dupes. */ |
---|
| 305 | if( g_strcasecmp( bi->resource, s + 1 ) == 0 ) |
---|
[6a1128d] | 306 | { |
---|
| 307 | *s = '/'; |
---|
| 308 | g_free( new ); |
---|
[0d3f30f] | 309 | g_free( full_jid ); |
---|
[6a1128d] | 310 | return NULL; |
---|
| 311 | } |
---|
| 312 | /* Append the new item to the list. */ |
---|
| 313 | else if( bi->next == NULL ) |
---|
| 314 | { |
---|
| 315 | bi->next = new; |
---|
| 316 | break; |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | else |
---|
| 321 | { |
---|
[0d3f30f] | 322 | new->bare_jid = g_strdup( full_jid ); |
---|
| 323 | g_hash_table_insert( jd->buddies, new->bare_jid, new ); |
---|
[6a1128d] | 324 | } |
---|
| 325 | |
---|
[0d3f30f] | 326 | if( s ) |
---|
| 327 | { |
---|
| 328 | *s = '/'; |
---|
| 329 | new->full_jid = full_jid; |
---|
| 330 | new->resource = strchr( new->full_jid, '/' ) + 1; |
---|
| 331 | } |
---|
| 332 | else |
---|
| 333 | { |
---|
| 334 | /* Let's waste some more bytes of RAM instead of to make |
---|
| 335 | memory management a total disaster here.. */ |
---|
| 336 | new->full_jid = full_jid; |
---|
| 337 | } |
---|
[6a1128d] | 338 | |
---|
| 339 | return new; |
---|
| 340 | } |
---|
| 341 | |
---|
[788a1af] | 342 | /* Finds a buddy from our structures. Can find both full- and bare JIDs. When |
---|
| 343 | asked for a bare JID, it uses the "resource_select" setting to see which |
---|
| 344 | resource to pick. */ |
---|
[0da65d5] | 345 | struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags ) |
---|
[6a1128d] | 346 | { |
---|
[0da65d5] | 347 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 348 | struct jabber_buddy *bud; |
---|
[0d3f30f] | 349 | char *s, *jid; |
---|
| 350 | |
---|
| 351 | jid = jabber_normalize( jid_ ); |
---|
[6a1128d] | 352 | |
---|
| 353 | if( ( s = strchr( jid, '/' ) ) ) |
---|
| 354 | { |
---|
| 355 | *s = 0; |
---|
| 356 | if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) ) |
---|
[0d3f30f] | 357 | { |
---|
| 358 | /* Is this one of those no-resource buddies? */ |
---|
| 359 | if( bud->resource == NULL ) |
---|
| 360 | { |
---|
[16b5f86] | 361 | g_free( jid ); |
---|
| 362 | return NULL; |
---|
[0d3f30f] | 363 | } |
---|
| 364 | else |
---|
| 365 | { |
---|
| 366 | /* See if there's an exact match. */ |
---|
| 367 | for( ; bud; bud = bud->next ) |
---|
| 368 | if( g_strcasecmp( bud->resource, s + 1 ) == 0 ) |
---|
| 369 | break; |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
[f0cb961] | 373 | if( bud == NULL && ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid ) ) |
---|
[16b5f86] | 374 | { |
---|
| 375 | *s = '/'; |
---|
[0da65d5] | 376 | bud = jabber_buddy_add( ic, jid ); |
---|
[16b5f86] | 377 | } |
---|
[0d3f30f] | 378 | |
---|
| 379 | g_free( jid ); |
---|
| 380 | return bud; |
---|
[6a1128d] | 381 | } |
---|
| 382 | else |
---|
| 383 | { |
---|
[a21a8ac] | 384 | struct jabber_buddy *best_prio, *best_time; |
---|
| 385 | char *set; |
---|
| 386 | |
---|
[0d3f30f] | 387 | bud = g_hash_table_lookup( jd->buddies, jid ); |
---|
| 388 | |
---|
| 389 | g_free( jid ); |
---|
| 390 | |
---|
| 391 | if( bud == NULL ) |
---|
[16b5f86] | 392 | /* No match. Create it now? */ |
---|
[f0cb961] | 393 | return ( ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid_ ) ) ? |
---|
[0da65d5] | 394 | jabber_buddy_add( ic, jid_ ) : NULL; |
---|
[16b5f86] | 395 | else if( bud->resource && ( flags & GET_BUDDY_EXACT ) ) |
---|
| 396 | /* We want an exact match, so in thise case there shouldn't be a /resource. */ |
---|
| 397 | return NULL; |
---|
[0d3f30f] | 398 | else if( ( bud->resource == NULL || bud->next == NULL ) ) |
---|
[16b5f86] | 399 | /* No need for selection if there's only one option. */ |
---|
[0d3f30f] | 400 | return bud; |
---|
| 401 | |
---|
| 402 | best_prio = best_time = bud; |
---|
[a21a8ac] | 403 | for( ; bud; bud = bud->next ) |
---|
| 404 | { |
---|
| 405 | if( bud->priority > best_prio->priority ) |
---|
| 406 | best_prio = bud; |
---|
| 407 | if( bud->last_act > best_time->last_act ) |
---|
| 408 | best_time = bud; |
---|
| 409 | } |
---|
| 410 | |
---|
[0da65d5] | 411 | if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL ) |
---|
[a21a8ac] | 412 | return NULL; |
---|
| 413 | else if( strcmp( set, "activity" ) == 0 ) |
---|
| 414 | return best_time; |
---|
| 415 | else /* if( strcmp( set, "priority" ) == 0 ) */ |
---|
| 416 | return best_prio; |
---|
[6a1128d] | 417 | } |
---|
| 418 | } |
---|
| 419 | |
---|
[788a1af] | 420 | /* Remove one specific full JID from our list. Use this when a buddy goes |
---|
[0d3f30f] | 421 | off-line (because (s)he can still be online from a different location. |
---|
| 422 | XXX: See above, we should accept bare JIDs too... */ |
---|
[0da65d5] | 423 | int jabber_buddy_remove( struct im_connection *ic, char *full_jid_ ) |
---|
[6a1128d] | 424 | { |
---|
[0da65d5] | 425 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 426 | struct jabber_buddy *bud, *prev, *bi; |
---|
[0d3f30f] | 427 | char *s, *full_jid; |
---|
[6a1128d] | 428 | |
---|
[0d3f30f] | 429 | full_jid = jabber_normalize( full_jid_ ); |
---|
| 430 | |
---|
| 431 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
| 432 | *s = 0; |
---|
[6a1128d] | 433 | |
---|
| 434 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
| 435 | { |
---|
| 436 | /* If there's only one item in the list (and if the resource |
---|
| 437 | matches), removing it is simple. (And the hash reference |
---|
| 438 | should be removed too!) */ |
---|
[0d3f30f] | 439 | if( bud->next == NULL && ( ( s == NULL || bud->resource == NULL ) || g_strcasecmp( bud->resource, s + 1 ) == 0 ) ) |
---|
[6a1128d] | 440 | { |
---|
[0d3f30f] | 441 | g_hash_table_remove( jd->buddies, bud->bare_jid ); |
---|
| 442 | g_free( bud->bare_jid ); |
---|
[a21a8ac] | 443 | g_free( bud->full_jid ); |
---|
[6a1128d] | 444 | g_free( bud->away_message ); |
---|
| 445 | g_free( bud ); |
---|
[0d3f30f] | 446 | |
---|
| 447 | g_free( full_jid ); |
---|
| 448 | |
---|
| 449 | return 1; |
---|
| 450 | } |
---|
| 451 | else if( s == NULL || bud->resource == NULL ) |
---|
| 452 | { |
---|
| 453 | /* Tried to remove a bare JID while this JID does seem |
---|
| 454 | to have resources... (Or the opposite.) *sigh* */ |
---|
| 455 | g_free( full_jid ); |
---|
| 456 | return 0; |
---|
[6a1128d] | 457 | } |
---|
| 458 | else |
---|
| 459 | { |
---|
| 460 | for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next ) |
---|
[0d3f30f] | 461 | if( g_strcasecmp( bi->resource, s + 1 ) == 0 ) |
---|
[6a1128d] | 462 | break; |
---|
| 463 | |
---|
[0d3f30f] | 464 | g_free( full_jid ); |
---|
| 465 | |
---|
[6a1128d] | 466 | if( bi ) |
---|
| 467 | { |
---|
| 468 | if( prev ) |
---|
| 469 | prev->next = bi->next; |
---|
| 470 | else |
---|
| 471 | /* The hash table should point at the second |
---|
| 472 | item, because we're removing the first. */ |
---|
[0d3f30f] | 473 | g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next ); |
---|
[6a1128d] | 474 | |
---|
[a21a8ac] | 475 | g_free( bi->full_jid ); |
---|
[6a1128d] | 476 | g_free( bi->away_message ); |
---|
| 477 | g_free( bi ); |
---|
[0d3f30f] | 478 | |
---|
| 479 | return 1; |
---|
[6a1128d] | 480 | } |
---|
| 481 | else |
---|
| 482 | { |
---|
| 483 | return 0; |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | else |
---|
| 488 | { |
---|
[0d3f30f] | 489 | g_free( full_jid ); |
---|
[6a1128d] | 490 | return 0; |
---|
| 491 | } |
---|
| 492 | } |
---|
[788a1af] | 493 | |
---|
| 494 | /* Remove a buddy completely; removes all resources that belong to the |
---|
| 495 | specified bare JID. Use this when removing someone from the contact |
---|
| 496 | list, for example. */ |
---|
[0da65d5] | 497 | int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid_ ) |
---|
[788a1af] | 498 | { |
---|
[0da65d5] | 499 | struct jabber_data *jd = ic->proto_data; |
---|
[788a1af] | 500 | struct jabber_buddy *bud, *next; |
---|
[0d3f30f] | 501 | char *bare_jid; |
---|
[788a1af] | 502 | |
---|
[0d3f30f] | 503 | if( strchr( bare_jid_, '/' ) ) |
---|
[788a1af] | 504 | return 0; |
---|
| 505 | |
---|
[0d3f30f] | 506 | bare_jid = jabber_normalize( bare_jid_ ); |
---|
| 507 | |
---|
[788a1af] | 508 | if( ( bud = g_hash_table_lookup( jd->buddies, bare_jid ) ) ) |
---|
| 509 | { |
---|
| 510 | /* Most important: Remove the hash reference. We don't know |
---|
| 511 | this buddy anymore. */ |
---|
[0d3f30f] | 512 | g_hash_table_remove( jd->buddies, bud->bare_jid ); |
---|
[788a1af] | 513 | |
---|
| 514 | /* Deallocate the linked list of resources. */ |
---|
| 515 | while( bud ) |
---|
| 516 | { |
---|
| 517 | next = bud->next; |
---|
| 518 | g_free( bud->full_jid ); |
---|
| 519 | g_free( bud->away_message ); |
---|
| 520 | g_free( bud ); |
---|
| 521 | bud = next; |
---|
| 522 | } |
---|
| 523 | |
---|
[0d3f30f] | 524 | g_free( bare_jid ); |
---|
[788a1af] | 525 | return 1; |
---|
| 526 | } |
---|
| 527 | else |
---|
| 528 | { |
---|
[0d3f30f] | 529 | g_free( bare_jid ); |
---|
[788a1af] | 530 | return 0; |
---|
| 531 | } |
---|
| 532 | } |
---|