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