[f06894d] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - IQ packets * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2006-2012 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" |
---|
[77bfd07] | 25 | #include "sha1.h" |
---|
[f06894d] | 26 | |
---|
[5ebff60] | 27 | static xt_status jabber_parse_roster(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
| 28 | static xt_status jabber_iq_display_vcard(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[dd43c62] | 29 | static xt_status jabber_gmail_handle_new(struct im_connection *ic, struct xt_node *node); |
---|
[fa8f57b] | 30 | static xt_status jabber_iq_carbons_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[abbd8ed] | 31 | |
---|
[5ebff60] | 32 | xt_status jabber_pkt_iq(struct xt_node *node, gpointer data) |
---|
[f06894d] | 33 | { |
---|
[0da65d5] | 34 | struct im_connection *ic = data; |
---|
[68286eb] | 35 | struct jabber_data *jd = ic->proto_data; |
---|
[58b5f62] | 36 | struct xt_node *c, *reply = NULL; |
---|
[861c199] | 37 | char *type, *s; |
---|
[259edd4] | 38 | int st, pack = 1; |
---|
[5ebff60] | 39 | |
---|
| 40 | type = xt_find_attr(node, "type"); |
---|
| 41 | |
---|
| 42 | if (!type) { |
---|
| 43 | imcb_error(ic, "Received IQ packet without type."); |
---|
| 44 | imc_logout(ic, TRUE); |
---|
[861c199] | 45 | return XT_ABORT; |
---|
| 46 | } |
---|
[5ebff60] | 47 | |
---|
| 48 | if (strcmp(type, "result") == 0 || strcmp(type, "error") == 0) { |
---|
| 49 | return jabber_cache_handle_packet(ic, node); |
---|
| 50 | } else if (strcmp(type, "get") == 0) { |
---|
| 51 | if (!((c = xt_find_node(node->children, "query")) || |
---|
| 52 | (c = xt_find_node(node->children, "ping")) || |
---|
| 53 | (c = xt_find_node(node->children, "time"))) || |
---|
| 54 | !(s = xt_find_attr(c, "xmlns"))) { |
---|
[74349eb] | 55 | /* Sigh. Who decided to suddenly invent new elements |
---|
| 56 | instead of just sticking with <query/>? */ |
---|
[58b5f62] | 57 | return XT_HANDLED; |
---|
| 58 | } |
---|
[5ebff60] | 59 | |
---|
| 60 | reply = xt_new_node("query", NULL, NULL); |
---|
| 61 | xt_add_attr(reply, "xmlns", s); |
---|
| 62 | |
---|
[58b5f62] | 63 | /* Of course this is a very essential query to support. ;-) */ |
---|
[5ebff60] | 64 | if (strcmp(s, XMLNS_VERSION) == 0) { |
---|
| 65 | xt_add_child(reply, xt_new_node("name", set_getstr(&ic->acc->set, "user_agent"), NULL)); |
---|
| 66 | xt_add_child(reply, xt_new_node("version", BITLBEE_VERSION, NULL)); |
---|
| 67 | xt_add_child(reply, xt_new_node("os", ARCH, NULL)); |
---|
| 68 | } else if (strcmp(s, XMLNS_TIME_OLD) == 0) { |
---|
[a4effbf] | 69 | time_t time_ep; |
---|
| 70 | char buf[1024]; |
---|
[5ebff60] | 71 | |
---|
| 72 | buf[sizeof(buf) - 1] = 0; |
---|
| 73 | time_ep = time(NULL); |
---|
| 74 | |
---|
| 75 | strftime(buf, sizeof(buf) - 1, "%Y%m%dT%H:%M:%S", gmtime(&time_ep)); |
---|
| 76 | xt_add_child(reply, xt_new_node("utc", buf, NULL)); |
---|
| 77 | |
---|
| 78 | strftime(buf, sizeof(buf) - 1, "%Z", localtime(&time_ep)); |
---|
| 79 | xt_add_child(reply, xt_new_node("tz", buf, NULL)); |
---|
| 80 | } else if (strcmp(s, XMLNS_TIME) == 0) { |
---|
[d76e12f] | 81 | time_t time_ep; |
---|
| 82 | char buf[1024]; |
---|
[5ebff60] | 83 | |
---|
| 84 | buf[sizeof(buf) - 1] = 0; |
---|
| 85 | time_ep = time(NULL); |
---|
| 86 | |
---|
| 87 | xt_free_node(reply); |
---|
| 88 | reply = xt_new_node("time", NULL, NULL); |
---|
| 89 | xt_add_attr(reply, "xmlns", XMLNS_TIME); |
---|
| 90 | |
---|
| 91 | strftime(buf, sizeof(buf) - 1, "%Y%m%dT%H:%M:%SZ", gmtime(&time_ep)); |
---|
| 92 | xt_add_child(reply, xt_new_node("utc", buf, NULL)); |
---|
| 93 | |
---|
| 94 | strftime(buf, sizeof(buf) - 1, "%z", localtime(&time_ep)); |
---|
| 95 | if (strlen(buf) >= 5) { |
---|
[d76e12f] | 96 | buf[6] = '\0'; |
---|
| 97 | buf[5] = buf[4]; |
---|
| 98 | buf[4] = buf[3]; |
---|
| 99 | buf[3] = ':'; |
---|
| 100 | } |
---|
[5ebff60] | 101 | xt_add_child(reply, xt_new_node("tzo", buf, NULL)); |
---|
| 102 | } else if (strcmp(s, XMLNS_PING) == 0) { |
---|
| 103 | xt_free_node(reply); |
---|
| 104 | reply = jabber_make_packet("iq", "result", xt_find_attr(node, "from"), NULL); |
---|
| 105 | if ((s = xt_find_attr(node, "id"))) { |
---|
| 106 | xt_add_attr(reply, "id", s); |
---|
| 107 | } |
---|
[eded1f7] | 108 | pack = 0; |
---|
[5ebff60] | 109 | } else if (strcmp(s, XMLNS_DISCO_INFO) == 0) { |
---|
[1ba7e8f] | 110 | const char *features[] = { XMLNS_DISCO_INFO, |
---|
[5ebff60] | 111 | XMLNS_VERSION, |
---|
| 112 | XMLNS_TIME_OLD, |
---|
| 113 | XMLNS_TIME, |
---|
| 114 | XMLNS_CHATSTATES, |
---|
| 115 | XMLNS_MUC, |
---|
| 116 | XMLNS_PING, |
---|
| 117 | XMLNS_RECEIPTS, |
---|
| 118 | XMLNS_SI, |
---|
| 119 | XMLNS_BYTESTREAMS, |
---|
| 120 | XMLNS_FILETRANSFER, |
---|
[fa8f57b] | 121 | XMLNS_CARBONS, |
---|
[5ebff60] | 122 | NULL }; |
---|
[40ef702] | 123 | const char **f; |
---|
[5ebff60] | 124 | |
---|
| 125 | c = xt_new_node("identity", NULL, NULL); |
---|
| 126 | xt_add_attr(c, "category", "client"); |
---|
| 127 | xt_add_attr(c, "type", "pc"); |
---|
| 128 | xt_add_attr(c, "name", set_getstr(&ic->acc->set, "user_agent")); |
---|
| 129 | xt_add_child(reply, c); |
---|
| 130 | |
---|
| 131 | for (f = features; *f; f++) { |
---|
| 132 | c = xt_new_node("feature", NULL, NULL); |
---|
| 133 | xt_add_attr(c, "var", *f); |
---|
| 134 | xt_add_child(reply, c); |
---|
[40ef702] | 135 | } |
---|
[5ebff60] | 136 | } else { |
---|
| 137 | xt_free_node(reply); |
---|
| 138 | reply = jabber_make_error_packet(node, "feature-not-implemented", "cancel", NULL); |
---|
[259edd4] | 139 | pack = 0; |
---|
[58b5f62] | 140 | } |
---|
[5ebff60] | 141 | } else if (strcmp(type, "set") == 0) { |
---|
| 142 | if ((c = xt_find_node(node->children, "si")) && |
---|
| 143 | (s = xt_find_attr(c, "xmlns")) && |
---|
| 144 | (strcmp(s, XMLNS_SI) == 0)) { |
---|
| 145 | return jabber_si_handle_request(ic, node, c); |
---|
[dd43c62] | 146 | } else if ((c = xt_find_node(node->children, "new-mail")) && |
---|
| 147 | (s = xt_find_attr(c, "xmlns")) && |
---|
| 148 | (strcmp(s, XMLNS_GMAILNOTIFY) == 0)) { |
---|
| 149 | return jabber_gmail_handle_new(ic, node); |
---|
[5ebff60] | 150 | } else if (!(c = xt_find_node(node->children, "query")) || |
---|
| 151 | !(s = xt_find_attr(c, "xmlns"))) { |
---|
[dfa41a4] | 152 | return XT_HANDLED; |
---|
[5ebff60] | 153 | } else if (strcmp(s, XMLNS_ROSTER) == 0) { |
---|
| 154 | /* This is a roster push. XMPP servers send this when someone |
---|
| 155 | was added to (or removed from) the buddy list. AFAIK they're |
---|
| 156 | sent even if we added this buddy in our own session. */ |
---|
| 157 | int bare_len = strlen(jd->me); |
---|
| 158 | |
---|
| 159 | if ((s = xt_find_attr(node, "from")) == NULL || |
---|
| 160 | (strncmp(s, jd->me, bare_len) == 0 && |
---|
| 161 | (s[bare_len] == 0 || s[bare_len] == '/'))) { |
---|
| 162 | jabber_parse_roster(ic, node, NULL); |
---|
| 163 | |
---|
[abbd8ed] | 164 | /* Should we generate a reply here? Don't think it's |
---|
| 165 | very important... */ |
---|
[5ebff60] | 166 | } else { |
---|
| 167 | imcb_log(ic, "Warning: %s tried to fake a roster push!", s ? s : "(unknown)"); |
---|
| 168 | |
---|
| 169 | xt_free_node(reply); |
---|
| 170 | reply = jabber_make_error_packet(node, "not-allowed", "cancel", NULL); |
---|
[abbd8ed] | 171 | pack = 0; |
---|
| 172 | } |
---|
[5ebff60] | 173 | } else if (strcmp(s, XMLNS_BYTESTREAMS) == 0) { |
---|
[1c3008a] | 174 | /* Bytestream Request (stage 2 of file transfer) */ |
---|
[5ebff60] | 175 | return jabber_bs_recv_request(ic, node, c); |
---|
| 176 | } else { |
---|
| 177 | xt_free_node(reply); |
---|
| 178 | reply = jabber_make_error_packet(node, "feature-not-implemented", "cancel", NULL); |
---|
[dfa41a4] | 179 | pack = 0; |
---|
| 180 | } |
---|
[259edd4] | 181 | } |
---|
[5ebff60] | 182 | |
---|
[259edd4] | 183 | /* If we recognized the xmlns and managed to generate a reply, |
---|
| 184 | finish and send it. */ |
---|
[5ebff60] | 185 | if (reply) { |
---|
[259edd4] | 186 | /* Normally we still have to pack it into an iq-result |
---|
| 187 | packet, but for errors, for example, we don't. */ |
---|
[5ebff60] | 188 | if (pack) { |
---|
| 189 | reply = jabber_make_packet("iq", "result", xt_find_attr(node, "from"), reply); |
---|
| 190 | if ((s = xt_find_attr(node, "id"))) { |
---|
| 191 | xt_add_attr(reply, "id", s); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | st = jabber_write_packet(ic, reply); |
---|
| 196 | xt_free_node(reply); |
---|
| 197 | if (!st) { |
---|
[259edd4] | 198 | return XT_ABORT; |
---|
[5ebff60] | 199 | } |
---|
[58b5f62] | 200 | } |
---|
[5ebff60] | 201 | |
---|
[861c199] | 202 | return XT_HANDLED; |
---|
| 203 | } |
---|
| 204 | |
---|
[5ebff60] | 205 | static xt_status jabber_do_iq_auth(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
| 206 | static xt_status jabber_finish_iq_auth(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[861c199] | 207 | |
---|
[5ebff60] | 208 | int jabber_init_iq_auth(struct im_connection *ic) |
---|
[861c199] | 209 | { |
---|
[0da65d5] | 210 | struct jabber_data *jd = ic->proto_data; |
---|
[861c199] | 211 | struct xt_node *node; |
---|
| 212 | int st; |
---|
[5ebff60] | 213 | |
---|
| 214 | node = xt_new_node("query", NULL, xt_new_node("username", jd->username, NULL)); |
---|
| 215 | xt_add_attr(node, "xmlns", XMLNS_AUTH); |
---|
| 216 | node = jabber_make_packet("iq", "get", NULL, node); |
---|
| 217 | |
---|
| 218 | jabber_cache_add(ic, node, jabber_do_iq_auth); |
---|
| 219 | st = jabber_write_packet(ic, node); |
---|
| 220 | |
---|
[861c199] | 221 | return st; |
---|
| 222 | } |
---|
| 223 | |
---|
[5ebff60] | 224 | static xt_status jabber_do_iq_auth(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[861c199] | 225 | { |
---|
[0da65d5] | 226 | struct jabber_data *jd = ic->proto_data; |
---|
[861c199] | 227 | struct xt_node *reply, *query; |
---|
| 228 | xt_status st; |
---|
| 229 | char *s; |
---|
[5ebff60] | 230 | |
---|
| 231 | if (!(query = xt_find_node(node->children, "query"))) { |
---|
| 232 | imcb_log(ic, "Warning: Received incomplete IQ packet while authenticating"); |
---|
| 233 | imc_logout(ic, FALSE); |
---|
[9bcbe48] | 234 | return XT_HANDLED; |
---|
| 235 | } |
---|
[5ebff60] | 236 | |
---|
[861c199] | 237 | /* Time to authenticate ourselves! */ |
---|
[5ebff60] | 238 | reply = xt_new_node("query", NULL, NULL); |
---|
| 239 | xt_add_attr(reply, "xmlns", XMLNS_AUTH); |
---|
| 240 | xt_add_child(reply, xt_new_node("username", jd->username, NULL)); |
---|
| 241 | xt_add_child(reply, xt_new_node("resource", set_getstr(&ic->acc->set, "resource"), NULL)); |
---|
| 242 | |
---|
| 243 | if (xt_find_node(query->children, "digest") && (s = xt_find_attr(jd->xt->root, "id"))) { |
---|
[861c199] | 244 | /* We can do digest authentication, it seems, and of |
---|
| 245 | course we prefer that. */ |
---|
[77bfd07] | 246 | sha1_state_t sha; |
---|
[e727608] | 247 | char hash_hex[41]; |
---|
[861c199] | 248 | unsigned char hash[20]; |
---|
| 249 | int i; |
---|
[5ebff60] | 250 | |
---|
| 251 | sha1_init(&sha); |
---|
| 252 | sha1_append(&sha, (unsigned char *) s, strlen(s)); |
---|
| 253 | sha1_append(&sha, (unsigned char *) ic->acc->pass, strlen(ic->acc->pass)); |
---|
| 254 | sha1_finish(&sha, hash); |
---|
| 255 | |
---|
| 256 | for (i = 0; i < 20; i++) { |
---|
| 257 | sprintf(hash_hex + i * 2, "%02x", hash[i]); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | xt_add_child(reply, xt_new_node("digest", hash_hex, NULL)); |
---|
| 261 | } else if (xt_find_node(query->children, "password")) { |
---|
[861c199] | 262 | /* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */ |
---|
[5ebff60] | 263 | xt_add_child(reply, xt_new_node("password", ic->acc->pass, NULL)); |
---|
| 264 | } else { |
---|
| 265 | xt_free_node(reply); |
---|
| 266 | |
---|
| 267 | imcb_error(ic, "Can't find suitable authentication method"); |
---|
| 268 | imc_logout(ic, FALSE); |
---|
[861c199] | 269 | return XT_ABORT; |
---|
[70f6aab8] | 270 | } |
---|
[5ebff60] | 271 | |
---|
| 272 | reply = jabber_make_packet("iq", "set", NULL, reply); |
---|
| 273 | jabber_cache_add(ic, reply, jabber_finish_iq_auth); |
---|
| 274 | st = jabber_write_packet(ic, reply); |
---|
| 275 | |
---|
[861c199] | 276 | return st ? XT_HANDLED : XT_ABORT; |
---|
| 277 | } |
---|
| 278 | |
---|
[5ebff60] | 279 | static xt_status jabber_finish_iq_auth(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[861c199] | 280 | { |
---|
[0da65d5] | 281 | struct jabber_data *jd = ic->proto_data; |
---|
[9bcbe48] | 282 | char *type; |
---|
[5ebff60] | 283 | |
---|
| 284 | if (!(type = xt_find_attr(node, "type"))) { |
---|
| 285 | imcb_log(ic, "Warning: Received incomplete IQ packet while authenticating"); |
---|
| 286 | imc_logout(ic, FALSE); |
---|
[9bcbe48] | 287 | return XT_HANDLED; |
---|
| 288 | } |
---|
[5ebff60] | 289 | |
---|
| 290 | if (strcmp(type, "error") == 0) { |
---|
| 291 | imcb_error(ic, "Authentication failure"); |
---|
| 292 | imc_logout(ic, FALSE); |
---|
[861c199] | 293 | return XT_ABORT; |
---|
[5ebff60] | 294 | } else if (strcmp(type, "result") == 0) { |
---|
[861c199] | 295 | /* This happens when we just successfully authenticated the |
---|
| 296 | old (non-SASL) way. */ |
---|
| 297 | jd->flags |= JFLAG_AUTHENTICATED; |
---|
[5ebff60] | 298 | if (!jabber_get_roster(ic)) { |
---|
[70f6aab8] | 299 | return XT_ABORT; |
---|
[5ebff60] | 300 | } |
---|
| 301 | if (!jabber_iq_disco_server(ic)) { |
---|
[bb2d198] | 302 | return XT_ABORT; |
---|
[5ebff60] | 303 | } |
---|
[70f6aab8] | 304 | } |
---|
[5ebff60] | 305 | |
---|
[f06894d] | 306 | return XT_HANDLED; |
---|
| 307 | } |
---|
| 308 | |
---|
[5ebff60] | 309 | xt_status jabber_pkt_bind_sess(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[21167d2] | 310 | { |
---|
[0da65d5] | 311 | struct jabber_data *jd = ic->proto_data; |
---|
[8fb1263] | 312 | struct xt_node *c, *reply = NULL; |
---|
[861c199] | 313 | char *s; |
---|
[5ebff60] | 314 | |
---|
| 315 | if (node && (c = xt_find_node(node->children, "bind"))) { |
---|
| 316 | c = xt_find_node(c->children, "jid"); |
---|
| 317 | if (!c || !c->text) { |
---|
[68286eb] | 318 | /* Server is crap, but this is no disaster. */ |
---|
[5ebff60] | 319 | } else if (jabber_compare_jid(jd->me, c->text) == 0) { |
---|
| 320 | s = strchr(c->text, '/'); |
---|
| 321 | if (s) { |
---|
[68286eb] | 322 | *s = '\0'; |
---|
[5ebff60] | 323 | } |
---|
| 324 | jabber_set_me(ic, c->text); |
---|
| 325 | if (s) { |
---|
[68286eb] | 326 | *s = '/'; |
---|
[5ebff60] | 327 | } |
---|
| 328 | } else if (c && c->text_len && (s = strchr(c->text, '/')) && |
---|
| 329 | strcmp(s + 1, set_getstr(&ic->acc->set, "resource")) != 0) { |
---|
| 330 | imcb_log(ic, "Server changed session resource string to `%s'", s + 1); |
---|
[68286eb] | 331 | } |
---|
[861c199] | 332 | } |
---|
[5ebff60] | 333 | |
---|
| 334 | if (jd->flags & JFLAG_WANT_BIND) { |
---|
| 335 | reply = xt_new_node("bind", NULL, xt_new_node("resource", set_getstr(&ic->acc->set, "resource"), NULL)); |
---|
| 336 | xt_add_attr(reply, "xmlns", XMLNS_BIND); |
---|
[315dd4c] | 337 | jd->flags &= ~JFLAG_WANT_BIND; |
---|
[5ebff60] | 338 | } else if (jd->flags & JFLAG_WANT_SESSION) { |
---|
| 339 | reply = xt_new_node("session", NULL, NULL); |
---|
| 340 | xt_add_attr(reply, "xmlns", XMLNS_SESSION); |
---|
[315dd4c] | 341 | jd->flags &= ~JFLAG_WANT_SESSION; |
---|
[861c199] | 342 | } |
---|
[5ebff60] | 343 | |
---|
| 344 | if (reply != NULL) { |
---|
| 345 | reply = jabber_make_packet("iq", "set", NULL, reply); |
---|
| 346 | jabber_cache_add(ic, reply, jabber_pkt_bind_sess); |
---|
| 347 | |
---|
| 348 | if (!jabber_write_packet(ic, reply)) { |
---|
[8fb1263] | 349 | return XT_ABORT; |
---|
[5ebff60] | 350 | } |
---|
[dd43c62] | 351 | if (jd->flags & JFLAG_GMAILNOTIFY && node == NULL) { |
---|
| 352 | jabber_iq_query_server(ic, jd->server, XMLNS_DISCO_INFO); |
---|
| 353 | } |
---|
[5ebff60] | 354 | } else if ((jd->flags & (JFLAG_WANT_BIND | JFLAG_WANT_SESSION)) == 0) { |
---|
| 355 | if (!jabber_get_roster(ic)) { |
---|
[861c199] | 356 | return XT_ABORT; |
---|
[5ebff60] | 357 | } |
---|
| 358 | if (!jabber_iq_disco_server(ic)) { |
---|
[bb2d198] | 359 | return XT_ABORT; |
---|
[5ebff60] | 360 | } |
---|
[861c199] | 361 | } |
---|
[5ebff60] | 362 | |
---|
[861c199] | 363 | return XT_HANDLED; |
---|
[21167d2] | 364 | } |
---|
[70f6aab8] | 365 | |
---|
[5ebff60] | 366 | int jabber_get_roster(struct im_connection *ic) |
---|
[70f6aab8] | 367 | { |
---|
| 368 | struct xt_node *node; |
---|
| 369 | int st; |
---|
[5ebff60] | 370 | |
---|
| 371 | imcb_log(ic, "Authenticated, requesting buddy list"); |
---|
| 372 | |
---|
| 373 | node = xt_new_node("query", NULL, NULL); |
---|
| 374 | xt_add_attr(node, "xmlns", XMLNS_ROSTER); |
---|
| 375 | node = jabber_make_packet("iq", "get", NULL, node); |
---|
| 376 | |
---|
| 377 | jabber_cache_add(ic, node, jabber_parse_roster); |
---|
| 378 | st = jabber_write_packet(ic, node); |
---|
| 379 | |
---|
[70f6aab8] | 380 | return st; |
---|
| 381 | } |
---|
[cfbb3a6] | 382 | |
---|
[dd43c62] | 383 | xt_status jabber_iq_query_gmail(struct im_connection *ic); |
---|
| 384 | |
---|
| 385 | static xt_status jabber_gmail_handle_new(struct im_connection *ic, struct xt_node *node) |
---|
| 386 | { |
---|
| 387 | struct xt_node *response; |
---|
| 388 | struct jabber_data *jd = ic->proto_data; |
---|
[df215a3] | 389 | |
---|
| 390 | response = jabber_make_packet("iq", "result", jd->me, NULL); |
---|
[dd43c62] | 391 | |
---|
| 392 | jabber_cache_add(ic, response, NULL); |
---|
| 393 | if (!jabber_write_packet(ic, response)) { |
---|
| 394 | return XT_ABORT; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | jabber_iq_query_gmail(ic); |
---|
| 398 | |
---|
| 399 | return XT_HANDLED; |
---|
| 400 | } |
---|
| 401 | |
---|
[5ebff60] | 402 | static xt_status jabber_parse_roster(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[861c199] | 403 | { |
---|
[40cfbc5] | 404 | struct jabber_data *jd = ic->proto_data; |
---|
[861c199] | 405 | struct xt_node *query, *c; |
---|
[5ebff60] | 406 | int initial = (orig != NULL); |
---|
| 407 | |
---|
| 408 | if (!(query = xt_find_node(node->children, "query"))) { |
---|
| 409 | imcb_log(ic, "Warning: Received NULL roster packet"); |
---|
[9bcbe48] | 410 | return XT_HANDLED; |
---|
| 411 | } |
---|
[5ebff60] | 412 | |
---|
[861c199] | 413 | c = query->children; |
---|
[5ebff60] | 414 | while ((c = xt_find_node(c, "item"))) { |
---|
| 415 | struct xt_node *group = xt_find_node(c->children, "group"); |
---|
| 416 | char *jid = xt_find_attr(c, "jid"); |
---|
| 417 | char *name = xt_find_attr(c, "name"); |
---|
| 418 | char *sub = xt_find_attr(c, "subscription"); |
---|
[40cfbc5] | 419 | char *mention_name = xt_find_attr(c, "mention_name"); |
---|
[5ebff60] | 420 | |
---|
| 421 | if (jid && sub) { |
---|
| 422 | if ((strcmp(sub, "both") == 0 || strcmp(sub, "to") == 0)) { |
---|
| 423 | imcb_add_buddy(ic, jid, (group && group->text_len) ? |
---|
| 424 | group->text : NULL); |
---|
| 425 | |
---|
| 426 | if (name) { |
---|
| 427 | imcb_rename_buddy(ic, jid, name); |
---|
| 428 | } |
---|
[40cfbc5] | 429 | |
---|
| 430 | /* This could also be used to set the full name as nick for fb/gtalk, |
---|
| 431 | * but i'm keeping the old (ugly?) default behavior just to be safe */ |
---|
| 432 | if (mention_name && (jd->flags & JFLAG_HIPCHAT)) { |
---|
| 433 | imcb_buddy_nick_hint(ic, jid, mention_name); |
---|
| 434 | } |
---|
[5ebff60] | 435 | } else if (strcmp(sub, "remove") == 0) { |
---|
| 436 | jabber_buddy_remove_bare(ic, jid); |
---|
| 437 | imcb_remove_buddy(ic, jid, NULL); |
---|
[abbd8ed] | 438 | } |
---|
| 439 | } |
---|
[5ebff60] | 440 | |
---|
[861c199] | 441 | c = c->next; |
---|
| 442 | } |
---|
[5ebff60] | 443 | |
---|
| 444 | if (initial) { |
---|
| 445 | imcb_connected(ic); |
---|
| 446 | } |
---|
| 447 | |
---|
[861c199] | 448 | return XT_HANDLED; |
---|
| 449 | } |
---|
| 450 | |
---|
[5ebff60] | 451 | int jabber_get_vcard(struct im_connection *ic, char *bare_jid) |
---|
[1991be6] | 452 | { |
---|
| 453 | struct xt_node *node; |
---|
[5ebff60] | 454 | |
---|
| 455 | if (strchr(bare_jid, '/')) { |
---|
| 456 | return 1; /* This was an error, but return 0 should only be done if the connection died... */ |
---|
| 457 | |
---|
| 458 | } |
---|
| 459 | node = xt_new_node("vCard", NULL, NULL); |
---|
| 460 | xt_add_attr(node, "xmlns", XMLNS_VCARD); |
---|
| 461 | node = jabber_make_packet("iq", "get", bare_jid, node); |
---|
| 462 | |
---|
| 463 | jabber_cache_add(ic, node, jabber_iq_display_vcard); |
---|
| 464 | return jabber_write_packet(ic, node); |
---|
[1991be6] | 465 | } |
---|
| 466 | |
---|
[5ebff60] | 467 | static xt_status jabber_iq_display_vcard(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[1991be6] | 468 | { |
---|
[0da65d5] | 469 | struct xt_node *vc, *c, *sc; /* subchild, ic is already in use ;-) */ |
---|
[1991be6] | 470 | GString *reply; |
---|
| 471 | char *s; |
---|
[5ebff60] | 472 | |
---|
| 473 | if ((s = xt_find_attr(node, "type")) == NULL || |
---|
| 474 | strcmp(s, "result") != 0 || |
---|
| 475 | (vc = xt_find_node(node->children, "vCard")) == NULL) { |
---|
| 476 | s = xt_find_attr(orig, "to"); /* If this returns NULL something's wrong.. */ |
---|
| 477 | imcb_log(ic, "Could not retrieve vCard of %s", s ? s : "(NULL)"); |
---|
[1991be6] | 478 | return XT_HANDLED; |
---|
| 479 | } |
---|
[5ebff60] | 480 | |
---|
| 481 | s = xt_find_attr(orig, "to"); |
---|
| 482 | reply = g_string_new("vCard information for "); |
---|
| 483 | reply = g_string_append(reply, s ? s : "(NULL)"); |
---|
| 484 | reply = g_string_append(reply, ":\n"); |
---|
| 485 | |
---|
[1991be6] | 486 | /* I hate this format, I really do... */ |
---|
[5ebff60] | 487 | |
---|
| 488 | if ((c = xt_find_node(vc->children, "FN")) && c->text_len) { |
---|
| 489 | g_string_append_printf(reply, "Name: %s\n", c->text); |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | if ((c = xt_find_node(vc->children, "N")) && c->children) { |
---|
| 493 | reply = g_string_append(reply, "Full name:"); |
---|
| 494 | |
---|
| 495 | if ((sc = xt_find_node(c->children, "PREFIX")) && sc->text_len) { |
---|
| 496 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 497 | } |
---|
| 498 | if ((sc = xt_find_node(c->children, "GIVEN")) && sc->text_len) { |
---|
| 499 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 500 | } |
---|
| 501 | if ((sc = xt_find_node(c->children, "MIDDLE")) && sc->text_len) { |
---|
| 502 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 503 | } |
---|
| 504 | if ((sc = xt_find_node(c->children, "FAMILY")) && sc->text_len) { |
---|
| 505 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 506 | } |
---|
| 507 | if ((sc = xt_find_node(c->children, "SUFFIX")) && sc->text_len) { |
---|
| 508 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | reply = g_string_append_c(reply, '\n'); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | if ((c = xt_find_node(vc->children, "NICKNAME")) && c->text_len) { |
---|
| 515 | g_string_append_printf(reply, "Nickname: %s\n", c->text); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | if ((c = xt_find_node(vc->children, "BDAY")) && c->text_len) { |
---|
| 519 | g_string_append_printf(reply, "Date of birth: %s\n", c->text); |
---|
| 520 | } |
---|
| 521 | |
---|
[1991be6] | 522 | /* Slightly alternative use of for... ;-) */ |
---|
[5ebff60] | 523 | for (c = vc->children; (c = xt_find_node(c, "EMAIL")); c = c->next) { |
---|
| 524 | if ((sc = xt_find_node(c->children, "USERID")) == NULL || sc->text_len == 0) { |
---|
[1991be6] | 525 | continue; |
---|
[5ebff60] | 526 | } |
---|
| 527 | |
---|
| 528 | if (xt_find_node(c->children, "HOME")) { |
---|
[1991be6] | 529 | s = "Home"; |
---|
[5ebff60] | 530 | } else if (xt_find_node(c->children, "WORK")) { |
---|
[1991be6] | 531 | s = "Work"; |
---|
[5ebff60] | 532 | } else { |
---|
[1991be6] | 533 | s = "Misc."; |
---|
[5ebff60] | 534 | } |
---|
| 535 | |
---|
| 536 | g_string_append_printf(reply, "%s e-mail address: %s\n", s, sc->text); |
---|
[1991be6] | 537 | } |
---|
[5ebff60] | 538 | |
---|
| 539 | if ((c = xt_find_node(vc->children, "URL")) && c->text_len) { |
---|
| 540 | g_string_append_printf(reply, "Homepage: %s\n", c->text); |
---|
| 541 | } |
---|
| 542 | |
---|
[1991be6] | 543 | /* Slightly alternative use of for... ;-) */ |
---|
[5ebff60] | 544 | for (c = vc->children; (c = xt_find_node(c, "ADR")); c = c->next) { |
---|
| 545 | if (xt_find_node(c->children, "HOME")) { |
---|
[1991be6] | 546 | s = "Home"; |
---|
[5ebff60] | 547 | } else if (xt_find_node(c->children, "WORK")) { |
---|
[1991be6] | 548 | s = "Work"; |
---|
[5ebff60] | 549 | } else { |
---|
[1991be6] | 550 | s = "Misc."; |
---|
[5ebff60] | 551 | } |
---|
| 552 | |
---|
| 553 | g_string_append_printf(reply, "%s address: ", s); |
---|
| 554 | |
---|
| 555 | if ((sc = xt_find_node(c->children, "STREET")) && sc->text_len) { |
---|
| 556 | g_string_append_printf(reply, "%s ", sc->text); |
---|
| 557 | } |
---|
| 558 | if ((sc = xt_find_node(c->children, "EXTADR")) && sc->text_len) { |
---|
| 559 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 560 | } |
---|
| 561 | if ((sc = xt_find_node(c->children, "PCODE")) && sc->text_len) { |
---|
| 562 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 563 | } |
---|
| 564 | if ((sc = xt_find_node(c->children, "LOCALITY")) && sc->text_len) { |
---|
| 565 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 566 | } |
---|
| 567 | if ((sc = xt_find_node(c->children, "REGION")) && sc->text_len) { |
---|
| 568 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 569 | } |
---|
| 570 | if ((sc = xt_find_node(c->children, "CTRY")) && sc->text_len) { |
---|
| 571 | g_string_append_printf(reply, "%s", sc->text); |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | if (reply->str[reply->len - 2] == ',') { |
---|
| 575 | reply = g_string_truncate(reply, reply->len - 2); |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | reply = g_string_append_c(reply, '\n'); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | for (c = vc->children; (c = xt_find_node(c, "TEL")); c = c->next) { |
---|
| 582 | if ((sc = xt_find_node(c->children, "NUMBER")) == NULL || sc->text_len == 0) { |
---|
[1991be6] | 583 | continue; |
---|
[5ebff60] | 584 | } |
---|
| 585 | |
---|
| 586 | if (xt_find_node(c->children, "HOME")) { |
---|
[1991be6] | 587 | s = "Home"; |
---|
[5ebff60] | 588 | } else if (xt_find_node(c->children, "WORK")) { |
---|
[1991be6] | 589 | s = "Work"; |
---|
[5ebff60] | 590 | } else { |
---|
[1991be6] | 591 | s = "Misc."; |
---|
[5ebff60] | 592 | } |
---|
| 593 | |
---|
| 594 | g_string_append_printf(reply, "%s phone number: %s\n", s, sc->text); |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | if ((c = xt_find_node(vc->children, "DESC")) && c->text_len) { |
---|
| 598 | g_string_append_printf(reply, "Other information:\n%s", c->text); |
---|
[1991be6] | 599 | } |
---|
[5ebff60] | 600 | |
---|
[1991be6] | 601 | /* *sigh* */ |
---|
[5ebff60] | 602 | |
---|
| 603 | imcb_log(ic, "%s", reply->str); |
---|
| 604 | g_string_free(reply, TRUE); |
---|
| 605 | |
---|
[1991be6] | 606 | return XT_HANDLED; |
---|
| 607 | } |
---|
| 608 | |
---|
[5ebff60] | 609 | static xt_status jabber_add_to_roster_callback(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[a73e91a] | 610 | |
---|
[5ebff60] | 611 | int jabber_add_to_roster(struct im_connection *ic, const char *handle, const char *name, const char *group) |
---|
[cfbb3a6] | 612 | { |
---|
| 613 | struct xt_node *node; |
---|
| 614 | int st; |
---|
[5ebff60] | 615 | |
---|
[cfbb3a6] | 616 | /* Build the item entry */ |
---|
[5ebff60] | 617 | node = xt_new_node("item", NULL, NULL); |
---|
| 618 | xt_add_attr(node, "jid", handle); |
---|
| 619 | if (name) { |
---|
| 620 | xt_add_attr(node, "name", name); |
---|
| 621 | } |
---|
| 622 | if (group) { |
---|
| 623 | xt_add_child(node, xt_new_node("group", group, NULL)); |
---|
| 624 | } |
---|
| 625 | |
---|
[cfbb3a6] | 626 | /* And pack it into a roster-add packet */ |
---|
[5ebff60] | 627 | node = xt_new_node("query", NULL, node); |
---|
| 628 | xt_add_attr(node, "xmlns", XMLNS_ROSTER); |
---|
| 629 | node = jabber_make_packet("iq", "set", NULL, node); |
---|
| 630 | jabber_cache_add(ic, node, jabber_add_to_roster_callback); |
---|
| 631 | |
---|
| 632 | st = jabber_write_packet(ic, node); |
---|
| 633 | |
---|
[cfbb3a6] | 634 | return st; |
---|
| 635 | } |
---|
| 636 | |
---|
[5ebff60] | 637 | static xt_status jabber_add_to_roster_callback(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[a73e91a] | 638 | { |
---|
| 639 | char *s, *jid = NULL; |
---|
| 640 | struct xt_node *c; |
---|
[5ebff60] | 641 | |
---|
| 642 | if ((c = xt_find_node(orig->children, "query")) && |
---|
| 643 | (c = xt_find_node(c->children, "item")) && |
---|
| 644 | (jid = xt_find_attr(c, "jid")) && |
---|
| 645 | (s = xt_find_attr(node, "type")) && |
---|
| 646 | strcmp(s, "result") == 0) { |
---|
| 647 | if (bee_user_by_handle(ic->bee, ic, jid) == NULL) { |
---|
| 648 | imcb_add_buddy(ic, jid, NULL); |
---|
| 649 | } |
---|
| 650 | } else { |
---|
| 651 | imcb_log(ic, "Error while adding `%s' to your contact list.", |
---|
| 652 | jid ? jid : "(unknown handle)"); |
---|
| 653 | } |
---|
| 654 | |
---|
[a73e91a] | 655 | return XT_HANDLED; |
---|
| 656 | } |
---|
| 657 | |
---|
[5ebff60] | 658 | int jabber_remove_from_roster(struct im_connection *ic, char *handle) |
---|
[cfbb3a6] | 659 | { |
---|
| 660 | struct xt_node *node; |
---|
| 661 | int st; |
---|
[5ebff60] | 662 | |
---|
[cfbb3a6] | 663 | /* Build the item entry */ |
---|
[5ebff60] | 664 | node = xt_new_node("item", NULL, NULL); |
---|
| 665 | xt_add_attr(node, "jid", handle); |
---|
| 666 | xt_add_attr(node, "subscription", "remove"); |
---|
| 667 | |
---|
[cfbb3a6] | 668 | /* And pack it into a roster-add packet */ |
---|
[5ebff60] | 669 | node = xt_new_node("query", NULL, node); |
---|
| 670 | xt_add_attr(node, "xmlns", XMLNS_ROSTER); |
---|
| 671 | node = jabber_make_packet("iq", "set", NULL, node); |
---|
| 672 | |
---|
| 673 | st = jabber_write_packet(ic, node); |
---|
| 674 | |
---|
| 675 | xt_free_node(node); |
---|
[cfbb3a6] | 676 | return st; |
---|
| 677 | } |
---|
[dc0ba9c] | 678 | |
---|
[5ebff60] | 679 | xt_status jabber_iq_parse_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[dc0ba9c] | 680 | |
---|
[5ebff60] | 681 | xt_status jabber_iq_query_features(struct im_connection *ic, char *bare_jid) |
---|
[dc0ba9c] | 682 | { |
---|
| 683 | struct xt_node *node, *query; |
---|
| 684 | struct jabber_buddy *bud; |
---|
[5ebff60] | 685 | |
---|
| 686 | if ((bud = jabber_buddy_by_jid(ic, bare_jid, 0)) == NULL) { |
---|
[dc0ba9c] | 687 | /* Who cares about the unknown... */ |
---|
[5ebff60] | 688 | imcb_log(ic, "Couldn't find buddy: %s", bare_jid); |
---|
[54a2014] | 689 | return XT_HANDLED; |
---|
[dc0ba9c] | 690 | } |
---|
[5ebff60] | 691 | |
---|
| 692 | if (bud->features) { /* been here already */ |
---|
[dc0ba9c] | 693 | return XT_HANDLED; |
---|
[5ebff60] | 694 | } |
---|
| 695 | |
---|
| 696 | node = xt_new_node("query", NULL, NULL); |
---|
| 697 | xt_add_attr(node, "xmlns", XMLNS_DISCO_INFO); |
---|
| 698 | |
---|
| 699 | if (!(query = jabber_make_packet("iq", "get", bare_jid, node))) { |
---|
| 700 | imcb_log(ic, "WARNING: Couldn't generate feature query"); |
---|
| 701 | xt_free_node(node); |
---|
[54a2014] | 702 | return XT_HANDLED; |
---|
[dc0ba9c] | 703 | } |
---|
| 704 | |
---|
[5ebff60] | 705 | jabber_cache_add(ic, query, jabber_iq_parse_features); |
---|
[dc0ba9c] | 706 | |
---|
[5ebff60] | 707 | return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT; |
---|
[dc0ba9c] | 708 | } |
---|
| 709 | |
---|
[5ebff60] | 710 | xt_status jabber_iq_parse_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[dc0ba9c] | 711 | { |
---|
| 712 | struct xt_node *c; |
---|
| 713 | struct jabber_buddy *bud; |
---|
[c1a3c27] | 714 | char *feature, *xmlns, *from; |
---|
[dc0ba9c] | 715 | |
---|
[5ebff60] | 716 | if (!(from = xt_find_attr(node, "from")) || |
---|
| 717 | !(c = xt_find_node(node->children, "query")) || |
---|
| 718 | !(xmlns = xt_find_attr(c, "xmlns")) || |
---|
| 719 | !(strcmp(xmlns, XMLNS_DISCO_INFO) == 0)) { |
---|
| 720 | imcb_log(ic, "WARNING: Received incomplete IQ-result packet for discover"); |
---|
[dc0ba9c] | 721 | return XT_HANDLED; |
---|
| 722 | } |
---|
[5ebff60] | 723 | if ((bud = jabber_buddy_by_jid(ic, from, 0)) == NULL) { |
---|
[dc0ba9c] | 724 | /* Who cares about the unknown... */ |
---|
[5ebff60] | 725 | imcb_log(ic, "Couldn't find buddy: %s", from); |
---|
[54a2014] | 726 | return XT_HANDLED; |
---|
[dc0ba9c] | 727 | } |
---|
[5ebff60] | 728 | |
---|
[dc0ba9c] | 729 | c = c->children; |
---|
[5ebff60] | 730 | while ((c = xt_find_node(c, "feature"))) { |
---|
| 731 | feature = xt_find_attr(c, "var"); |
---|
| 732 | if (feature) { |
---|
| 733 | bud->features = g_slist_append(bud->features, g_strdup(feature)); |
---|
| 734 | } |
---|
[dc0ba9c] | 735 | c = c->next; |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | return XT_HANDLED; |
---|
| 739 | } |
---|
| 740 | |
---|
[dd43c62] | 741 | xt_status jabber_iq_parse_gmail(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
| 742 | |
---|
| 743 | xt_status jabber_iq_query_gmail(struct im_connection *ic) |
---|
| 744 | { |
---|
| 745 | struct xt_node *node, *query; |
---|
| 746 | struct jabber_data *jd = ic->proto_data; |
---|
| 747 | |
---|
| 748 | node = xt_new_node("query", NULL, NULL); |
---|
| 749 | xt_add_attr(node, "xmlns", XMLNS_GMAILNOTIFY); |
---|
| 750 | if (jd->gmail_time) { |
---|
| 751 | char *formatted = g_strdup_printf("%" G_GUINT64_FORMAT, (jd->gmail_time + 1)); |
---|
| 752 | xt_add_attr(node, "newer-than-time", formatted); |
---|
| 753 | g_free(formatted); |
---|
| 754 | } |
---|
| 755 | if (jd->gmail_tid) { |
---|
| 756 | xt_add_attr(node, "newer-than-tid", jd->gmail_tid); |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | if (!(query = jabber_make_packet("iq", "get", jd->me, node))) { |
---|
| 760 | imcb_log(ic, "WARNING: Couldn't generate server query"); |
---|
| 761 | xt_free_node(node); |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | jabber_cache_add(ic, query, jabber_iq_parse_gmail); |
---|
| 765 | |
---|
| 766 | return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT; |
---|
| 767 | } |
---|
| 768 | |
---|
[5ebff60] | 769 | xt_status jabber_iq_parse_server_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[dc0ba9c] | 770 | |
---|
[5ebff60] | 771 | xt_status jabber_iq_query_server(struct im_connection *ic, char *jid, char *xmlns) |
---|
[dc0ba9c] | 772 | { |
---|
| 773 | struct xt_node *node, *query; |
---|
| 774 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 775 | |
---|
| 776 | node = xt_new_node("query", NULL, NULL); |
---|
| 777 | xt_add_attr(node, "xmlns", xmlns); |
---|
| 778 | |
---|
| 779 | if (!(query = jabber_make_packet("iq", "get", jid, node))) { |
---|
| 780 | imcb_log(ic, "WARNING: Couldn't generate server query"); |
---|
| 781 | xt_free_node(node); |
---|
[dc0ba9c] | 782 | } |
---|
| 783 | |
---|
| 784 | jd->have_streamhosts--; |
---|
[5ebff60] | 785 | jabber_cache_add(ic, query, jabber_iq_parse_server_features); |
---|
[dc0ba9c] | 786 | |
---|
[5ebff60] | 787 | return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT; |
---|
[dc0ba9c] | 788 | } |
---|
| 789 | |
---|
[dd43c62] | 790 | xt_status jabber_iq_parse_gmail(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
| 791 | { |
---|
| 792 | struct xt_node *c; |
---|
| 793 | struct jabber_data *jd = ic->proto_data; |
---|
| 794 | char *xmlns, *from; |
---|
| 795 | guint64 l_time = 0; |
---|
| 796 | char *tid = NULL; |
---|
[faeb521] | 797 | int max = 0; |
---|
[dd43c62] | 798 | |
---|
| 799 | if (!(c = xt_find_node(node->children, "mailbox")) || |
---|
| 800 | !(from = xt_find_attr(node, "from")) || |
---|
| 801 | !(xmlns = xt_find_attr(c, "xmlns")) || |
---|
| 802 | (g_strcmp0(xmlns, XMLNS_GMAILNOTIFY) != 0)) { |
---|
| 803 | imcb_log(ic, "WARNING: Received incomplete mailbox packet for gmail notify"); |
---|
| 804 | return XT_HANDLED; |
---|
| 805 | } |
---|
| 806 | |
---|
[b38f655] | 807 | max = set_getint(&ic->acc->set, "mail_notifications_limit"); |
---|
[dd43c62] | 808 | c = c->children; |
---|
| 809 | |
---|
[faeb521] | 810 | while ((max-- > 0) && (c = xt_find_node(c, "mail-thread-info"))) { |
---|
| 811 | struct xt_node *s; |
---|
| 812 | char *subject = "<no subject>"; |
---|
| 813 | char *sender = "<no sender>"; |
---|
[dd43c62] | 814 | guint64 t_time; |
---|
| 815 | |
---|
| 816 | t_time = g_ascii_strtoull(xt_find_attr(c, "date"), NULL, 10); |
---|
| 817 | if (t_time && t_time > l_time) { |
---|
| 818 | l_time = t_time; |
---|
| 819 | tid = xt_find_attr(c, "tid"); |
---|
| 820 | } |
---|
| 821 | |
---|
[faeb521] | 822 | if ((s = xt_find_node(c->children, "senders")) && |
---|
| 823 | (s = xt_find_node_by_attr(s->children, "sender", "unread", "1"))) { |
---|
| 824 | sender = xt_find_attr(s, "name"); |
---|
| 825 | } |
---|
[dd43c62] | 826 | |
---|
[faeb521] | 827 | if ((s = xt_find_node(c->children, "subject")) && s->text) { |
---|
[dd43c62] | 828 | subject = s->text; |
---|
| 829 | } |
---|
| 830 | |
---|
[0864a52] | 831 | imcb_notify_email(ic, "New mail from %s: %s", sender, subject); |
---|
[dd43c62] | 832 | |
---|
| 833 | c = c->next; |
---|
| 834 | } |
---|
| 835 | |
---|
| 836 | if (l_time && (!jd->gmail_time || l_time > jd->gmail_time)) { |
---|
| 837 | jd->gmail_time = l_time; |
---|
| 838 | if (tid) { |
---|
| 839 | g_free(jd->gmail_tid); |
---|
| 840 | jd->gmail_tid = g_strdup(tid); |
---|
| 841 | } |
---|
| 842 | } |
---|
| 843 | |
---|
| 844 | return XT_HANDLED; |
---|
| 845 | } |
---|
| 846 | |
---|
[dc0ba9c] | 847 | /* |
---|
| 848 | * Query the server for "items", query each "item" for identities, query each "item" that's a proxy for it's bytestream info |
---|
| 849 | */ |
---|
[5ebff60] | 850 | xt_status jabber_iq_parse_server_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[dc0ba9c] | 851 | { |
---|
| 852 | struct xt_node *c; |
---|
| 853 | struct jabber_data *jd = ic->proto_data; |
---|
[c1a3c27] | 854 | char *xmlns, *from; |
---|
[dc0ba9c] | 855 | |
---|
[5ebff60] | 856 | if (!(c = xt_find_node(node->children, "query")) || |
---|
| 857 | !(from = xt_find_attr(node, "from")) || |
---|
| 858 | !(xmlns = xt_find_attr(c, "xmlns"))) { |
---|
| 859 | imcb_log(ic, "WARNING: Received incomplete IQ-result packet for discover"); |
---|
[dc0ba9c] | 860 | return XT_HANDLED; |
---|
| 861 | } |
---|
| 862 | |
---|
| 863 | jd->have_streamhosts++; |
---|
| 864 | |
---|
[5ebff60] | 865 | if (strcmp(xmlns, XMLNS_DISCO_ITEMS) == 0) { |
---|
[c1a3c27] | 866 | char *itemjid; |
---|
[dc0ba9c] | 867 | |
---|
| 868 | /* answer from server */ |
---|
[5ebff60] | 869 | |
---|
[dc0ba9c] | 870 | c = c->children; |
---|
[5ebff60] | 871 | while ((c = xt_find_node(c, "item"))) { |
---|
| 872 | itemjid = xt_find_attr(c, "jid"); |
---|
| 873 | |
---|
| 874 | if (itemjid) { |
---|
| 875 | jabber_iq_query_server(ic, itemjid, XMLNS_DISCO_INFO); |
---|
| 876 | } |
---|
[dc0ba9c] | 877 | |
---|
| 878 | c = c->next; |
---|
| 879 | } |
---|
[5ebff60] | 880 | } else if (strcmp(xmlns, XMLNS_DISCO_INFO) == 0) { |
---|
[dc0ba9c] | 881 | char *category, *type; |
---|
| 882 | |
---|
| 883 | /* answer from potential proxy */ |
---|
| 884 | |
---|
| 885 | c = c->children; |
---|
[5ebff60] | 886 | while ((c = xt_find_node(c, "identity"))) { |
---|
| 887 | category = xt_find_attr(c, "category"); |
---|
| 888 | type = xt_find_attr(c, "type"); |
---|
[dc0ba9c] | 889 | |
---|
[5ebff60] | 890 | if (type && (strcmp(type, "bytestreams") == 0) && |
---|
| 891 | category && (strcmp(category, "proxy") == 0)) { |
---|
| 892 | jabber_iq_query_server(ic, from, XMLNS_BYTESTREAMS); |
---|
| 893 | } |
---|
[dc0ba9c] | 894 | |
---|
| 895 | c = c->next; |
---|
| 896 | } |
---|
[dd43c62] | 897 | |
---|
| 898 | if (jd->flags & JFLAG_GMAILNOTIFY) { |
---|
| 899 | /* search for gmail notification feature */ |
---|
| 900 | c = xt_find_node(node->children, "query"); |
---|
| 901 | c = c->children; |
---|
| 902 | while ((c = xt_find_node(c, "feature"))) { |
---|
| 903 | if (strcmp(xt_find_attr(c, "var"), XMLNS_GMAILNOTIFY) == 0) { |
---|
| 904 | jabber_iq_query_gmail(ic); |
---|
| 905 | } |
---|
| 906 | c = c->next; |
---|
| 907 | } |
---|
| 908 | } |
---|
| 909 | |
---|
[5ebff60] | 910 | } else if (strcmp(xmlns, XMLNS_BYTESTREAMS) == 0) { |
---|
[c1a3c27] | 911 | char *host, *jid, *port_s; |
---|
[dc0ba9c] | 912 | int port; |
---|
| 913 | |
---|
| 914 | /* answer from proxy */ |
---|
| 915 | |
---|
[5ebff60] | 916 | if ((c = xt_find_node(c->children, "streamhost")) && |
---|
| 917 | (host = xt_find_attr(c, "host")) && |
---|
| 918 | (port_s = xt_find_attr(c, "port")) && |
---|
| 919 | (sscanf(port_s, "%d", &port) == 1) && |
---|
| 920 | (jid = xt_find_attr(c, "jid"))) { |
---|
| 921 | jabber_streamhost_t *sh = g_new0(jabber_streamhost_t, 1); |
---|
[dc0ba9c] | 922 | |
---|
[5ebff60] | 923 | sh->jid = g_strdup(jid); |
---|
| 924 | sh->host = g_strdup(host); |
---|
| 925 | g_snprintf(sh->port, sizeof(sh->port), "%u", port); |
---|
| 926 | |
---|
| 927 | imcb_log(ic, "Proxy found: jid %s host %s port %u", jid, host, port); |
---|
| 928 | jd->streamhosts = g_slist_append(jd->streamhosts, sh); |
---|
[dc0ba9c] | 929 | } |
---|
| 930 | } |
---|
| 931 | |
---|
[5ebff60] | 932 | if (jd->have_streamhosts == 0) { |
---|
[dc0ba9c] | 933 | jd->have_streamhosts++; |
---|
[5ebff60] | 934 | } |
---|
[1c3008a] | 935 | |
---|
[dc0ba9c] | 936 | return XT_HANDLED; |
---|
| 937 | } |
---|
[d88c92a] | 938 | |
---|
[5ebff60] | 939 | static xt_status jabber_iq_version_response(struct im_connection *ic, |
---|
| 940 | struct xt_node *node, struct xt_node *orig); |
---|
[d88c92a] | 941 | |
---|
[5ebff60] | 942 | void jabber_iq_version_send(struct im_connection *ic, struct jabber_buddy *bud, void *data) |
---|
[d88c92a] | 943 | { |
---|
| 944 | struct xt_node *node, *query; |
---|
| 945 | |
---|
[5ebff60] | 946 | node = xt_new_node("query", NULL, NULL); |
---|
| 947 | xt_add_attr(node, "xmlns", XMLNS_VERSION); |
---|
| 948 | query = jabber_make_packet("iq", "get", bud->full_jid, node); |
---|
| 949 | jabber_cache_add(ic, query, jabber_iq_version_response); |
---|
| 950 | |
---|
| 951 | jabber_write_packet(ic, query); |
---|
[d88c92a] | 952 | } |
---|
| 953 | |
---|
[5ebff60] | 954 | static xt_status jabber_iq_version_response(struct im_connection *ic, |
---|
| 955 | struct xt_node *node, struct xt_node *orig) |
---|
[d88c92a] | 956 | { |
---|
| 957 | struct xt_node *query; |
---|
| 958 | GString *rets; |
---|
| 959 | char *s; |
---|
| 960 | char *ret[2] = {}; |
---|
| 961 | bee_user_t *bu; |
---|
| 962 | struct jabber_buddy *bud = NULL; |
---|
[5ebff60] | 963 | |
---|
| 964 | if ((s = xt_find_attr(node, "from")) && |
---|
| 965 | (bud = jabber_buddy_by_jid(ic, s, 0)) && |
---|
| 966 | (query = xt_find_node(node->children, "query")) && |
---|
| 967 | (bu = bee_user_by_handle(ic->bee, ic, bud->bare_jid))) { |
---|
| 968 | rets = g_string_new("Resource "); |
---|
| 969 | g_string_append(rets, bud->resource); |
---|
| 970 | } else { |
---|
[d88c92a] | 971 | return XT_HANDLED; |
---|
[5ebff60] | 972 | } |
---|
| 973 | |
---|
| 974 | for (query = query->children; query; query = query->next) { |
---|
| 975 | if (query->text_len > 0) { |
---|
| 976 | g_string_append_printf(rets, " %s: %s,", query->name, query->text); |
---|
| 977 | } |
---|
| 978 | } |
---|
| 979 | |
---|
| 980 | g_string_truncate(rets, rets->len - 1); |
---|
[d88c92a] | 981 | ret[0] = rets->str; |
---|
[5ebff60] | 982 | imcb_buddy_action_response(bu, "VERSION", ret, NULL); |
---|
| 983 | g_string_free(rets, TRUE); |
---|
| 984 | |
---|
[d88c92a] | 985 | return XT_HANDLED; |
---|
| 986 | } |
---|
[bb2d198] | 987 | |
---|
[5ebff60] | 988 | static xt_status jabber_iq_disco_server_response(struct im_connection *ic, |
---|
| 989 | struct xt_node *node, struct xt_node *orig); |
---|
[bb2d198] | 990 | |
---|
[40cfbc5] | 991 | int jabber_iq_disco_server(struct im_connection *ic) |
---|
[bb2d198] | 992 | { |
---|
| 993 | struct xt_node *node, *iq; |
---|
| 994 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 995 | |
---|
| 996 | node = xt_new_node("query", NULL, NULL); |
---|
| 997 | xt_add_attr(node, "xmlns", XMLNS_DISCO_INFO); |
---|
| 998 | iq = jabber_make_packet("iq", "get", jd->server, node); |
---|
| 999 | |
---|
| 1000 | jabber_cache_add(ic, iq, jabber_iq_disco_server_response); |
---|
| 1001 | return jabber_write_packet(ic, iq); |
---|
[bb2d198] | 1002 | } |
---|
| 1003 | |
---|
[5ebff60] | 1004 | static xt_status jabber_iq_disco_server_response(struct im_connection *ic, |
---|
| 1005 | struct xt_node *node, struct xt_node *orig) |
---|
[bb2d198] | 1006 | { |
---|
| 1007 | struct jabber_data *jd = ic->proto_data; |
---|
[fa8f57b] | 1008 | struct xt_node *query, *id; |
---|
[5ebff60] | 1009 | |
---|
[fa8f57b] | 1010 | if (!(query = xt_find_node(node->children, "query"))) { |
---|
| 1011 | return XT_HANDLED; |
---|
| 1012 | } |
---|
| 1013 | |
---|
| 1014 | if (xt_find_node_by_attr(query->children, "feature", "var", XMLNS_CARBONS) && |
---|
| 1015 | set_getbool(&ic->acc->set, "carbons")) { |
---|
| 1016 | |
---|
| 1017 | struct xt_node *enable, *iq; |
---|
| 1018 | |
---|
| 1019 | enable = xt_new_node("enable", NULL, NULL); |
---|
| 1020 | xt_add_attr(enable, "xmlns", XMLNS_CARBONS); |
---|
| 1021 | iq = jabber_make_packet("iq", "set", NULL, enable); |
---|
| 1022 | |
---|
| 1023 | jabber_cache_add(ic, iq, jabber_iq_carbons_response); |
---|
| 1024 | jabber_write_packet(ic, iq); |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | if ((id = xt_find_node(query->children, "identity"))) { |
---|
[bb2d198] | 1028 | char *cat, *type, *name; |
---|
[5ebff60] | 1029 | |
---|
| 1030 | if (!(cat = xt_find_attr(id, "category")) || |
---|
| 1031 | !(type = xt_find_attr(id, "type")) || |
---|
| 1032 | !(name = xt_find_attr(id, "name"))) { |
---|
[bb2d198] | 1033 | return XT_HANDLED; |
---|
[5ebff60] | 1034 | } |
---|
| 1035 | |
---|
| 1036 | if (strcmp(cat, "server") == 0 && strcmp(type, "im") == 0 && |
---|
| 1037 | strstr(name, "Google") != NULL) { |
---|
[bb2d198] | 1038 | jd->flags |= JFLAG_GTALK; |
---|
[5ebff60] | 1039 | } |
---|
[bb2d198] | 1040 | } |
---|
[5ebff60] | 1041 | |
---|
[bb2d198] | 1042 | return XT_HANDLED; |
---|
| 1043 | } |
---|
[fa8f57b] | 1044 | |
---|
| 1045 | static xt_status jabber_iq_carbons_response(struct im_connection *ic, |
---|
| 1046 | struct xt_node *node, struct xt_node *orig) |
---|
| 1047 | { |
---|
| 1048 | struct jabber_error *err; |
---|
| 1049 | |
---|
| 1050 | if ((err = jabber_error_parse(xt_find_node(node->children, "error"), XMLNS_STANZA_ERROR))) { |
---|
| 1051 | imcb_error(ic, "Error enabling carbons: %s%s%s", |
---|
| 1052 | err->code, err->text ? ": " : "", err->text ? err->text : ""); |
---|
| 1053 | jabber_error_free(err); |
---|
| 1054 | } else { |
---|
| 1055 | imcb_log(ic, "Carbons enabled"); |
---|
| 1056 | } |
---|
| 1057 | |
---|
| 1058 | return XT_HANDLED; |
---|
| 1059 | } |
---|