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