[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"); |
---|
| 390 | |
---|
| 391 | if (jid && sub) { |
---|
| 392 | if ((strcmp(sub, "both") == 0 || strcmp(sub, "to") == 0)) { |
---|
| 393 | imcb_add_buddy(ic, jid, (group && group->text_len) ? |
---|
| 394 | group->text : NULL); |
---|
| 395 | |
---|
| 396 | if (name) { |
---|
| 397 | imcb_rename_buddy(ic, jid, name); |
---|
| 398 | } |
---|
| 399 | } else if (strcmp(sub, "remove") == 0) { |
---|
| 400 | jabber_buddy_remove_bare(ic, jid); |
---|
| 401 | imcb_remove_buddy(ic, jid, NULL); |
---|
[abbd8ed] | 402 | } |
---|
| 403 | } |
---|
[5ebff60] | 404 | |
---|
[861c199] | 405 | c = c->next; |
---|
| 406 | } |
---|
[5ebff60] | 407 | |
---|
| 408 | if (initial) { |
---|
| 409 | imcb_connected(ic); |
---|
| 410 | } |
---|
| 411 | |
---|
[861c199] | 412 | return XT_HANDLED; |
---|
| 413 | } |
---|
| 414 | |
---|
[5ebff60] | 415 | int jabber_get_vcard(struct im_connection *ic, char *bare_jid) |
---|
[1991be6] | 416 | { |
---|
| 417 | struct xt_node *node; |
---|
[5ebff60] | 418 | |
---|
| 419 | if (strchr(bare_jid, '/')) { |
---|
| 420 | return 1; /* This was an error, but return 0 should only be done if the connection died... */ |
---|
| 421 | |
---|
| 422 | } |
---|
| 423 | node = xt_new_node("vCard", NULL, NULL); |
---|
| 424 | xt_add_attr(node, "xmlns", XMLNS_VCARD); |
---|
| 425 | node = jabber_make_packet("iq", "get", bare_jid, node); |
---|
| 426 | |
---|
| 427 | jabber_cache_add(ic, node, jabber_iq_display_vcard); |
---|
| 428 | return jabber_write_packet(ic, node); |
---|
[1991be6] | 429 | } |
---|
| 430 | |
---|
[5ebff60] | 431 | static xt_status jabber_iq_display_vcard(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[1991be6] | 432 | { |
---|
[0da65d5] | 433 | struct xt_node *vc, *c, *sc; /* subchild, ic is already in use ;-) */ |
---|
[1991be6] | 434 | GString *reply; |
---|
| 435 | char *s; |
---|
[5ebff60] | 436 | |
---|
| 437 | if ((s = xt_find_attr(node, "type")) == NULL || |
---|
| 438 | strcmp(s, "result") != 0 || |
---|
| 439 | (vc = xt_find_node(node->children, "vCard")) == NULL) { |
---|
| 440 | s = xt_find_attr(orig, "to"); /* If this returns NULL something's wrong.. */ |
---|
| 441 | imcb_log(ic, "Could not retrieve vCard of %s", s ? s : "(NULL)"); |
---|
[1991be6] | 442 | return XT_HANDLED; |
---|
| 443 | } |
---|
[5ebff60] | 444 | |
---|
| 445 | s = xt_find_attr(orig, "to"); |
---|
| 446 | reply = g_string_new("vCard information for "); |
---|
| 447 | reply = g_string_append(reply, s ? s : "(NULL)"); |
---|
| 448 | reply = g_string_append(reply, ":\n"); |
---|
| 449 | |
---|
[1991be6] | 450 | /* I hate this format, I really do... */ |
---|
[5ebff60] | 451 | |
---|
| 452 | if ((c = xt_find_node(vc->children, "FN")) && c->text_len) { |
---|
| 453 | g_string_append_printf(reply, "Name: %s\n", c->text); |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | if ((c = xt_find_node(vc->children, "N")) && c->children) { |
---|
| 457 | reply = g_string_append(reply, "Full name:"); |
---|
| 458 | |
---|
| 459 | if ((sc = xt_find_node(c->children, "PREFIX")) && sc->text_len) { |
---|
| 460 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 461 | } |
---|
| 462 | if ((sc = xt_find_node(c->children, "GIVEN")) && sc->text_len) { |
---|
| 463 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 464 | } |
---|
| 465 | if ((sc = xt_find_node(c->children, "MIDDLE")) && sc->text_len) { |
---|
| 466 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 467 | } |
---|
| 468 | if ((sc = xt_find_node(c->children, "FAMILY")) && sc->text_len) { |
---|
| 469 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 470 | } |
---|
| 471 | if ((sc = xt_find_node(c->children, "SUFFIX")) && sc->text_len) { |
---|
| 472 | g_string_append_printf(reply, " %s", sc->text); |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | reply = g_string_append_c(reply, '\n'); |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | if ((c = xt_find_node(vc->children, "NICKNAME")) && c->text_len) { |
---|
| 479 | g_string_append_printf(reply, "Nickname: %s\n", c->text); |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | if ((c = xt_find_node(vc->children, "BDAY")) && c->text_len) { |
---|
| 483 | g_string_append_printf(reply, "Date of birth: %s\n", c->text); |
---|
| 484 | } |
---|
| 485 | |
---|
[1991be6] | 486 | /* Slightly alternative use of for... ;-) */ |
---|
[5ebff60] | 487 | for (c = vc->children; (c = xt_find_node(c, "EMAIL")); c = c->next) { |
---|
| 488 | if ((sc = xt_find_node(c->children, "USERID")) == NULL || sc->text_len == 0) { |
---|
[1991be6] | 489 | continue; |
---|
[5ebff60] | 490 | } |
---|
| 491 | |
---|
| 492 | if (xt_find_node(c->children, "HOME")) { |
---|
[1991be6] | 493 | s = "Home"; |
---|
[5ebff60] | 494 | } else if (xt_find_node(c->children, "WORK")) { |
---|
[1991be6] | 495 | s = "Work"; |
---|
[5ebff60] | 496 | } else { |
---|
[1991be6] | 497 | s = "Misc."; |
---|
[5ebff60] | 498 | } |
---|
| 499 | |
---|
| 500 | g_string_append_printf(reply, "%s e-mail address: %s\n", s, sc->text); |
---|
[1991be6] | 501 | } |
---|
[5ebff60] | 502 | |
---|
| 503 | if ((c = xt_find_node(vc->children, "URL")) && c->text_len) { |
---|
| 504 | g_string_append_printf(reply, "Homepage: %s\n", c->text); |
---|
| 505 | } |
---|
| 506 | |
---|
[1991be6] | 507 | /* Slightly alternative use of for... ;-) */ |
---|
[5ebff60] | 508 | for (c = vc->children; (c = xt_find_node(c, "ADR")); c = c->next) { |
---|
| 509 | if (xt_find_node(c->children, "HOME")) { |
---|
[1991be6] | 510 | s = "Home"; |
---|
[5ebff60] | 511 | } else if (xt_find_node(c->children, "WORK")) { |
---|
[1991be6] | 512 | s = "Work"; |
---|
[5ebff60] | 513 | } else { |
---|
[1991be6] | 514 | s = "Misc."; |
---|
[5ebff60] | 515 | } |
---|
| 516 | |
---|
| 517 | g_string_append_printf(reply, "%s address: ", s); |
---|
| 518 | |
---|
| 519 | if ((sc = xt_find_node(c->children, "STREET")) && sc->text_len) { |
---|
| 520 | g_string_append_printf(reply, "%s ", sc->text); |
---|
| 521 | } |
---|
| 522 | if ((sc = xt_find_node(c->children, "EXTADR")) && sc->text_len) { |
---|
| 523 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 524 | } |
---|
| 525 | if ((sc = xt_find_node(c->children, "PCODE")) && sc->text_len) { |
---|
| 526 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 527 | } |
---|
| 528 | if ((sc = xt_find_node(c->children, "LOCALITY")) && sc->text_len) { |
---|
| 529 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 530 | } |
---|
| 531 | if ((sc = xt_find_node(c->children, "REGION")) && sc->text_len) { |
---|
| 532 | g_string_append_printf(reply, "%s, ", sc->text); |
---|
| 533 | } |
---|
| 534 | if ((sc = xt_find_node(c->children, "CTRY")) && sc->text_len) { |
---|
| 535 | g_string_append_printf(reply, "%s", sc->text); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | if (reply->str[reply->len - 2] == ',') { |
---|
| 539 | reply = g_string_truncate(reply, reply->len - 2); |
---|
| 540 | } |
---|
| 541 | |
---|
| 542 | reply = g_string_append_c(reply, '\n'); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | for (c = vc->children; (c = xt_find_node(c, "TEL")); c = c->next) { |
---|
| 546 | if ((sc = xt_find_node(c->children, "NUMBER")) == NULL || sc->text_len == 0) { |
---|
[1991be6] | 547 | continue; |
---|
[5ebff60] | 548 | } |
---|
| 549 | |
---|
| 550 | if (xt_find_node(c->children, "HOME")) { |
---|
[1991be6] | 551 | s = "Home"; |
---|
[5ebff60] | 552 | } else if (xt_find_node(c->children, "WORK")) { |
---|
[1991be6] | 553 | s = "Work"; |
---|
[5ebff60] | 554 | } else { |
---|
[1991be6] | 555 | s = "Misc."; |
---|
[5ebff60] | 556 | } |
---|
| 557 | |
---|
| 558 | g_string_append_printf(reply, "%s phone number: %s\n", s, sc->text); |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | if ((c = xt_find_node(vc->children, "DESC")) && c->text_len) { |
---|
| 562 | g_string_append_printf(reply, "Other information:\n%s", c->text); |
---|
[1991be6] | 563 | } |
---|
[5ebff60] | 564 | |
---|
[1991be6] | 565 | /* *sigh* */ |
---|
[5ebff60] | 566 | |
---|
| 567 | imcb_log(ic, "%s", reply->str); |
---|
| 568 | g_string_free(reply, TRUE); |
---|
| 569 | |
---|
[1991be6] | 570 | return XT_HANDLED; |
---|
| 571 | } |
---|
| 572 | |
---|
[5ebff60] | 573 | static xt_status jabber_add_to_roster_callback(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[a73e91a] | 574 | |
---|
[5ebff60] | 575 | int jabber_add_to_roster(struct im_connection *ic, const char *handle, const char *name, const char *group) |
---|
[cfbb3a6] | 576 | { |
---|
| 577 | struct xt_node *node; |
---|
| 578 | int st; |
---|
[5ebff60] | 579 | |
---|
[cfbb3a6] | 580 | /* Build the item entry */ |
---|
[5ebff60] | 581 | node = xt_new_node("item", NULL, NULL); |
---|
| 582 | xt_add_attr(node, "jid", handle); |
---|
| 583 | if (name) { |
---|
| 584 | xt_add_attr(node, "name", name); |
---|
| 585 | } |
---|
| 586 | if (group) { |
---|
| 587 | xt_add_child(node, xt_new_node("group", group, NULL)); |
---|
| 588 | } |
---|
| 589 | |
---|
[cfbb3a6] | 590 | /* And pack it into a roster-add packet */ |
---|
[5ebff60] | 591 | node = xt_new_node("query", NULL, node); |
---|
| 592 | xt_add_attr(node, "xmlns", XMLNS_ROSTER); |
---|
| 593 | node = jabber_make_packet("iq", "set", NULL, node); |
---|
| 594 | jabber_cache_add(ic, node, jabber_add_to_roster_callback); |
---|
| 595 | |
---|
| 596 | st = jabber_write_packet(ic, node); |
---|
| 597 | |
---|
[cfbb3a6] | 598 | return st; |
---|
| 599 | } |
---|
| 600 | |
---|
[5ebff60] | 601 | static xt_status jabber_add_to_roster_callback(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[a73e91a] | 602 | { |
---|
| 603 | char *s, *jid = NULL; |
---|
| 604 | struct xt_node *c; |
---|
[5ebff60] | 605 | |
---|
| 606 | if ((c = xt_find_node(orig->children, "query")) && |
---|
| 607 | (c = xt_find_node(c->children, "item")) && |
---|
| 608 | (jid = xt_find_attr(c, "jid")) && |
---|
| 609 | (s = xt_find_attr(node, "type")) && |
---|
| 610 | strcmp(s, "result") == 0) { |
---|
| 611 | if (bee_user_by_handle(ic->bee, ic, jid) == NULL) { |
---|
| 612 | imcb_add_buddy(ic, jid, NULL); |
---|
| 613 | } |
---|
| 614 | } else { |
---|
| 615 | imcb_log(ic, "Error while adding `%s' to your contact list.", |
---|
| 616 | jid ? jid : "(unknown handle)"); |
---|
| 617 | } |
---|
| 618 | |
---|
[a73e91a] | 619 | return XT_HANDLED; |
---|
| 620 | } |
---|
| 621 | |
---|
[5ebff60] | 622 | int jabber_remove_from_roster(struct im_connection *ic, char *handle) |
---|
[cfbb3a6] | 623 | { |
---|
| 624 | struct xt_node *node; |
---|
| 625 | int st; |
---|
[5ebff60] | 626 | |
---|
[cfbb3a6] | 627 | /* Build the item entry */ |
---|
[5ebff60] | 628 | node = xt_new_node("item", NULL, NULL); |
---|
| 629 | xt_add_attr(node, "jid", handle); |
---|
| 630 | xt_add_attr(node, "subscription", "remove"); |
---|
| 631 | |
---|
[cfbb3a6] | 632 | /* And pack it into a roster-add packet */ |
---|
[5ebff60] | 633 | node = xt_new_node("query", NULL, node); |
---|
| 634 | xt_add_attr(node, "xmlns", XMLNS_ROSTER); |
---|
| 635 | node = jabber_make_packet("iq", "set", NULL, node); |
---|
| 636 | |
---|
| 637 | st = jabber_write_packet(ic, node); |
---|
| 638 | |
---|
| 639 | xt_free_node(node); |
---|
[cfbb3a6] | 640 | return st; |
---|
| 641 | } |
---|
[dc0ba9c] | 642 | |
---|
[5ebff60] | 643 | xt_status jabber_iq_parse_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[dc0ba9c] | 644 | |
---|
[5ebff60] | 645 | xt_status jabber_iq_query_features(struct im_connection *ic, char *bare_jid) |
---|
[dc0ba9c] | 646 | { |
---|
| 647 | struct xt_node *node, *query; |
---|
| 648 | struct jabber_buddy *bud; |
---|
[5ebff60] | 649 | |
---|
| 650 | if ((bud = jabber_buddy_by_jid(ic, bare_jid, 0)) == NULL) { |
---|
[dc0ba9c] | 651 | /* Who cares about the unknown... */ |
---|
[5ebff60] | 652 | imcb_log(ic, "Couldn't find buddy: %s", bare_jid); |
---|
[54a2014] | 653 | return XT_HANDLED; |
---|
[dc0ba9c] | 654 | } |
---|
[5ebff60] | 655 | |
---|
| 656 | if (bud->features) { /* been here already */ |
---|
[dc0ba9c] | 657 | return XT_HANDLED; |
---|
[5ebff60] | 658 | } |
---|
| 659 | |
---|
| 660 | node = xt_new_node("query", NULL, NULL); |
---|
| 661 | xt_add_attr(node, "xmlns", XMLNS_DISCO_INFO); |
---|
| 662 | |
---|
| 663 | if (!(query = jabber_make_packet("iq", "get", bare_jid, node))) { |
---|
| 664 | imcb_log(ic, "WARNING: Couldn't generate feature query"); |
---|
| 665 | xt_free_node(node); |
---|
[54a2014] | 666 | return XT_HANDLED; |
---|
[dc0ba9c] | 667 | } |
---|
| 668 | |
---|
[5ebff60] | 669 | jabber_cache_add(ic, query, jabber_iq_parse_features); |
---|
[dc0ba9c] | 670 | |
---|
[5ebff60] | 671 | return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT; |
---|
[dc0ba9c] | 672 | } |
---|
| 673 | |
---|
[5ebff60] | 674 | xt_status jabber_iq_parse_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[dc0ba9c] | 675 | { |
---|
| 676 | struct xt_node *c; |
---|
| 677 | struct jabber_buddy *bud; |
---|
[c1a3c27] | 678 | char *feature, *xmlns, *from; |
---|
[dc0ba9c] | 679 | |
---|
[5ebff60] | 680 | if (!(from = xt_find_attr(node, "from")) || |
---|
| 681 | !(c = xt_find_node(node->children, "query")) || |
---|
| 682 | !(xmlns = xt_find_attr(c, "xmlns")) || |
---|
| 683 | !(strcmp(xmlns, XMLNS_DISCO_INFO) == 0)) { |
---|
| 684 | imcb_log(ic, "WARNING: Received incomplete IQ-result packet for discover"); |
---|
[dc0ba9c] | 685 | return XT_HANDLED; |
---|
| 686 | } |
---|
[5ebff60] | 687 | if ((bud = jabber_buddy_by_jid(ic, from, 0)) == NULL) { |
---|
[dc0ba9c] | 688 | /* Who cares about the unknown... */ |
---|
[5ebff60] | 689 | imcb_log(ic, "Couldn't find buddy: %s", from); |
---|
[54a2014] | 690 | return XT_HANDLED; |
---|
[dc0ba9c] | 691 | } |
---|
[5ebff60] | 692 | |
---|
[dc0ba9c] | 693 | c = c->children; |
---|
[5ebff60] | 694 | while ((c = xt_find_node(c, "feature"))) { |
---|
| 695 | feature = xt_find_attr(c, "var"); |
---|
| 696 | if (feature) { |
---|
| 697 | bud->features = g_slist_append(bud->features, g_strdup(feature)); |
---|
| 698 | } |
---|
[dc0ba9c] | 699 | c = c->next; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | return XT_HANDLED; |
---|
| 703 | } |
---|
| 704 | |
---|
[5ebff60] | 705 | xt_status jabber_iq_parse_server_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
[dc0ba9c] | 706 | |
---|
[5ebff60] | 707 | xt_status jabber_iq_query_server(struct im_connection *ic, char *jid, char *xmlns) |
---|
[dc0ba9c] | 708 | { |
---|
| 709 | struct xt_node *node, *query; |
---|
| 710 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 711 | |
---|
| 712 | node = xt_new_node("query", NULL, NULL); |
---|
| 713 | xt_add_attr(node, "xmlns", xmlns); |
---|
| 714 | |
---|
| 715 | if (!(query = jabber_make_packet("iq", "get", jid, node))) { |
---|
| 716 | imcb_log(ic, "WARNING: Couldn't generate server query"); |
---|
| 717 | xt_free_node(node); |
---|
[dc0ba9c] | 718 | } |
---|
| 719 | |
---|
| 720 | jd->have_streamhosts--; |
---|
[5ebff60] | 721 | jabber_cache_add(ic, query, jabber_iq_parse_server_features); |
---|
[dc0ba9c] | 722 | |
---|
[5ebff60] | 723 | return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT; |
---|
[dc0ba9c] | 724 | } |
---|
| 725 | |
---|
| 726 | /* |
---|
| 727 | * Query the server for "items", query each "item" for identities, query each "item" that's a proxy for it's bytestream info |
---|
| 728 | */ |
---|
[5ebff60] | 729 | xt_status jabber_iq_parse_server_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
[dc0ba9c] | 730 | { |
---|
| 731 | struct xt_node *c; |
---|
| 732 | struct jabber_data *jd = ic->proto_data; |
---|
[c1a3c27] | 733 | char *xmlns, *from; |
---|
[dc0ba9c] | 734 | |
---|
[5ebff60] | 735 | if (!(c = xt_find_node(node->children, "query")) || |
---|
| 736 | !(from = xt_find_attr(node, "from")) || |
---|
| 737 | !(xmlns = xt_find_attr(c, "xmlns"))) { |
---|
| 738 | imcb_log(ic, "WARNING: Received incomplete IQ-result packet for discover"); |
---|
[dc0ba9c] | 739 | return XT_HANDLED; |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | jd->have_streamhosts++; |
---|
| 743 | |
---|
[5ebff60] | 744 | if (strcmp(xmlns, XMLNS_DISCO_ITEMS) == 0) { |
---|
[c1a3c27] | 745 | char *itemjid; |
---|
[dc0ba9c] | 746 | |
---|
| 747 | /* answer from server */ |
---|
[5ebff60] | 748 | |
---|
[dc0ba9c] | 749 | c = c->children; |
---|
[5ebff60] | 750 | while ((c = xt_find_node(c, "item"))) { |
---|
| 751 | itemjid = xt_find_attr(c, "jid"); |
---|
| 752 | |
---|
| 753 | if (itemjid) { |
---|
| 754 | jabber_iq_query_server(ic, itemjid, XMLNS_DISCO_INFO); |
---|
| 755 | } |
---|
[dc0ba9c] | 756 | |
---|
| 757 | c = c->next; |
---|
| 758 | } |
---|
[5ebff60] | 759 | } else if (strcmp(xmlns, XMLNS_DISCO_INFO) == 0) { |
---|
[dc0ba9c] | 760 | char *category, *type; |
---|
| 761 | |
---|
| 762 | /* answer from potential proxy */ |
---|
| 763 | |
---|
| 764 | c = c->children; |
---|
[5ebff60] | 765 | while ((c = xt_find_node(c, "identity"))) { |
---|
| 766 | category = xt_find_attr(c, "category"); |
---|
| 767 | type = xt_find_attr(c, "type"); |
---|
[dc0ba9c] | 768 | |
---|
[5ebff60] | 769 | if (type && (strcmp(type, "bytestreams") == 0) && |
---|
| 770 | category && (strcmp(category, "proxy") == 0)) { |
---|
| 771 | jabber_iq_query_server(ic, from, XMLNS_BYTESTREAMS); |
---|
| 772 | } |
---|
[dc0ba9c] | 773 | |
---|
| 774 | c = c->next; |
---|
| 775 | } |
---|
[5ebff60] | 776 | } else if (strcmp(xmlns, XMLNS_BYTESTREAMS) == 0) { |
---|
[c1a3c27] | 777 | char *host, *jid, *port_s; |
---|
[dc0ba9c] | 778 | int port; |
---|
| 779 | |
---|
| 780 | /* answer from proxy */ |
---|
| 781 | |
---|
[5ebff60] | 782 | if ((c = xt_find_node(c->children, "streamhost")) && |
---|
| 783 | (host = xt_find_attr(c, "host")) && |
---|
| 784 | (port_s = xt_find_attr(c, "port")) && |
---|
| 785 | (sscanf(port_s, "%d", &port) == 1) && |
---|
| 786 | (jid = xt_find_attr(c, "jid"))) { |
---|
| 787 | jabber_streamhost_t *sh = g_new0(jabber_streamhost_t, 1); |
---|
[dc0ba9c] | 788 | |
---|
[5ebff60] | 789 | sh->jid = g_strdup(jid); |
---|
| 790 | sh->host = g_strdup(host); |
---|
| 791 | g_snprintf(sh->port, sizeof(sh->port), "%u", port); |
---|
| 792 | |
---|
| 793 | imcb_log(ic, "Proxy found: jid %s host %s port %u", jid, host, port); |
---|
| 794 | jd->streamhosts = g_slist_append(jd->streamhosts, sh); |
---|
[dc0ba9c] | 795 | } |
---|
| 796 | } |
---|
| 797 | |
---|
[5ebff60] | 798 | if (jd->have_streamhosts == 0) { |
---|
[dc0ba9c] | 799 | jd->have_streamhosts++; |
---|
[5ebff60] | 800 | } |
---|
[1c3008a] | 801 | |
---|
[dc0ba9c] | 802 | return XT_HANDLED; |
---|
| 803 | } |
---|
[d88c92a] | 804 | |
---|
[5ebff60] | 805 | static xt_status jabber_iq_version_response(struct im_connection *ic, |
---|
| 806 | struct xt_node *node, struct xt_node *orig); |
---|
[d88c92a] | 807 | |
---|
[5ebff60] | 808 | void jabber_iq_version_send(struct im_connection *ic, struct jabber_buddy *bud, void *data) |
---|
[d88c92a] | 809 | { |
---|
| 810 | struct xt_node *node, *query; |
---|
| 811 | |
---|
[5ebff60] | 812 | node = xt_new_node("query", NULL, NULL); |
---|
| 813 | xt_add_attr(node, "xmlns", XMLNS_VERSION); |
---|
| 814 | query = jabber_make_packet("iq", "get", bud->full_jid, node); |
---|
| 815 | jabber_cache_add(ic, query, jabber_iq_version_response); |
---|
| 816 | |
---|
| 817 | jabber_write_packet(ic, query); |
---|
[d88c92a] | 818 | } |
---|
| 819 | |
---|
[5ebff60] | 820 | static xt_status jabber_iq_version_response(struct im_connection *ic, |
---|
| 821 | struct xt_node *node, struct xt_node *orig) |
---|
[d88c92a] | 822 | { |
---|
| 823 | struct xt_node *query; |
---|
| 824 | GString *rets; |
---|
| 825 | char *s; |
---|
| 826 | char *ret[2] = {}; |
---|
| 827 | bee_user_t *bu; |
---|
| 828 | struct jabber_buddy *bud = NULL; |
---|
[5ebff60] | 829 | |
---|
| 830 | if ((s = xt_find_attr(node, "from")) && |
---|
| 831 | (bud = jabber_buddy_by_jid(ic, s, 0)) && |
---|
| 832 | (query = xt_find_node(node->children, "query")) && |
---|
| 833 | (bu = bee_user_by_handle(ic->bee, ic, bud->bare_jid))) { |
---|
| 834 | rets = g_string_new("Resource "); |
---|
| 835 | g_string_append(rets, bud->resource); |
---|
| 836 | } else { |
---|
[d88c92a] | 837 | return XT_HANDLED; |
---|
[5ebff60] | 838 | } |
---|
| 839 | |
---|
| 840 | for (query = query->children; query; query = query->next) { |
---|
| 841 | if (query->text_len > 0) { |
---|
| 842 | g_string_append_printf(rets, " %s: %s,", query->name, query->text); |
---|
| 843 | } |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | g_string_truncate(rets, rets->len - 1); |
---|
[d88c92a] | 847 | ret[0] = rets->str; |
---|
[5ebff60] | 848 | imcb_buddy_action_response(bu, "VERSION", ret, NULL); |
---|
| 849 | g_string_free(rets, TRUE); |
---|
| 850 | |
---|
[d88c92a] | 851 | return XT_HANDLED; |
---|
| 852 | } |
---|
[bb2d198] | 853 | |
---|
[5ebff60] | 854 | static xt_status jabber_iq_disco_server_response(struct im_connection *ic, |
---|
| 855 | struct xt_node *node, struct xt_node *orig); |
---|
[bb2d198] | 856 | |
---|
[5ebff60] | 857 | static int jabber_iq_disco_server(struct im_connection *ic) |
---|
[bb2d198] | 858 | { |
---|
| 859 | struct xt_node *node, *iq; |
---|
| 860 | struct jabber_data *jd = ic->proto_data; |
---|
[5ebff60] | 861 | |
---|
| 862 | node = xt_new_node("query", NULL, NULL); |
---|
| 863 | xt_add_attr(node, "xmlns", XMLNS_DISCO_INFO); |
---|
| 864 | iq = jabber_make_packet("iq", "get", jd->server, node); |
---|
| 865 | |
---|
| 866 | jabber_cache_add(ic, iq, jabber_iq_disco_server_response); |
---|
| 867 | return jabber_write_packet(ic, iq); |
---|
[bb2d198] | 868 | } |
---|
| 869 | |
---|
[5ebff60] | 870 | static xt_status jabber_iq_disco_server_response(struct im_connection *ic, |
---|
| 871 | struct xt_node *node, struct xt_node *orig) |
---|
[bb2d198] | 872 | { |
---|
| 873 | struct jabber_data *jd = ic->proto_data; |
---|
| 874 | struct xt_node *id; |
---|
[5ebff60] | 875 | |
---|
| 876 | if ((id = xt_find_path(node, "query/identity"))) { |
---|
[bb2d198] | 877 | char *cat, *type, *name; |
---|
[5ebff60] | 878 | |
---|
| 879 | if (!(cat = xt_find_attr(id, "category")) || |
---|
| 880 | !(type = xt_find_attr(id, "type")) || |
---|
| 881 | !(name = xt_find_attr(id, "name"))) { |
---|
[bb2d198] | 882 | return XT_HANDLED; |
---|
[5ebff60] | 883 | } |
---|
| 884 | |
---|
| 885 | if (strcmp(cat, "server") == 0 && strcmp(type, "im") == 0 && |
---|
| 886 | strstr(name, "Google") != NULL) { |
---|
[bb2d198] | 887 | jd->flags |= JFLAG_GTALK; |
---|
[5ebff60] | 888 | } |
---|
[bb2d198] | 889 | } |
---|
[5ebff60] | 890 | |
---|
[bb2d198] | 891 | return XT_HANDLED; |
---|
| 892 | } |
---|