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