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