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