[f06894d] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
[21167d2] | 4 | * Jabber module - Misc. stuff * |
---|
[f06894d] | 5 | * * |
---|
[842cd8d] | 6 | * Copyright 2006-2010 Wilmer van der Gaast <wilmer@gaast.net> |
---|
[f06894d] | 7 | * * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify * |
---|
| 9 | * it under the terms of the GNU General Public License as published by * |
---|
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 11 | * (at your option) any later version. * |
---|
| 12 | * * |
---|
| 13 | * This program is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
| 21 | * * |
---|
| 22 | \***************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include "jabber.h" |
---|
[89d736a] | 25 | #include "md5.h" |
---|
| 26 | #include "base64.h" |
---|
[f06894d] | 27 | |
---|
[dfa41a4] | 28 | static unsigned int next_id = 1; |
---|
[21167d2] | 29 | |
---|
[ebe7b36] | 30 | char *set_eval_priority( set_t *set, char *value ) |
---|
[f06894d] | 31 | { |
---|
| 32 | account_t *acc = set->data; |
---|
[788a1af] | 33 | int i; |
---|
[f06894d] | 34 | |
---|
[788a1af] | 35 | if( sscanf( value, "%d", &i ) == 1 ) |
---|
| 36 | { |
---|
| 37 | /* Priority is a signed 8-bit integer, according to RFC 3921. */ |
---|
| 38 | if( i < -128 || i > 127 ) |
---|
[7125cb3] | 39 | return SET_INVALID; |
---|
[788a1af] | 40 | } |
---|
| 41 | else |
---|
[7125cb3] | 42 | return SET_INVALID; |
---|
[172a73f1] | 43 | |
---|
| 44 | /* Only run this stuff if the account is online ATM, |
---|
| 45 | and if the setting seems to be acceptable. */ |
---|
[0da65d5] | 46 | if( acc->ic ) |
---|
[f06894d] | 47 | { |
---|
[ebe7b36] | 48 | /* Although set_eval functions usually are very nice and |
---|
| 49 | convenient, they have one disadvantage: If I would just |
---|
| 50 | call p_s_u() now to send the new prio setting, it would |
---|
| 51 | send the old setting because the set->value gets changed |
---|
[e35d1a1] | 52 | after the (this) eval returns a non-NULL value. |
---|
[ebe7b36] | 53 | |
---|
| 54 | So now I can choose between implementing post-set |
---|
| 55 | functions next to evals, or just do this little hack: */ |
---|
| 56 | |
---|
| 57 | g_free( set->value ); |
---|
[788a1af] | 58 | set->value = g_strdup( value ); |
---|
[ebe7b36] | 59 | |
---|
| 60 | /* (Yes, sorry, I prefer the hack. :-P) */ |
---|
| 61 | |
---|
[0da65d5] | 62 | presence_send_update( acc->ic ); |
---|
[f06894d] | 63 | } |
---|
| 64 | |
---|
[788a1af] | 65 | return value; |
---|
[f06894d] | 66 | } |
---|
| 67 | |
---|
| 68 | char *set_eval_tls( set_t *set, char *value ) |
---|
| 69 | { |
---|
| 70 | if( g_strcasecmp( value, "try" ) == 0 ) |
---|
| 71 | return value; |
---|
| 72 | else |
---|
| 73 | return set_eval_bool( set, value ); |
---|
| 74 | } |
---|
[21167d2] | 75 | |
---|
| 76 | struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children ) |
---|
| 77 | { |
---|
| 78 | struct xt_node *node; |
---|
| 79 | |
---|
| 80 | node = xt_new_node( name, NULL, children ); |
---|
| 81 | |
---|
| 82 | if( type ) |
---|
| 83 | xt_add_attr( node, "type", type ); |
---|
| 84 | if( to ) |
---|
| 85 | xt_add_attr( node, "to", to ); |
---|
| 86 | |
---|
[dfa41a4] | 87 | /* IQ packets should always have an ID, so let's generate one. It |
---|
| 88 | might get overwritten by jabber_cache_add() if this packet has |
---|
| 89 | to be saved until we receive a response. Cached packets get |
---|
| 90 | slightly different IDs so we can recognize them. */ |
---|
| 91 | if( strcmp( name, "iq" ) == 0 ) |
---|
| 92 | { |
---|
| 93 | char *id = g_strdup_printf( "%s%05x", JABBER_PACKET_ID, ( next_id++ ) & 0xfffff ); |
---|
| 94 | xt_add_attr( node, "id", id ); |
---|
| 95 | g_free( id ); |
---|
| 96 | } |
---|
| 97 | |
---|
[fe7a554] | 98 | return node; |
---|
| 99 | } |
---|
| 100 | |
---|
[259edd4] | 101 | struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type ) |
---|
| 102 | { |
---|
| 103 | struct xt_node *node, *c; |
---|
| 104 | char *to; |
---|
| 105 | |
---|
| 106 | /* Create the "defined-condition" tag. */ |
---|
| 107 | c = xt_new_node( err_cond, NULL, NULL ); |
---|
[47d3ac4] | 108 | xt_add_attr( c, "xmlns", XMLNS_STANZA_ERROR ); |
---|
[259edd4] | 109 | |
---|
| 110 | /* Put it in an <error> tag. */ |
---|
| 111 | c = xt_new_node( "error", NULL, c ); |
---|
| 112 | xt_add_attr( c, "type", err_type ); |
---|
| 113 | |
---|
| 114 | /* To make the actual error packet, we copy the original packet and |
---|
| 115 | add our <error>/type="error" tag. Including the original packet |
---|
| 116 | is recommended, so let's just do it. */ |
---|
| 117 | node = xt_dup( orig ); |
---|
| 118 | xt_add_child( node, c ); |
---|
| 119 | xt_add_attr( node, "type", "error" ); |
---|
| 120 | |
---|
| 121 | /* Return to sender. */ |
---|
| 122 | if( ( to = xt_find_attr( node, "from" ) ) ) |
---|
| 123 | { |
---|
| 124 | xt_add_attr( node, "to", to ); |
---|
| 125 | xt_remove_attr( node, "from" ); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return node; |
---|
| 129 | } |
---|
| 130 | |
---|
[dfa41a4] | 131 | /* Cache a node/packet for later use. Mainly useful for IQ packets if you need |
---|
[fe7a554] | 132 | them when you receive the response. Use this BEFORE sending the packet so |
---|
[e35d1a1] | 133 | it'll get a new id= tag, and do NOT free() the packet after sending it! */ |
---|
[0da65d5] | 134 | void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func ) |
---|
[fe7a554] | 135 | { |
---|
[0da65d5] | 136 | struct jabber_data *jd = ic->proto_data; |
---|
[038d17f] | 137 | struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 ); |
---|
[89d736a] | 138 | md5_state_t id_hash; |
---|
| 139 | md5_byte_t id_sum[16]; |
---|
| 140 | char *id, *asc_hash; |
---|
[fe7a554] | 141 | |
---|
[89d736a] | 142 | next_id ++; |
---|
| 143 | |
---|
| 144 | id_hash = jd->cached_id_prefix; |
---|
| 145 | md5_append( &id_hash, (md5_byte_t*) &next_id, sizeof( next_id ) ); |
---|
| 146 | md5_finish( &id_hash, id_sum ); |
---|
| 147 | asc_hash = base64_encode( id_sum, 12 ); |
---|
| 148 | |
---|
| 149 | id = g_strdup_printf( "%s%s", JABBER_CACHED_ID, asc_hash ); |
---|
[fe7a554] | 150 | xt_add_attr( node, "id", id ); |
---|
[21167d2] | 151 | g_free( id ); |
---|
[89d736a] | 152 | g_free( asc_hash ); |
---|
[038d17f] | 153 | |
---|
| 154 | entry->node = node; |
---|
[861c199] | 155 | entry->func = func; |
---|
[979cfb4] | 156 | entry->saved_at = time( NULL ); |
---|
[038d17f] | 157 | g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry ); |
---|
[fe7a554] | 158 | } |
---|
| 159 | |
---|
[038d17f] | 160 | void jabber_cache_entry_free( gpointer data ) |
---|
| 161 | { |
---|
| 162 | struct jabber_cache_entry *entry = data; |
---|
[21167d2] | 163 | |
---|
[038d17f] | 164 | xt_free_node( entry->node ); |
---|
| 165 | g_free( entry ); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer ); |
---|
| 169 | |
---|
[861c199] | 170 | /* This one should be called from time to time (from keepalive, in this case) |
---|
| 171 | to make sure things don't stay in the node cache forever. By marking nodes |
---|
| 172 | during the first run and deleting marked nodes during a next run, every |
---|
| 173 | node should be available in the cache for at least a minute (assuming the |
---|
| 174 | function is indeed called every minute). */ |
---|
[0da65d5] | 175 | void jabber_cache_clean( struct im_connection *ic ) |
---|
[038d17f] | 176 | { |
---|
[0da65d5] | 177 | struct jabber_data *jd = ic->proto_data; |
---|
[979cfb4] | 178 | time_t threshold = time( NULL ) - JABBER_CACHE_MAX_AGE; |
---|
[038d17f] | 179 | |
---|
[979cfb4] | 180 | g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, &threshold ); |
---|
[038d17f] | 181 | } |
---|
| 182 | |
---|
[979cfb4] | 183 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer threshold_ ) |
---|
[038d17f] | 184 | { |
---|
| 185 | struct jabber_cache_entry *entry = entry_; |
---|
[979cfb4] | 186 | time_t *threshold = threshold_; |
---|
[038d17f] | 187 | |
---|
[979cfb4] | 188 | return entry->saved_at < *threshold; |
---|
[21167d2] | 189 | } |
---|
[5e202b0] | 190 | |
---|
[4306d8b] | 191 | xt_status jabber_cache_handle_packet( struct im_connection *ic, struct xt_node *node ) |
---|
| 192 | { |
---|
| 193 | struct jabber_data *jd = ic->proto_data; |
---|
| 194 | struct jabber_cache_entry *entry; |
---|
| 195 | char *s; |
---|
| 196 | |
---|
| 197 | if( ( s = xt_find_attr( node, "id" ) ) == NULL || |
---|
[89d736a] | 198 | strncmp( s, JABBER_CACHED_ID, strlen( JABBER_CACHED_ID ) ) != 0 ) |
---|
[4306d8b] | 199 | { |
---|
| 200 | /* Silently ignore it, without an ID (or a non-cache |
---|
| 201 | ID) we don't know how to handle the packet and we |
---|
| 202 | probably don't have to. */ |
---|
| 203 | return XT_HANDLED; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | entry = g_hash_table_lookup( jd->node_cache, s ); |
---|
| 207 | |
---|
| 208 | if( entry == NULL ) |
---|
| 209 | { |
---|
[89d736a] | 210 | /* |
---|
| 211 | There's no longer an easy way to see if we generated this |
---|
| 212 | one or someone else, and there's a ten-minute timeout anyway, |
---|
| 213 | so meh. |
---|
| 214 | |
---|
[43462708] | 215 | imcb_log( ic, "Warning: Received %s-%s packet with unknown/expired ID %s!", |
---|
[4306d8b] | 216 | node->name, xt_find_attr( node, "type" ) ? : "(no type)", s ); |
---|
[89d736a] | 217 | */ |
---|
[4306d8b] | 218 | } |
---|
| 219 | else if( entry->func ) |
---|
| 220 | { |
---|
| 221 | return entry->func( ic, node, entry->node ); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | return XT_HANDLED; |
---|
| 225 | } |
---|
| 226 | |
---|
[5e202b0] | 227 | const struct jabber_away_state jabber_away_state_list[] = |
---|
| 228 | { |
---|
| 229 | { "away", "Away" }, |
---|
[840bba8] | 230 | { "chat", "Free for Chat" }, /* WTF actually uses this? */ |
---|
[5e202b0] | 231 | { "dnd", "Do not Disturb" }, |
---|
| 232 | { "xa", "Extended Away" }, |
---|
| 233 | { "", NULL } |
---|
| 234 | }; |
---|
| 235 | |
---|
| 236 | const struct jabber_away_state *jabber_away_state_by_code( char *code ) |
---|
| 237 | { |
---|
| 238 | int i; |
---|
| 239 | |
---|
[840bba8] | 240 | if( code == NULL ) |
---|
| 241 | return NULL; |
---|
| 242 | |
---|
[5e202b0] | 243 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 244 | if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 ) |
---|
| 245 | return jabber_away_state_list + i; |
---|
| 246 | |
---|
| 247 | return NULL; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | const struct jabber_away_state *jabber_away_state_by_name( char *name ) |
---|
| 251 | { |
---|
| 252 | int i; |
---|
| 253 | |
---|
[840bba8] | 254 | if( name == NULL ) |
---|
| 255 | return NULL; |
---|
| 256 | |
---|
[5e202b0] | 257 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 258 | if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 ) |
---|
| 259 | return jabber_away_state_list + i; |
---|
| 260 | |
---|
| 261 | return NULL; |
---|
| 262 | } |
---|
[8e5e2e9] | 263 | |
---|
| 264 | struct jabber_buddy_ask_data |
---|
| 265 | { |
---|
[0da65d5] | 266 | struct im_connection *ic; |
---|
[8e5e2e9] | 267 | char *handle; |
---|
| 268 | char *realname; |
---|
| 269 | }; |
---|
| 270 | |
---|
[9143aeb] | 271 | static void jabber_buddy_ask_yes( void *data ) |
---|
[8e5e2e9] | 272 | { |
---|
[9143aeb] | 273 | struct jabber_buddy_ask_data *bla = data; |
---|
| 274 | |
---|
[0da65d5] | 275 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
[8e5e2e9] | 276 | |
---|
[f0cb961] | 277 | if( imcb_find_buddy( bla->ic, bla->handle ) == NULL ) |
---|
[84b045d] | 278 | imcb_ask_add( bla->ic, bla->handle, NULL ); |
---|
[8e5e2e9] | 279 | |
---|
| 280 | g_free( bla->handle ); |
---|
| 281 | g_free( bla ); |
---|
| 282 | } |
---|
| 283 | |
---|
[9143aeb] | 284 | static void jabber_buddy_ask_no( void *data ) |
---|
[8e5e2e9] | 285 | { |
---|
[9143aeb] | 286 | struct jabber_buddy_ask_data *bla = data; |
---|
| 287 | |
---|
[0da65d5] | 288 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
[8e5e2e9] | 289 | |
---|
| 290 | g_free( bla->handle ); |
---|
| 291 | g_free( bla ); |
---|
| 292 | } |
---|
| 293 | |
---|
[0da65d5] | 294 | void jabber_buddy_ask( struct im_connection *ic, char *handle ) |
---|
[8e5e2e9] | 295 | { |
---|
| 296 | struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 ); |
---|
| 297 | char *buf; |
---|
| 298 | |
---|
[0da65d5] | 299 | bla->ic = ic; |
---|
[8e5e2e9] | 300 | bla->handle = g_strdup( handle ); |
---|
| 301 | |
---|
| 302 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle ); |
---|
[84b045d] | 303 | imcb_ask( ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no ); |
---|
[6266fca] | 304 | g_free( buf ); |
---|
[8e5e2e9] | 305 | } |
---|
[6a1128d] | 306 | |
---|
[0d3f30f] | 307 | /* Returns a new string. Don't leak it! */ |
---|
[e35d1a1] | 308 | char *jabber_normalize( const char *orig ) |
---|
[0d3f30f] | 309 | { |
---|
| 310 | int len, i; |
---|
| 311 | char *new; |
---|
| 312 | |
---|
| 313 | len = strlen( orig ); |
---|
| 314 | new = g_new( char, len + 1 ); |
---|
[98de2cc] | 315 | |
---|
| 316 | /* So it turns out the /resource part is case sensitive. Yeah, and |
---|
| 317 | it's Unicode but feck Unicode. :-P So stop once we see a slash. */ |
---|
| 318 | for( i = 0; i < len && orig[i] != '/' ; i ++ ) |
---|
[0d3f30f] | 319 | new[i] = tolower( orig[i] ); |
---|
[98de2cc] | 320 | for( ; orig[i]; i ++ ) |
---|
| 321 | new[i] = orig[i]; |
---|
[0d3f30f] | 322 | |
---|
| 323 | new[i] = 0; |
---|
| 324 | return new; |
---|
| 325 | } |
---|
| 326 | |
---|
[6a1128d] | 327 | /* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a |
---|
[0d3f30f] | 328 | FULL jid or if we already have this buddy/resource. XXX: No, great, actually |
---|
| 329 | buddies from transports don't (usually) have resources. So we'll really have |
---|
| 330 | to deal with that properly. Set their ->resource property to NULL. Do *NOT* |
---|
| 331 | allow to mix this stuff, though... */ |
---|
[0da65d5] | 332 | struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid_ ) |
---|
[6a1128d] | 333 | { |
---|
[0da65d5] | 334 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 335 | struct jabber_buddy *bud, *new, *bi; |
---|
[0d3f30f] | 336 | char *s, *full_jid; |
---|
[6a1128d] | 337 | |
---|
[0d3f30f] | 338 | full_jid = jabber_normalize( full_jid_ ); |
---|
| 339 | |
---|
| 340 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
| 341 | *s = 0; |
---|
[6a1128d] | 342 | |
---|
| 343 | new = g_new0( struct jabber_buddy, 1 ); |
---|
| 344 | |
---|
| 345 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
| 346 | { |
---|
[76c85b4c] | 347 | /* The first entry is always a bare JID. If there are more, we |
---|
| 348 | should ignore the first one here. */ |
---|
| 349 | if( bud->next ) |
---|
| 350 | bud = bud->next; |
---|
| 351 | |
---|
[0d3f30f] | 352 | /* If this is a transport buddy or whatever, it can't have more |
---|
| 353 | than one instance, so this is always wrong: */ |
---|
| 354 | if( s == NULL || bud->resource == NULL ) |
---|
| 355 | { |
---|
| 356 | if( s ) *s = '/'; |
---|
| 357 | g_free( new ); |
---|
| 358 | g_free( full_jid ); |
---|
| 359 | return NULL; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | new->bare_jid = bud->bare_jid; |
---|
[6a1128d] | 363 | |
---|
| 364 | /* We already have another resource for this buddy, add the |
---|
| 365 | new one to the list. */ |
---|
| 366 | for( bi = bud; bi; bi = bi->next ) |
---|
| 367 | { |
---|
[0d3f30f] | 368 | /* Check for dupes. */ |
---|
[98de2cc] | 369 | if( strcmp( bi->resource, s + 1 ) == 0 ) |
---|
[6a1128d] | 370 | { |
---|
| 371 | *s = '/'; |
---|
| 372 | g_free( new ); |
---|
[0d3f30f] | 373 | g_free( full_jid ); |
---|
[6a1128d] | 374 | return NULL; |
---|
| 375 | } |
---|
| 376 | /* Append the new item to the list. */ |
---|
| 377 | else if( bi->next == NULL ) |
---|
| 378 | { |
---|
| 379 | bi->next = new; |
---|
| 380 | break; |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | else |
---|
| 385 | { |
---|
[76c85b4c] | 386 | new->full_jid = new->bare_jid = g_strdup( full_jid ); |
---|
[0d3f30f] | 387 | g_hash_table_insert( jd->buddies, new->bare_jid, new ); |
---|
[76c85b4c] | 388 | |
---|
| 389 | if( s ) |
---|
| 390 | { |
---|
| 391 | new->next = g_new0( struct jabber_buddy, 1 ); |
---|
| 392 | new->next->bare_jid = new->bare_jid; |
---|
| 393 | new = new->next; |
---|
| 394 | } |
---|
[6a1128d] | 395 | } |
---|
| 396 | |
---|
[0d3f30f] | 397 | if( s ) |
---|
| 398 | { |
---|
| 399 | *s = '/'; |
---|
| 400 | new->full_jid = full_jid; |
---|
| 401 | new->resource = strchr( new->full_jid, '/' ) + 1; |
---|
| 402 | } |
---|
| 403 | else |
---|
| 404 | { |
---|
| 405 | /* Let's waste some more bytes of RAM instead of to make |
---|
[b9f8b87] | 406 | memory management a total disaster here. And it saves |
---|
| 407 | me one g_free() call in this function. :-P */ |
---|
[0d3f30f] | 408 | new->full_jid = full_jid; |
---|
| 409 | } |
---|
[6a1128d] | 410 | |
---|
| 411 | return new; |
---|
| 412 | } |
---|
| 413 | |
---|
[788a1af] | 414 | /* Finds a buddy from our structures. Can find both full- and bare JIDs. When |
---|
| 415 | asked for a bare JID, it uses the "resource_select" setting to see which |
---|
| 416 | resource to pick. */ |
---|
[0da65d5] | 417 | struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags ) |
---|
[6a1128d] | 418 | { |
---|
[0da65d5] | 419 | struct jabber_data *jd = ic->proto_data; |
---|
[76c85b4c] | 420 | struct jabber_buddy *bud, *head; |
---|
[0d3f30f] | 421 | char *s, *jid; |
---|
| 422 | |
---|
| 423 | jid = jabber_normalize( jid_ ); |
---|
[6a1128d] | 424 | |
---|
| 425 | if( ( s = strchr( jid, '/' ) ) ) |
---|
| 426 | { |
---|
[08e5bb2] | 427 | int bare_exists = 0; |
---|
| 428 | |
---|
[6a1128d] | 429 | *s = 0; |
---|
| 430 | if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) ) |
---|
[0d3f30f] | 431 | { |
---|
[08e5bb2] | 432 | bare_exists = 1; |
---|
| 433 | |
---|
[76c85b4c] | 434 | if( bud->next ) |
---|
| 435 | bud = bud->next; |
---|
| 436 | |
---|
[0adce21] | 437 | /* Just return the first one for this bare JID. */ |
---|
| 438 | if( flags & GET_BUDDY_FIRST ) |
---|
| 439 | { |
---|
| 440 | *s = '/'; |
---|
| 441 | g_free( jid ); |
---|
| 442 | return bud; |
---|
| 443 | } |
---|
| 444 | |
---|
[0d3f30f] | 445 | /* Is this one of those no-resource buddies? */ |
---|
| 446 | if( bud->resource == NULL ) |
---|
| 447 | { |
---|
[0adce21] | 448 | *s = '/'; |
---|
[16b5f86] | 449 | g_free( jid ); |
---|
| 450 | return NULL; |
---|
[0d3f30f] | 451 | } |
---|
[0adce21] | 452 | |
---|
| 453 | /* See if there's an exact match. */ |
---|
| 454 | for( ; bud; bud = bud->next ) |
---|
[98de2cc] | 455 | if( strcmp( bud->resource, s + 1 ) == 0 ) |
---|
[0adce21] | 456 | break; |
---|
[0d3f30f] | 457 | } |
---|
| 458 | |
---|
[08e5bb2] | 459 | if( bud == NULL && ( flags & GET_BUDDY_CREAT ) && |
---|
[dde9d571] | 460 | ( bare_exists || imcb_find_buddy( ic, jid ) ) ) |
---|
[16b5f86] | 461 | { |
---|
| 462 | *s = '/'; |
---|
[0da65d5] | 463 | bud = jabber_buddy_add( ic, jid ); |
---|
[16b5f86] | 464 | } |
---|
[0d3f30f] | 465 | |
---|
| 466 | g_free( jid ); |
---|
| 467 | return bud; |
---|
[6a1128d] | 468 | } |
---|
| 469 | else |
---|
| 470 | { |
---|
[a21a8ac] | 471 | struct jabber_buddy *best_prio, *best_time; |
---|
| 472 | char *set; |
---|
| 473 | |
---|
[76c85b4c] | 474 | head = g_hash_table_lookup( jd->buddies, jid ); |
---|
[20e830b] | 475 | bud = ( head && head->next ) ? head->next : head; |
---|
[0d3f30f] | 476 | |
---|
| 477 | g_free( jid ); |
---|
| 478 | |
---|
| 479 | if( bud == NULL ) |
---|
[16b5f86] | 480 | /* No match. Create it now? */ |
---|
[f0cb961] | 481 | return ( ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid_ ) ) ? |
---|
[0da65d5] | 482 | jabber_buddy_add( ic, jid_ ) : NULL; |
---|
[16b5f86] | 483 | else if( bud->resource && ( flags & GET_BUDDY_EXACT ) ) |
---|
| 484 | /* We want an exact match, so in thise case there shouldn't be a /resource. */ |
---|
| 485 | return NULL; |
---|
[98de2cc] | 486 | else if( bud->resource == NULL || bud->next == NULL ) |
---|
[16b5f86] | 487 | /* No need for selection if there's only one option. */ |
---|
[0d3f30f] | 488 | return bud; |
---|
[0adce21] | 489 | else if( flags & GET_BUDDY_FIRST ) |
---|
| 490 | /* Looks like the caller doesn't care about details. */ |
---|
| 491 | return bud; |
---|
[76c85b4c] | 492 | else if( flags & GET_BUDDY_BARE ) |
---|
| 493 | return head; |
---|
[0d3f30f] | 494 | |
---|
| 495 | best_prio = best_time = bud; |
---|
[a21a8ac] | 496 | for( ; bud; bud = bud->next ) |
---|
| 497 | { |
---|
| 498 | if( bud->priority > best_prio->priority ) |
---|
| 499 | best_prio = bud; |
---|
[76c85b4c] | 500 | if( bud->last_msg > best_time->last_msg ) |
---|
[a21a8ac] | 501 | best_time = bud; |
---|
| 502 | } |
---|
| 503 | |
---|
[0da65d5] | 504 | if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL ) |
---|
[a21a8ac] | 505 | return NULL; |
---|
[76c85b4c] | 506 | else if( strcmp( set, "priority" ) == 0 ) |
---|
[a21a8ac] | 507 | return best_prio; |
---|
[76c85b4c] | 508 | else if( flags & GET_BUDDY_BARE_OK ) /* && strcmp( set, "activity" ) == 0 */ |
---|
| 509 | { |
---|
| 510 | if( best_time->last_msg + set_getint( &ic->acc->set, "activity_timeout" ) >= time( NULL ) ) |
---|
| 511 | return best_time; |
---|
| 512 | else |
---|
| 513 | return head; |
---|
| 514 | } |
---|
| 515 | else |
---|
| 516 | return best_time; |
---|
[6a1128d] | 517 | } |
---|
| 518 | } |
---|
| 519 | |
---|
[b9f8b87] | 520 | /* I'm keeping a separate ext_jid attribute to save a JID that makes sense |
---|
| 521 | to export to BitlBee. This is mainly for groupchats right now. It's |
---|
| 522 | a bit of a hack, but I just think having the user nickname in the hostname |
---|
| 523 | part of the hostmask doesn't look nice on IRC. Normally you can convert |
---|
| 524 | a normal JID to ext_jid by swapping the part before and after the / and |
---|
| 525 | replacing the / with a =. But there should be some stripping (@s are |
---|
| 526 | allowed in Jabber nicks...). */ |
---|
| 527 | struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags ) |
---|
| 528 | { |
---|
| 529 | struct jabber_buddy *bud; |
---|
| 530 | char *s, *jid; |
---|
| 531 | |
---|
| 532 | jid = jabber_normalize( jid_ ); |
---|
| 533 | |
---|
| 534 | if( ( s = strchr( jid, '=' ) ) == NULL ) |
---|
| 535 | return NULL; |
---|
| 536 | |
---|
| 537 | for( bud = jabber_buddy_by_jid( ic, s + 1, GET_BUDDY_FIRST ); bud; bud = bud->next ) |
---|
| 538 | { |
---|
| 539 | /* Hmmm, could happen if not all people in the chat are anonymized? */ |
---|
| 540 | if( bud->ext_jid == NULL ) |
---|
| 541 | continue; |
---|
| 542 | |
---|
| 543 | if( strcmp( bud->ext_jid, jid ) == 0 ) |
---|
| 544 | break; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | g_free( jid ); |
---|
| 548 | |
---|
| 549 | return bud; |
---|
| 550 | } |
---|
| 551 | |
---|
[788a1af] | 552 | /* Remove one specific full JID from our list. Use this when a buddy goes |
---|
[0d3f30f] | 553 | off-line (because (s)he can still be online from a different location. |
---|
| 554 | XXX: See above, we should accept bare JIDs too... */ |
---|
[0da65d5] | 555 | int jabber_buddy_remove( struct im_connection *ic, char *full_jid_ ) |
---|
[6a1128d] | 556 | { |
---|
[0da65d5] | 557 | struct jabber_data *jd = ic->proto_data; |
---|
[842cd8d] | 558 | struct jabber_buddy *bud, *prev = NULL, *bi; |
---|
[0d3f30f] | 559 | char *s, *full_jid; |
---|
[6a1128d] | 560 | |
---|
[0d3f30f] | 561 | full_jid = jabber_normalize( full_jid_ ); |
---|
| 562 | |
---|
| 563 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
| 564 | *s = 0; |
---|
[6a1128d] | 565 | |
---|
[20e830b] | 566 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
[6a1128d] | 567 | { |
---|
[20e830b] | 568 | if( bud->next ) |
---|
[842cd8d] | 569 | bud = (prev=bud)->next; |
---|
[76c85b4c] | 570 | |
---|
[6a1128d] | 571 | /* If there's only one item in the list (and if the resource |
---|
| 572 | matches), removing it is simple. (And the hash reference |
---|
| 573 | should be removed too!) */ |
---|
[3e6764a] | 574 | if( bud->next == NULL && |
---|
| 575 | ( ( s == NULL && bud->resource == NULL ) || |
---|
[98de2cc] | 576 | ( bud->resource && s && strcmp( bud->resource, s + 1 ) == 0 ) ) ) |
---|
[6a1128d] | 577 | { |
---|
[76c85b4c] | 578 | return jabber_buddy_remove_bare( ic, full_jid ); |
---|
[0d3f30f] | 579 | } |
---|
| 580 | else if( s == NULL || bud->resource == NULL ) |
---|
| 581 | { |
---|
| 582 | /* Tried to remove a bare JID while this JID does seem |
---|
| 583 | to have resources... (Or the opposite.) *sigh* */ |
---|
| 584 | g_free( full_jid ); |
---|
| 585 | return 0; |
---|
[6a1128d] | 586 | } |
---|
| 587 | else |
---|
| 588 | { |
---|
[842cd8d] | 589 | for( bi = bud; bi; bi = (prev=bi)->next ) |
---|
[98de2cc] | 590 | if( strcmp( bi->resource, s + 1 ) == 0 ) |
---|
[6a1128d] | 591 | break; |
---|
| 592 | |
---|
[0d3f30f] | 593 | g_free( full_jid ); |
---|
| 594 | |
---|
[6a1128d] | 595 | if( bi ) |
---|
| 596 | { |
---|
| 597 | if( prev ) |
---|
| 598 | prev->next = bi->next; |
---|
| 599 | else |
---|
[842cd8d] | 600 | /* Don't think this should ever happen anymore. */ |
---|
[0d3f30f] | 601 | g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next ); |
---|
[6a1128d] | 602 | |
---|
[6286f80] | 603 | g_free( bi->ext_jid ); |
---|
[a21a8ac] | 604 | g_free( bi->full_jid ); |
---|
[6a1128d] | 605 | g_free( bi->away_message ); |
---|
| 606 | g_free( bi ); |
---|
[0d3f30f] | 607 | |
---|
| 608 | return 1; |
---|
[6a1128d] | 609 | } |
---|
| 610 | else |
---|
| 611 | { |
---|
| 612 | return 0; |
---|
| 613 | } |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | else |
---|
| 617 | { |
---|
[0d3f30f] | 618 | g_free( full_jid ); |
---|
[6a1128d] | 619 | return 0; |
---|
| 620 | } |
---|
| 621 | } |
---|
[788a1af] | 622 | |
---|
| 623 | /* Remove a buddy completely; removes all resources that belong to the |
---|
| 624 | specified bare JID. Use this when removing someone from the contact |
---|
| 625 | list, for example. */ |
---|
[9da0bbf] | 626 | int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid ) |
---|
[788a1af] | 627 | { |
---|
[0da65d5] | 628 | struct jabber_data *jd = ic->proto_data; |
---|
[788a1af] | 629 | struct jabber_buddy *bud, *next; |
---|
| 630 | |
---|
[9da0bbf] | 631 | if( strchr( bare_jid, '/' ) ) |
---|
[788a1af] | 632 | return 0; |
---|
| 633 | |
---|
[9da0bbf] | 634 | if( ( bud = jabber_buddy_by_jid( ic, bare_jid, GET_BUDDY_FIRST ) ) ) |
---|
[788a1af] | 635 | { |
---|
| 636 | /* Most important: Remove the hash reference. We don't know |
---|
| 637 | this buddy anymore. */ |
---|
[0d3f30f] | 638 | g_hash_table_remove( jd->buddies, bud->bare_jid ); |
---|
[9da0bbf] | 639 | g_free( bud->bare_jid ); |
---|
[788a1af] | 640 | |
---|
| 641 | /* Deallocate the linked list of resources. */ |
---|
| 642 | while( bud ) |
---|
| 643 | { |
---|
[9da0bbf] | 644 | /* ext_jid && anonymous means that this buddy is |
---|
| 645 | specific to one groupchat (the one we're |
---|
| 646 | currently cleaning up) so it can be deleted |
---|
| 647 | completely. */ |
---|
| 648 | if( bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS ) |
---|
| 649 | imcb_remove_buddy( ic, bud->ext_jid, NULL ); |
---|
| 650 | |
---|
[788a1af] | 651 | next = bud->next; |
---|
[6286f80] | 652 | g_free( bud->ext_jid ); |
---|
[788a1af] | 653 | g_free( bud->full_jid ); |
---|
| 654 | g_free( bud->away_message ); |
---|
| 655 | g_free( bud ); |
---|
| 656 | bud = next; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | return 1; |
---|
| 660 | } |
---|
| 661 | else |
---|
| 662 | { |
---|
| 663 | return 0; |
---|
| 664 | } |
---|
| 665 | } |
---|
[e35d1a1] | 666 | |
---|
[43671b9] | 667 | time_t jabber_get_timestamp( struct xt_node *xt ) |
---|
| 668 | { |
---|
| 669 | struct tm tp, utc; |
---|
| 670 | struct xt_node *c; |
---|
| 671 | time_t res, tres; |
---|
| 672 | char *s = NULL; |
---|
| 673 | |
---|
| 674 | for( c = xt->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) |
---|
| 675 | { |
---|
| 676 | if( ( s = xt_find_attr( c, "xmlns" ) ) && strcmp( s, XMLNS_DELAY ) == 0 ) |
---|
| 677 | break; |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | if( !c || !( s = xt_find_attr( c, "stamp" ) ) ) |
---|
| 681 | return 0; |
---|
| 682 | |
---|
| 683 | memset( &tp, 0, sizeof( tp ) ); |
---|
| 684 | if( sscanf( s, "%4d%2d%2dT%2d:%2d:%2d", &tp.tm_year, &tp.tm_mon, &tp.tm_mday, |
---|
| 685 | &tp.tm_hour, &tp.tm_min, &tp.tm_sec ) != 6 ) |
---|
| 686 | return 0; |
---|
| 687 | |
---|
| 688 | tp.tm_year -= 1900; |
---|
| 689 | tp.tm_mon --; |
---|
| 690 | tp.tm_isdst = -1; /* GRRRRRRRRRRR */ |
---|
| 691 | |
---|
| 692 | res = mktime( &tp ); |
---|
| 693 | /* Problem is, mktime() just gave us the GMT timestamp for the |
---|
| 694 | given local time... While the given time WAS NOT local. So |
---|
| 695 | we should fix this now. |
---|
| 696 | |
---|
| 697 | Now I could choose between messing with environment variables |
---|
| 698 | (kludgy) or using timegm() (not portable)... Or doing the |
---|
| 699 | following, which I actually prefer... */ |
---|
| 700 | gmtime_r( &res, &utc ); |
---|
| 701 | utc.tm_isdst = -1; /* Once more: GRRRRRRRRRRRRRRRRRR!!! */ |
---|
| 702 | if( utc.tm_hour == tp.tm_hour && utc.tm_min == tp.tm_min ) |
---|
| 703 | /* Sweet! We're in UTC right now... */ |
---|
| 704 | return res; |
---|
| 705 | |
---|
| 706 | tres = mktime( &utc ); |
---|
| 707 | res += res - tres; |
---|
| 708 | |
---|
| 709 | /* Yes, this is a hack. And it will go wrong around DST changes. |
---|
| 710 | BUT this is more likely to be threadsafe than messing with |
---|
| 711 | environment variables, and possibly more portable... */ |
---|
| 712 | |
---|
| 713 | return res; |
---|
| 714 | } |
---|
[1baaef8] | 715 | |
---|
| 716 | struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns ) |
---|
| 717 | { |
---|
[5bd21df] | 718 | struct jabber_error *err; |
---|
[1baaef8] | 719 | struct xt_node *c; |
---|
| 720 | char *s; |
---|
| 721 | |
---|
[5bd21df] | 722 | if( node == NULL ) |
---|
| 723 | return NULL; |
---|
| 724 | |
---|
| 725 | err = g_new0( struct jabber_error, 1 ); |
---|
[1baaef8] | 726 | err->type = xt_find_attr( node, "type" ); |
---|
| 727 | |
---|
| 728 | for( c = node->children; c; c = c->next ) |
---|
| 729 | { |
---|
| 730 | if( !( s = xt_find_attr( c, "xmlns" ) ) || |
---|
| 731 | strcmp( s, xmlns ) != 0 ) |
---|
| 732 | continue; |
---|
| 733 | |
---|
| 734 | if( strcmp( c->name, "text" ) != 0 ) |
---|
| 735 | { |
---|
| 736 | err->code = c->name; |
---|
| 737 | } |
---|
| 738 | /* Only use the text if it doesn't have an xml:lang attribute, |
---|
| 739 | if it's empty or if it's set to something English. */ |
---|
| 740 | else if( !( s = xt_find_attr( c, "xml:lang" ) ) || |
---|
| 741 | !*s || strncmp( s, "en", 2 ) == 0 ) |
---|
| 742 | { |
---|
| 743 | err->text = c->text; |
---|
| 744 | } |
---|
| 745 | } |
---|
| 746 | |
---|
| 747 | return err; |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | void jabber_error_free( struct jabber_error *err ) |
---|
| 751 | { |
---|
| 752 | g_free( err ); |
---|
| 753 | } |
---|