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