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