[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 |
---|
[e35d1a1] | 50 | after the (this) eval returns a non-NULL value. |
---|
[ebe7b36] | 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 |
---|
[e35d1a1] | 131 | it'll get a new id= tag, and do NOT free() the packet after sending 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; |
---|
[038d17f] | 135 | struct jabber_cache_entry *entry = g_new0( struct jabber_cache_entry, 1 ); |
---|
[608f8cf] | 136 | char *id; |
---|
[fe7a554] | 137 | |
---|
[608f8cf] | 138 | id = g_strdup_printf( "%s%05x", jd->cached_id_prefix, ( next_id++ ) & 0xfffff ); |
---|
[fe7a554] | 139 | xt_add_attr( node, "id", id ); |
---|
[21167d2] | 140 | g_free( id ); |
---|
[038d17f] | 141 | |
---|
| 142 | entry->node = node; |
---|
[861c199] | 143 | entry->func = func; |
---|
[038d17f] | 144 | g_hash_table_insert( jd->node_cache, xt_find_attr( node, "id" ), entry ); |
---|
[fe7a554] | 145 | } |
---|
| 146 | |
---|
[038d17f] | 147 | void jabber_cache_entry_free( gpointer data ) |
---|
| 148 | { |
---|
| 149 | struct jabber_cache_entry *entry = data; |
---|
[21167d2] | 150 | |
---|
[038d17f] | 151 | xt_free_node( entry->node ); |
---|
| 152 | g_free( entry ); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry, gpointer nullpointer ); |
---|
| 156 | |
---|
[861c199] | 157 | /* This one should be called from time to time (from keepalive, in this case) |
---|
| 158 | to make sure things don't stay in the node cache forever. By marking nodes |
---|
| 159 | during the first run and deleting marked nodes during a next run, every |
---|
| 160 | node should be available in the cache for at least a minute (assuming the |
---|
| 161 | function is indeed called every minute). */ |
---|
[0da65d5] | 162 | void jabber_cache_clean( struct im_connection *ic ) |
---|
[038d17f] | 163 | { |
---|
[0da65d5] | 164 | struct jabber_data *jd = ic->proto_data; |
---|
[038d17f] | 165 | |
---|
| 166 | g_hash_table_foreach_remove( jd->node_cache, jabber_cache_clean_entry, NULL ); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | gboolean jabber_cache_clean_entry( gpointer key, gpointer entry_, gpointer nullpointer ) |
---|
| 170 | { |
---|
| 171 | struct jabber_cache_entry *entry = entry_; |
---|
| 172 | struct xt_node *node = entry->node; |
---|
| 173 | |
---|
| 174 | if( node->flags & XT_SEEN ) |
---|
| 175 | return TRUE; |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | node->flags |= XT_SEEN; |
---|
| 179 | return FALSE; |
---|
| 180 | } |
---|
[21167d2] | 181 | } |
---|
[5e202b0] | 182 | |
---|
[4306d8b] | 183 | xt_status jabber_cache_handle_packet( struct im_connection *ic, struct xt_node *node ) |
---|
| 184 | { |
---|
| 185 | struct jabber_data *jd = ic->proto_data; |
---|
| 186 | struct jabber_cache_entry *entry; |
---|
| 187 | char *s; |
---|
| 188 | |
---|
| 189 | if( ( s = xt_find_attr( node, "id" ) ) == NULL || |
---|
| 190 | strncmp( s, jd->cached_id_prefix, strlen( jd->cached_id_prefix ) ) != 0 ) |
---|
| 191 | { |
---|
| 192 | /* Silently ignore it, without an ID (or a non-cache |
---|
| 193 | ID) we don't know how to handle the packet and we |
---|
| 194 | probably don't have to. */ |
---|
| 195 | return XT_HANDLED; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | entry = g_hash_table_lookup( jd->node_cache, s ); |
---|
| 199 | |
---|
| 200 | if( entry == NULL ) |
---|
| 201 | { |
---|
[43462708] | 202 | imcb_log( ic, "Warning: Received %s-%s packet with unknown/expired ID %s!", |
---|
[4306d8b] | 203 | node->name, xt_find_attr( node, "type" ) ? : "(no type)", s ); |
---|
| 204 | } |
---|
| 205 | else if( entry->func ) |
---|
| 206 | { |
---|
| 207 | return entry->func( ic, node, entry->node ); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | return XT_HANDLED; |
---|
| 211 | } |
---|
| 212 | |
---|
[5e202b0] | 213 | const struct jabber_away_state jabber_away_state_list[] = |
---|
| 214 | { |
---|
| 215 | { "away", "Away" }, |
---|
| 216 | { "chat", "Free for Chat" }, |
---|
| 217 | { "dnd", "Do not Disturb" }, |
---|
| 218 | { "xa", "Extended Away" }, |
---|
| 219 | { "", "Online" }, |
---|
| 220 | { "", NULL } |
---|
| 221 | }; |
---|
| 222 | |
---|
| 223 | const struct jabber_away_state *jabber_away_state_by_code( char *code ) |
---|
| 224 | { |
---|
| 225 | int i; |
---|
| 226 | |
---|
| 227 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 228 | if( g_strcasecmp( jabber_away_state_list[i].code, code ) == 0 ) |
---|
| 229 | return jabber_away_state_list + i; |
---|
| 230 | |
---|
| 231 | return NULL; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | const struct jabber_away_state *jabber_away_state_by_name( char *name ) |
---|
| 235 | { |
---|
| 236 | int i; |
---|
| 237 | |
---|
| 238 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 239 | if( g_strcasecmp( jabber_away_state_list[i].full_name, name ) == 0 ) |
---|
| 240 | return jabber_away_state_list + i; |
---|
| 241 | |
---|
| 242 | return NULL; |
---|
| 243 | } |
---|
[8e5e2e9] | 244 | |
---|
| 245 | struct jabber_buddy_ask_data |
---|
| 246 | { |
---|
[0da65d5] | 247 | struct im_connection *ic; |
---|
[8e5e2e9] | 248 | char *handle; |
---|
| 249 | char *realname; |
---|
| 250 | }; |
---|
| 251 | |
---|
| 252 | static void jabber_buddy_ask_yes( gpointer w, struct jabber_buddy_ask_data *bla ) |
---|
| 253 | { |
---|
[0da65d5] | 254 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
[8e5e2e9] | 255 | |
---|
[f0cb961] | 256 | if( imcb_find_buddy( bla->ic, bla->handle ) == NULL ) |
---|
[84b045d] | 257 | imcb_ask_add( bla->ic, bla->handle, NULL ); |
---|
[8e5e2e9] | 258 | |
---|
| 259 | g_free( bla->handle ); |
---|
| 260 | g_free( bla ); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | static void jabber_buddy_ask_no( gpointer w, struct jabber_buddy_ask_data *bla ) |
---|
| 264 | { |
---|
[0da65d5] | 265 | presence_send_request( bla->ic, bla->handle, "subscribed" ); |
---|
[8e5e2e9] | 266 | |
---|
| 267 | g_free( bla->handle ); |
---|
| 268 | g_free( bla ); |
---|
| 269 | } |
---|
| 270 | |
---|
[0da65d5] | 271 | void jabber_buddy_ask( struct im_connection *ic, char *handle ) |
---|
[8e5e2e9] | 272 | { |
---|
| 273 | struct jabber_buddy_ask_data *bla = g_new0( struct jabber_buddy_ask_data, 1 ); |
---|
| 274 | char *buf; |
---|
| 275 | |
---|
[0da65d5] | 276 | bla->ic = ic; |
---|
[8e5e2e9] | 277 | bla->handle = g_strdup( handle ); |
---|
| 278 | |
---|
| 279 | buf = g_strdup_printf( "The user %s wants to add you to his/her buddy list.", handle ); |
---|
[84b045d] | 280 | imcb_ask( ic, buf, bla, jabber_buddy_ask_yes, jabber_buddy_ask_no ); |
---|
[6266fca] | 281 | g_free( buf ); |
---|
[8e5e2e9] | 282 | } |
---|
[6a1128d] | 283 | |
---|
[0d3f30f] | 284 | /* Returns a new string. Don't leak it! */ |
---|
[e35d1a1] | 285 | char *jabber_normalize( const char *orig ) |
---|
[0d3f30f] | 286 | { |
---|
| 287 | int len, i; |
---|
| 288 | char *new; |
---|
| 289 | |
---|
| 290 | len = strlen( orig ); |
---|
| 291 | new = g_new( char, len + 1 ); |
---|
| 292 | for( i = 0; i < len; i ++ ) |
---|
| 293 | new[i] = tolower( orig[i] ); |
---|
| 294 | |
---|
| 295 | new[i] = 0; |
---|
| 296 | return new; |
---|
| 297 | } |
---|
| 298 | |
---|
[6a1128d] | 299 | /* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a |
---|
[0d3f30f] | 300 | FULL jid or if we already have this buddy/resource. XXX: No, great, actually |
---|
| 301 | buddies from transports don't (usually) have resources. So we'll really have |
---|
| 302 | to deal with that properly. Set their ->resource property to NULL. Do *NOT* |
---|
| 303 | allow to mix this stuff, though... */ |
---|
[0da65d5] | 304 | struct jabber_buddy *jabber_buddy_add( struct im_connection *ic, char *full_jid_ ) |
---|
[6a1128d] | 305 | { |
---|
[0da65d5] | 306 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 307 | struct jabber_buddy *bud, *new, *bi; |
---|
[0d3f30f] | 308 | char *s, *full_jid; |
---|
[6a1128d] | 309 | |
---|
[0d3f30f] | 310 | full_jid = jabber_normalize( full_jid_ ); |
---|
| 311 | |
---|
| 312 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
| 313 | *s = 0; |
---|
[6a1128d] | 314 | |
---|
| 315 | new = g_new0( struct jabber_buddy, 1 ); |
---|
| 316 | |
---|
| 317 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
| 318 | { |
---|
[0d3f30f] | 319 | /* If this is a transport buddy or whatever, it can't have more |
---|
| 320 | than one instance, so this is always wrong: */ |
---|
| 321 | if( s == NULL || bud->resource == NULL ) |
---|
| 322 | { |
---|
| 323 | if( s ) *s = '/'; |
---|
| 324 | g_free( new ); |
---|
| 325 | g_free( full_jid ); |
---|
| 326 | return NULL; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | new->bare_jid = bud->bare_jid; |
---|
[6a1128d] | 330 | |
---|
| 331 | /* We already have another resource for this buddy, add the |
---|
| 332 | new one to the list. */ |
---|
| 333 | for( bi = bud; bi; bi = bi->next ) |
---|
| 334 | { |
---|
[0d3f30f] | 335 | /* Check for dupes. */ |
---|
| 336 | if( g_strcasecmp( bi->resource, s + 1 ) == 0 ) |
---|
[6a1128d] | 337 | { |
---|
| 338 | *s = '/'; |
---|
| 339 | g_free( new ); |
---|
[0d3f30f] | 340 | g_free( full_jid ); |
---|
[6a1128d] | 341 | return NULL; |
---|
| 342 | } |
---|
| 343 | /* Append the new item to the list. */ |
---|
| 344 | else if( bi->next == NULL ) |
---|
| 345 | { |
---|
| 346 | bi->next = new; |
---|
| 347 | break; |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | else |
---|
| 352 | { |
---|
[b9f8b87] | 353 | /* Keep in mind that full_jid currently isn't really |
---|
| 354 | a full JID... */ |
---|
[0d3f30f] | 355 | new->bare_jid = g_strdup( full_jid ); |
---|
| 356 | g_hash_table_insert( jd->buddies, new->bare_jid, new ); |
---|
[6a1128d] | 357 | } |
---|
| 358 | |
---|
[0d3f30f] | 359 | if( s ) |
---|
| 360 | { |
---|
| 361 | *s = '/'; |
---|
| 362 | new->full_jid = full_jid; |
---|
| 363 | new->resource = strchr( new->full_jid, '/' ) + 1; |
---|
| 364 | } |
---|
| 365 | else |
---|
| 366 | { |
---|
| 367 | /* Let's waste some more bytes of RAM instead of to make |
---|
[b9f8b87] | 368 | memory management a total disaster here. And it saves |
---|
| 369 | me one g_free() call in this function. :-P */ |
---|
[0d3f30f] | 370 | new->full_jid = full_jid; |
---|
| 371 | } |
---|
[6a1128d] | 372 | |
---|
| 373 | return new; |
---|
| 374 | } |
---|
| 375 | |
---|
[788a1af] | 376 | /* Finds a buddy from our structures. Can find both full- and bare JIDs. When |
---|
| 377 | asked for a bare JID, it uses the "resource_select" setting to see which |
---|
| 378 | resource to pick. */ |
---|
[0da65d5] | 379 | struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags ) |
---|
[6a1128d] | 380 | { |
---|
[0da65d5] | 381 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 382 | struct jabber_buddy *bud; |
---|
[0d3f30f] | 383 | char *s, *jid; |
---|
| 384 | |
---|
| 385 | jid = jabber_normalize( jid_ ); |
---|
[6a1128d] | 386 | |
---|
| 387 | if( ( s = strchr( jid, '/' ) ) ) |
---|
| 388 | { |
---|
[e35d1a1] | 389 | int none_found = 0; |
---|
| 390 | |
---|
[6a1128d] | 391 | *s = 0; |
---|
| 392 | if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) ) |
---|
[0d3f30f] | 393 | { |
---|
[0adce21] | 394 | /* Just return the first one for this bare JID. */ |
---|
| 395 | if( flags & GET_BUDDY_FIRST ) |
---|
| 396 | { |
---|
| 397 | *s = '/'; |
---|
| 398 | g_free( jid ); |
---|
| 399 | return bud; |
---|
| 400 | } |
---|
| 401 | |
---|
[0d3f30f] | 402 | /* Is this one of those no-resource buddies? */ |
---|
| 403 | if( bud->resource == NULL ) |
---|
| 404 | { |
---|
[0adce21] | 405 | *s = '/'; |
---|
[16b5f86] | 406 | g_free( jid ); |
---|
| 407 | return NULL; |
---|
[0d3f30f] | 408 | } |
---|
[0adce21] | 409 | |
---|
| 410 | /* See if there's an exact match. */ |
---|
| 411 | for( ; bud; bud = bud->next ) |
---|
| 412 | if( g_strcasecmp( bud->resource, s + 1 ) == 0 ) |
---|
| 413 | break; |
---|
[0d3f30f] | 414 | } |
---|
[e35d1a1] | 415 | else |
---|
| 416 | { |
---|
| 417 | /* This hack is there to make sure that O_CREAT will |
---|
| 418 | work if there's already another resouce present |
---|
| 419 | for this JID, even if it's an unknown buddy. This |
---|
| 420 | is done to handle conferences properly. */ |
---|
| 421 | none_found = 1; |
---|
[0adce21] | 422 | /* TODO(wilmer): Find out what I was thinking when I |
---|
| 423 | wrote this??? And then fix it. This makes me sad... */ |
---|
[e35d1a1] | 424 | } |
---|
[0d3f30f] | 425 | |
---|
[e35d1a1] | 426 | if( bud == NULL && ( flags & GET_BUDDY_CREAT ) && ( imcb_find_buddy( ic, jid ) || !none_found ) ) |
---|
[16b5f86] | 427 | { |
---|
| 428 | *s = '/'; |
---|
[0da65d5] | 429 | bud = jabber_buddy_add( ic, jid ); |
---|
[16b5f86] | 430 | } |
---|
[0d3f30f] | 431 | |
---|
| 432 | g_free( jid ); |
---|
| 433 | return bud; |
---|
[6a1128d] | 434 | } |
---|
| 435 | else |
---|
| 436 | { |
---|
[a21a8ac] | 437 | struct jabber_buddy *best_prio, *best_time; |
---|
| 438 | char *set; |
---|
| 439 | |
---|
[0d3f30f] | 440 | bud = g_hash_table_lookup( jd->buddies, jid ); |
---|
| 441 | |
---|
| 442 | g_free( jid ); |
---|
| 443 | |
---|
| 444 | if( bud == NULL ) |
---|
[16b5f86] | 445 | /* No match. Create it now? */ |
---|
[f0cb961] | 446 | return ( ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid_ ) ) ? |
---|
[0da65d5] | 447 | jabber_buddy_add( ic, jid_ ) : NULL; |
---|
[16b5f86] | 448 | else if( bud->resource && ( flags & GET_BUDDY_EXACT ) ) |
---|
| 449 | /* We want an exact match, so in thise case there shouldn't be a /resource. */ |
---|
| 450 | return NULL; |
---|
[0d3f30f] | 451 | else if( ( bud->resource == NULL || bud->next == NULL ) ) |
---|
[16b5f86] | 452 | /* No need for selection if there's only one option. */ |
---|
[0d3f30f] | 453 | return bud; |
---|
[0adce21] | 454 | else if( flags & GET_BUDDY_FIRST ) |
---|
| 455 | /* Looks like the caller doesn't care about details. */ |
---|
| 456 | return bud; |
---|
[0d3f30f] | 457 | |
---|
| 458 | best_prio = best_time = bud; |
---|
[a21a8ac] | 459 | for( ; bud; bud = bud->next ) |
---|
| 460 | { |
---|
| 461 | if( bud->priority > best_prio->priority ) |
---|
| 462 | best_prio = bud; |
---|
| 463 | if( bud->last_act > best_time->last_act ) |
---|
| 464 | best_time = bud; |
---|
| 465 | } |
---|
| 466 | |
---|
[0da65d5] | 467 | if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL ) |
---|
[a21a8ac] | 468 | return NULL; |
---|
| 469 | else if( strcmp( set, "activity" ) == 0 ) |
---|
| 470 | return best_time; |
---|
| 471 | else /* if( strcmp( set, "priority" ) == 0 ) */ |
---|
| 472 | return best_prio; |
---|
[6a1128d] | 473 | } |
---|
| 474 | } |
---|
| 475 | |
---|
[b9f8b87] | 476 | /* I'm keeping a separate ext_jid attribute to save a JID that makes sense |
---|
| 477 | to export to BitlBee. This is mainly for groupchats right now. It's |
---|
| 478 | a bit of a hack, but I just think having the user nickname in the hostname |
---|
| 479 | part of the hostmask doesn't look nice on IRC. Normally you can convert |
---|
| 480 | a normal JID to ext_jid by swapping the part before and after the / and |
---|
| 481 | replacing the / with a =. But there should be some stripping (@s are |
---|
| 482 | allowed in Jabber nicks...). */ |
---|
| 483 | struct jabber_buddy *jabber_buddy_by_ext_jid( struct im_connection *ic, char *jid_, get_buddy_flags_t flags ) |
---|
| 484 | { |
---|
| 485 | struct jabber_buddy *bud; |
---|
| 486 | char *s, *jid; |
---|
| 487 | |
---|
| 488 | jid = jabber_normalize( jid_ ); |
---|
| 489 | |
---|
| 490 | if( ( s = strchr( jid, '=' ) ) == NULL ) |
---|
| 491 | return NULL; |
---|
| 492 | |
---|
| 493 | for( bud = jabber_buddy_by_jid( ic, s + 1, GET_BUDDY_FIRST ); bud; bud = bud->next ) |
---|
| 494 | { |
---|
| 495 | /* Hmmm, could happen if not all people in the chat are anonymized? */ |
---|
| 496 | if( bud->ext_jid == NULL ) |
---|
| 497 | continue; |
---|
| 498 | |
---|
| 499 | if( strcmp( bud->ext_jid, jid ) == 0 ) |
---|
| 500 | break; |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | g_free( jid ); |
---|
| 504 | |
---|
| 505 | return bud; |
---|
| 506 | } |
---|
| 507 | |
---|
[788a1af] | 508 | /* Remove one specific full JID from our list. Use this when a buddy goes |
---|
[0d3f30f] | 509 | off-line (because (s)he can still be online from a different location. |
---|
| 510 | XXX: See above, we should accept bare JIDs too... */ |
---|
[0da65d5] | 511 | int jabber_buddy_remove( struct im_connection *ic, char *full_jid_ ) |
---|
[6a1128d] | 512 | { |
---|
[0da65d5] | 513 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 514 | struct jabber_buddy *bud, *prev, *bi; |
---|
[0d3f30f] | 515 | char *s, *full_jid; |
---|
[6a1128d] | 516 | |
---|
[0d3f30f] | 517 | full_jid = jabber_normalize( full_jid_ ); |
---|
| 518 | |
---|
| 519 | if( ( s = strchr( full_jid, '/' ) ) ) |
---|
| 520 | *s = 0; |
---|
[6a1128d] | 521 | |
---|
| 522 | if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) |
---|
| 523 | { |
---|
| 524 | /* If there's only one item in the list (and if the resource |
---|
| 525 | matches), removing it is simple. (And the hash reference |
---|
| 526 | should be removed too!) */ |
---|
[0d3f30f] | 527 | if( bud->next == NULL && ( ( s == NULL || bud->resource == NULL ) || g_strcasecmp( bud->resource, s + 1 ) == 0 ) ) |
---|
[6a1128d] | 528 | { |
---|
[0d3f30f] | 529 | g_hash_table_remove( jd->buddies, bud->bare_jid ); |
---|
| 530 | g_free( bud->bare_jid ); |
---|
[6286f80] | 531 | g_free( bud->ext_jid ); |
---|
[a21a8ac] | 532 | g_free( bud->full_jid ); |
---|
[6a1128d] | 533 | g_free( bud->away_message ); |
---|
| 534 | g_free( bud ); |
---|
[0d3f30f] | 535 | |
---|
| 536 | g_free( full_jid ); |
---|
| 537 | |
---|
| 538 | return 1; |
---|
| 539 | } |
---|
| 540 | else if( s == NULL || bud->resource == NULL ) |
---|
| 541 | { |
---|
| 542 | /* Tried to remove a bare JID while this JID does seem |
---|
| 543 | to have resources... (Or the opposite.) *sigh* */ |
---|
| 544 | g_free( full_jid ); |
---|
| 545 | return 0; |
---|
[6a1128d] | 546 | } |
---|
| 547 | else |
---|
| 548 | { |
---|
| 549 | for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next ) |
---|
[0d3f30f] | 550 | if( g_strcasecmp( bi->resource, s + 1 ) == 0 ) |
---|
[6a1128d] | 551 | break; |
---|
| 552 | |
---|
[0d3f30f] | 553 | g_free( full_jid ); |
---|
| 554 | |
---|
[6a1128d] | 555 | if( bi ) |
---|
| 556 | { |
---|
| 557 | if( prev ) |
---|
| 558 | prev->next = bi->next; |
---|
| 559 | else |
---|
| 560 | /* The hash table should point at the second |
---|
| 561 | item, because we're removing the first. */ |
---|
[0d3f30f] | 562 | g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next ); |
---|
[6a1128d] | 563 | |
---|
[6286f80] | 564 | g_free( bi->ext_jid ); |
---|
[a21a8ac] | 565 | g_free( bi->full_jid ); |
---|
[6a1128d] | 566 | g_free( bi->away_message ); |
---|
| 567 | g_free( bi ); |
---|
[0d3f30f] | 568 | |
---|
| 569 | return 1; |
---|
[6a1128d] | 570 | } |
---|
| 571 | else |
---|
| 572 | { |
---|
| 573 | return 0; |
---|
| 574 | } |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | else |
---|
| 578 | { |
---|
[0d3f30f] | 579 | g_free( full_jid ); |
---|
[6a1128d] | 580 | return 0; |
---|
| 581 | } |
---|
| 582 | } |
---|
[788a1af] | 583 | |
---|
| 584 | /* Remove a buddy completely; removes all resources that belong to the |
---|
| 585 | specified bare JID. Use this when removing someone from the contact |
---|
| 586 | list, for example. */ |
---|
[9da0bbf] | 587 | int jabber_buddy_remove_bare( struct im_connection *ic, char *bare_jid ) |
---|
[788a1af] | 588 | { |
---|
[0da65d5] | 589 | struct jabber_data *jd = ic->proto_data; |
---|
[788a1af] | 590 | struct jabber_buddy *bud, *next; |
---|
| 591 | |
---|
[9da0bbf] | 592 | if( strchr( bare_jid, '/' ) ) |
---|
[788a1af] | 593 | return 0; |
---|
| 594 | |
---|
[9da0bbf] | 595 | if( ( bud = jabber_buddy_by_jid( ic, bare_jid, GET_BUDDY_FIRST ) ) ) |
---|
[788a1af] | 596 | { |
---|
| 597 | /* Most important: Remove the hash reference. We don't know |
---|
| 598 | this buddy anymore. */ |
---|
[0d3f30f] | 599 | g_hash_table_remove( jd->buddies, bud->bare_jid ); |
---|
[9da0bbf] | 600 | g_free( bud->bare_jid ); |
---|
[788a1af] | 601 | |
---|
| 602 | /* Deallocate the linked list of resources. */ |
---|
| 603 | while( bud ) |
---|
| 604 | { |
---|
[9da0bbf] | 605 | /* ext_jid && anonymous means that this buddy is |
---|
| 606 | specific to one groupchat (the one we're |
---|
| 607 | currently cleaning up) so it can be deleted |
---|
| 608 | completely. */ |
---|
| 609 | if( bud->ext_jid && bud->flags & JBFLAG_IS_ANONYMOUS ) |
---|
| 610 | imcb_remove_buddy( ic, bud->ext_jid, NULL ); |
---|
| 611 | |
---|
[788a1af] | 612 | next = bud->next; |
---|
[6286f80] | 613 | g_free( bud->ext_jid ); |
---|
[788a1af] | 614 | g_free( bud->full_jid ); |
---|
| 615 | g_free( bud->away_message ); |
---|
| 616 | g_free( bud ); |
---|
| 617 | bud = next; |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | return 1; |
---|
| 621 | } |
---|
| 622 | else |
---|
| 623 | { |
---|
| 624 | return 0; |
---|
| 625 | } |
---|
| 626 | } |
---|
[e35d1a1] | 627 | |
---|
[43671b9] | 628 | time_t jabber_get_timestamp( struct xt_node *xt ) |
---|
| 629 | { |
---|
| 630 | struct tm tp, utc; |
---|
| 631 | struct xt_node *c; |
---|
| 632 | time_t res, tres; |
---|
| 633 | char *s = NULL; |
---|
| 634 | |
---|
| 635 | for( c = xt->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) |
---|
| 636 | { |
---|
| 637 | if( ( s = xt_find_attr( c, "xmlns" ) ) && strcmp( s, XMLNS_DELAY ) == 0 ) |
---|
| 638 | break; |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | if( !c || !( s = xt_find_attr( c, "stamp" ) ) ) |
---|
| 642 | return 0; |
---|
| 643 | |
---|
| 644 | memset( &tp, 0, sizeof( tp ) ); |
---|
| 645 | if( sscanf( s, "%4d%2d%2dT%2d:%2d:%2d", &tp.tm_year, &tp.tm_mon, &tp.tm_mday, |
---|
| 646 | &tp.tm_hour, &tp.tm_min, &tp.tm_sec ) != 6 ) |
---|
| 647 | return 0; |
---|
| 648 | |
---|
| 649 | tp.tm_year -= 1900; |
---|
| 650 | tp.tm_mon --; |
---|
| 651 | tp.tm_isdst = -1; /* GRRRRRRRRRRR */ |
---|
| 652 | |
---|
| 653 | res = mktime( &tp ); |
---|
| 654 | /* Problem is, mktime() just gave us the GMT timestamp for the |
---|
| 655 | given local time... While the given time WAS NOT local. So |
---|
| 656 | we should fix this now. |
---|
| 657 | |
---|
| 658 | Now I could choose between messing with environment variables |
---|
| 659 | (kludgy) or using timegm() (not portable)... Or doing the |
---|
| 660 | following, which I actually prefer... */ |
---|
| 661 | gmtime_r( &res, &utc ); |
---|
| 662 | utc.tm_isdst = -1; /* Once more: GRRRRRRRRRRRRRRRRRR!!! */ |
---|
| 663 | if( utc.tm_hour == tp.tm_hour && utc.tm_min == tp.tm_min ) |
---|
| 664 | /* Sweet! We're in UTC right now... */ |
---|
| 665 | return res; |
---|
| 666 | |
---|
| 667 | tres = mktime( &utc ); |
---|
| 668 | res += res - tres; |
---|
| 669 | |
---|
| 670 | /* Yes, this is a hack. And it will go wrong around DST changes. |
---|
| 671 | BUT this is more likely to be threadsafe than messing with |
---|
| 672 | environment variables, and possibly more portable... */ |
---|
| 673 | |
---|
| 674 | return res; |
---|
| 675 | } |
---|
[1baaef8] | 676 | |
---|
| 677 | struct jabber_error *jabber_error_parse( struct xt_node *node, char *xmlns ) |
---|
| 678 | { |
---|
[5bd21df] | 679 | struct jabber_error *err; |
---|
[1baaef8] | 680 | struct xt_node *c; |
---|
| 681 | char *s; |
---|
| 682 | |
---|
[5bd21df] | 683 | if( node == NULL ) |
---|
| 684 | return NULL; |
---|
| 685 | |
---|
| 686 | err = g_new0( struct jabber_error, 1 ); |
---|
[1baaef8] | 687 | err->type = xt_find_attr( node, "type" ); |
---|
| 688 | |
---|
| 689 | for( c = node->children; c; c = c->next ) |
---|
| 690 | { |
---|
| 691 | if( !( s = xt_find_attr( c, "xmlns" ) ) || |
---|
| 692 | strcmp( s, xmlns ) != 0 ) |
---|
| 693 | continue; |
---|
| 694 | |
---|
| 695 | if( strcmp( c->name, "text" ) != 0 ) |
---|
| 696 | { |
---|
| 697 | err->code = c->name; |
---|
| 698 | } |
---|
| 699 | /* Only use the text if it doesn't have an xml:lang attribute, |
---|
| 700 | if it's empty or if it's set to something English. */ |
---|
| 701 | else if( !( s = xt_find_attr( c, "xml:lang" ) ) || |
---|
| 702 | !*s || strncmp( s, "en", 2 ) == 0 ) |
---|
| 703 | { |
---|
| 704 | err->text = c->text; |
---|
| 705 | } |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | return err; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | void jabber_error_free( struct jabber_error *err ) |
---|
| 712 | { |
---|
| 713 | g_free( err ); |
---|
| 714 | } |
---|