[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 | { |
---|
[529078a] | 52 | if( !( ( c = xt_find_node( node->children, "query" ) ) || |
---|
[74349eb] | 53 | ( c = xt_find_node( node->children, "ping" ) ) ) || |
---|
[58b5f62] | 54 | !( s = xt_find_attr( c, "xmlns" ) ) ) |
---|
| 55 | { |
---|
[74349eb] | 56 | /* Sigh. Who decided to suddenly invent new elements |
---|
| 57 | instead of just sticking with <query/>? */ |
---|
[58b5f62] | 58 | return XT_HANDLED; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | reply = xt_new_node( "query", NULL, NULL ); |
---|
| 62 | xt_add_attr( reply, "xmlns", s ); |
---|
| 63 | |
---|
| 64 | /* Of course this is a very essential query to support. ;-) */ |
---|
[47d3ac4] | 65 | if( strcmp( s, XMLNS_VERSION ) == 0 ) |
---|
[58b5f62] | 66 | { |
---|
[4eef271] | 67 | xt_add_child( reply, xt_new_node( "name", set_getstr( &ic->acc->set, "user_agent" ), NULL ) ); |
---|
[58b5f62] | 68 | xt_add_child( reply, xt_new_node( "version", BITLBEE_VERSION, NULL ) ); |
---|
| 69 | xt_add_child( reply, xt_new_node( "os", ARCH, NULL ) ); |
---|
| 70 | } |
---|
[47d3ac4] | 71 | else if( strcmp( s, XMLNS_TIME ) == 0 ) |
---|
[a4effbf] | 72 | { |
---|
| 73 | time_t time_ep; |
---|
| 74 | char buf[1024]; |
---|
| 75 | |
---|
| 76 | buf[sizeof(buf)-1] = 0; |
---|
| 77 | time_ep = time( NULL ); |
---|
| 78 | |
---|
| 79 | strftime( buf, sizeof( buf ) - 1, "%Y%m%dT%H:%M:%S", gmtime( &time_ep ) ); |
---|
| 80 | xt_add_child( reply, xt_new_node( "utc", buf, NULL ) ); |
---|
| 81 | |
---|
| 82 | strftime( buf, sizeof( buf ) - 1, "%Z", localtime( &time_ep ) ); |
---|
| 83 | xt_add_child( reply, xt_new_node( "tz", buf, NULL ) ); |
---|
| 84 | } |
---|
[529078a] | 85 | else if( strcmp( s, XMLNS_PING ) == 0 ) |
---|
| 86 | { |
---|
| 87 | xt_free_node( reply ); |
---|
| 88 | reply = jabber_make_packet( "iq", "result", xt_find_attr( node, "from" ), NULL ); |
---|
| 89 | if( ( s = xt_find_attr( node, "id" ) ) ) |
---|
| 90 | xt_add_attr( reply, "id", s ); |
---|
| 91 | pack = 0; |
---|
| 92 | } |
---|
[47d3ac4] | 93 | else if( strcmp( s, XMLNS_DISCOVER ) == 0 ) |
---|
[58b5f62] | 94 | { |
---|
[8c1eb80] | 95 | const char *features[] = { XMLNS_DISCOVER, |
---|
| 96 | XMLNS_VERSION, |
---|
[40ef702] | 97 | XMLNS_TIME, |
---|
| 98 | XMLNS_CHATSTATES, |
---|
| 99 | XMLNS_MUC, |
---|
[529078a] | 100 | XMLNS_PING, |
---|
[40ef702] | 101 | NULL }; |
---|
| 102 | const char **f; |
---|
| 103 | |
---|
[58b5f62] | 104 | c = xt_new_node( "identity", NULL, NULL ); |
---|
| 105 | xt_add_attr( c, "category", "client" ); |
---|
| 106 | xt_add_attr( c, "type", "pc" ); |
---|
[4eef271] | 107 | xt_add_attr( c, "name", set_getstr( &ic->acc->set, "user_agent" ) ); |
---|
[58b5f62] | 108 | xt_add_child( reply, c ); |
---|
| 109 | |
---|
[40ef702] | 110 | for( f = features; *f; f ++ ) |
---|
| 111 | { |
---|
| 112 | c = xt_new_node( "feature", NULL, NULL ); |
---|
| 113 | xt_add_attr( c, "var", *f ); |
---|
| 114 | xt_add_child( reply, c ); |
---|
| 115 | } |
---|
[58b5f62] | 116 | } |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | xt_free_node( reply ); |
---|
[259edd4] | 120 | reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" ); |
---|
| 121 | pack = 0; |
---|
[58b5f62] | 122 | } |
---|
[259edd4] | 123 | } |
---|
| 124 | else if( strcmp( type, "set" ) == 0 ) |
---|
| 125 | { |
---|
[dfa41a4] | 126 | if( !( c = xt_find_node( node->children, "query" ) ) || |
---|
| 127 | !( s = xt_find_attr( c, "xmlns" ) ) ) |
---|
| 128 | { |
---|
| 129 | return XT_HANDLED; |
---|
| 130 | } |
---|
| 131 | |
---|
[9bcbe48] | 132 | /* This is a roster push. XMPP servers send this when someone |
---|
| 133 | was added to (or removed from) the buddy list. AFAIK they're |
---|
| 134 | sent even if we added this buddy in our own session. */ |
---|
[47d3ac4] | 135 | if( strcmp( s, XMLNS_ROSTER ) == 0 ) |
---|
[dfa41a4] | 136 | { |
---|
[0da65d5] | 137 | int bare_len = strlen( ic->acc->user ); |
---|
[abbd8ed] | 138 | |
---|
| 139 | if( ( s = xt_find_attr( node, "from" ) ) == NULL || |
---|
[0da65d5] | 140 | ( strncmp( s, ic->acc->user, bare_len ) == 0 && |
---|
[abbd8ed] | 141 | ( s[bare_len] == 0 || s[bare_len] == '/' ) ) ) |
---|
| 142 | { |
---|
[0da65d5] | 143 | jabber_parse_roster( ic, node, NULL ); |
---|
[abbd8ed] | 144 | |
---|
| 145 | /* Should we generate a reply here? Don't think it's |
---|
| 146 | very important... */ |
---|
| 147 | } |
---|
| 148 | else |
---|
| 149 | { |
---|
[43462708] | 150 | imcb_log( ic, "Warning: %s tried to fake a roster push!", s ? s : "(unknown)" ); |
---|
[abbd8ed] | 151 | |
---|
| 152 | xt_free_node( reply ); |
---|
| 153 | reply = jabber_make_error_packet( node, "not-allowed", "cancel" ); |
---|
| 154 | pack = 0; |
---|
| 155 | } |
---|
[dfa41a4] | 156 | } |
---|
| 157 | else |
---|
| 158 | { |
---|
| 159 | xt_free_node( reply ); |
---|
| 160 | reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" ); |
---|
| 161 | pack = 0; |
---|
| 162 | } |
---|
[259edd4] | 163 | } |
---|
| 164 | |
---|
| 165 | /* If we recognized the xmlns and managed to generate a reply, |
---|
| 166 | finish and send it. */ |
---|
| 167 | if( reply ) |
---|
| 168 | { |
---|
| 169 | /* Normally we still have to pack it into an iq-result |
---|
| 170 | packet, but for errors, for example, we don't. */ |
---|
| 171 | if( pack ) |
---|
[58b5f62] | 172 | { |
---|
| 173 | reply = jabber_make_packet( "iq", "result", xt_find_attr( node, "from" ), reply ); |
---|
| 174 | if( ( s = xt_find_attr( node, "id" ) ) ) |
---|
| 175 | xt_add_attr( reply, "id", s ); |
---|
| 176 | } |
---|
[259edd4] | 177 | |
---|
[0da65d5] | 178 | st = jabber_write_packet( ic, reply ); |
---|
[259edd4] | 179 | xt_free_node( reply ); |
---|
| 180 | if( !st ) |
---|
| 181 | return XT_ABORT; |
---|
[58b5f62] | 182 | } |
---|
[70f6aab8] | 183 | |
---|
[861c199] | 184 | return XT_HANDLED; |
---|
| 185 | } |
---|
| 186 | |
---|
[0da65d5] | 187 | static xt_status jabber_do_iq_auth( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); |
---|
| 188 | static xt_status jabber_finish_iq_auth( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); |
---|
[861c199] | 189 | |
---|
[0da65d5] | 190 | int jabber_init_iq_auth( struct im_connection *ic ) |
---|
[861c199] | 191 | { |
---|
[0da65d5] | 192 | struct jabber_data *jd = ic->proto_data; |
---|
[861c199] | 193 | struct xt_node *node; |
---|
| 194 | int st; |
---|
[fe7a554] | 195 | |
---|
[861c199] | 196 | node = xt_new_node( "query", NULL, xt_new_node( "username", jd->username, NULL ) ); |
---|
[47d3ac4] | 197 | xt_add_attr( node, "xmlns", XMLNS_AUTH ); |
---|
[861c199] | 198 | node = jabber_make_packet( "iq", "get", NULL, node ); |
---|
| 199 | |
---|
[0da65d5] | 200 | jabber_cache_add( ic, node, jabber_do_iq_auth ); |
---|
| 201 | st = jabber_write_packet( ic, node ); |
---|
[861c199] | 202 | |
---|
| 203 | return st; |
---|
| 204 | } |
---|
| 205 | |
---|
[0da65d5] | 206 | static xt_status jabber_do_iq_auth( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
[861c199] | 207 | { |
---|
[0da65d5] | 208 | struct jabber_data *jd = ic->proto_data; |
---|
[861c199] | 209 | struct xt_node *reply, *query; |
---|
| 210 | xt_status st; |
---|
| 211 | char *s; |
---|
| 212 | |
---|
[9bcbe48] | 213 | if( !( query = xt_find_node( node->children, "query" ) ) ) |
---|
| 214 | { |
---|
[43462708] | 215 | imcb_log( ic, "Warning: Received incomplete IQ packet while authenticating" ); |
---|
[c2fb3809] | 216 | imc_logout( ic, FALSE ); |
---|
[9bcbe48] | 217 | return XT_HANDLED; |
---|
| 218 | } |
---|
[861c199] | 219 | |
---|
| 220 | /* Time to authenticate ourselves! */ |
---|
| 221 | reply = xt_new_node( "query", NULL, NULL ); |
---|
[47d3ac4] | 222 | xt_add_attr( reply, "xmlns", XMLNS_AUTH ); |
---|
[861c199] | 223 | xt_add_child( reply, xt_new_node( "username", jd->username, NULL ) ); |
---|
[0da65d5] | 224 | xt_add_child( reply, xt_new_node( "resource", set_getstr( &ic->acc->set, "resource" ), NULL ) ); |
---|
[861c199] | 225 | |
---|
| 226 | if( xt_find_node( query->children, "digest" ) && ( s = xt_find_attr( jd->xt->root, "id" ) ) ) |
---|
[21167d2] | 227 | { |
---|
[861c199] | 228 | /* We can do digest authentication, it seems, and of |
---|
| 229 | course we prefer that. */ |
---|
[77bfd07] | 230 | sha1_state_t sha; |
---|
[e727608] | 231 | char hash_hex[41]; |
---|
[861c199] | 232 | unsigned char hash[20]; |
---|
| 233 | int i; |
---|
[21167d2] | 234 | |
---|
[77bfd07] | 235 | sha1_init( &sha ); |
---|
| 236 | sha1_append( &sha, (unsigned char*) s, strlen( s ) ); |
---|
| 237 | sha1_append( &sha, (unsigned char*) ic->acc->pass, strlen( ic->acc->pass ) ); |
---|
| 238 | sha1_finish( &sha, hash ); |
---|
[21167d2] | 239 | |
---|
[861c199] | 240 | for( i = 0; i < 20; i ++ ) |
---|
| 241 | sprintf( hash_hex + i * 2, "%02x", hash[i] ); |
---|
[21167d2] | 242 | |
---|
[861c199] | 243 | xt_add_child( reply, xt_new_node( "digest", hash_hex, NULL ) ); |
---|
[21167d2] | 244 | } |
---|
[861c199] | 245 | else if( xt_find_node( query->children, "password" ) ) |
---|
[0b4a0db] | 246 | { |
---|
[861c199] | 247 | /* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */ |
---|
[0da65d5] | 248 | xt_add_child( reply, xt_new_node( "password", ic->acc->pass, NULL ) ); |
---|
[0b4a0db] | 249 | } |
---|
[861c199] | 250 | else |
---|
[70f6aab8] | 251 | { |
---|
[861c199] | 252 | xt_free_node( reply ); |
---|
[995913b] | 253 | |
---|
[84b045d] | 254 | imcb_error( ic, "Can't find suitable authentication method" ); |
---|
[c2fb3809] | 255 | imc_logout( ic, FALSE ); |
---|
[861c199] | 256 | return XT_ABORT; |
---|
[70f6aab8] | 257 | } |
---|
[861c199] | 258 | |
---|
| 259 | reply = jabber_make_packet( "iq", "set", NULL, reply ); |
---|
[0da65d5] | 260 | jabber_cache_add( ic, reply, jabber_finish_iq_auth ); |
---|
| 261 | st = jabber_write_packet( ic, reply ); |
---|
[861c199] | 262 | |
---|
| 263 | return st ? XT_HANDLED : XT_ABORT; |
---|
| 264 | } |
---|
| 265 | |
---|
[0da65d5] | 266 | static xt_status jabber_finish_iq_auth( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
[861c199] | 267 | { |
---|
[0da65d5] | 268 | struct jabber_data *jd = ic->proto_data; |
---|
[9bcbe48] | 269 | char *type; |
---|
| 270 | |
---|
| 271 | if( !( type = xt_find_attr( node, "type" ) ) ) |
---|
| 272 | { |
---|
[43462708] | 273 | imcb_log( ic, "Warning: Received incomplete IQ packet while authenticating" ); |
---|
[c2fb3809] | 274 | imc_logout( ic, FALSE ); |
---|
[9bcbe48] | 275 | return XT_HANDLED; |
---|
| 276 | } |
---|
[861c199] | 277 | |
---|
| 278 | if( strcmp( type, "error" ) == 0 ) |
---|
[70f6aab8] | 279 | { |
---|
[84b045d] | 280 | imcb_error( ic, "Authentication failure" ); |
---|
[c2fb3809] | 281 | imc_logout( ic, FALSE ); |
---|
[861c199] | 282 | return XT_ABORT; |
---|
| 283 | } |
---|
| 284 | else if( strcmp( type, "result" ) == 0 ) |
---|
| 285 | { |
---|
| 286 | /* This happens when we just successfully authenticated the |
---|
| 287 | old (non-SASL) way. */ |
---|
| 288 | jd->flags |= JFLAG_AUTHENTICATED; |
---|
[0da65d5] | 289 | if( !jabber_get_roster( ic ) ) |
---|
[70f6aab8] | 290 | return XT_ABORT; |
---|
| 291 | } |
---|
[f06894d] | 292 | |
---|
| 293 | return XT_HANDLED; |
---|
| 294 | } |
---|
| 295 | |
---|
[0da65d5] | 296 | xt_status jabber_pkt_bind_sess( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
[21167d2] | 297 | { |
---|
[0da65d5] | 298 | struct jabber_data *jd = ic->proto_data; |
---|
[8fb1263] | 299 | struct xt_node *c, *reply = NULL; |
---|
[861c199] | 300 | char *s; |
---|
[21167d2] | 301 | |
---|
[8fb1263] | 302 | if( node && ( c = xt_find_node( node->children, "bind" ) ) ) |
---|
[861c199] | 303 | { |
---|
| 304 | c = xt_find_node( c->children, "jid" ); |
---|
| 305 | if( c && c->text_len && ( s = strchr( c->text, '/' ) ) && |
---|
[0da65d5] | 306 | strcmp( s + 1, set_getstr( &ic->acc->set, "resource" ) ) != 0 ) |
---|
[84b045d] | 307 | imcb_log( ic, "Server changed session resource string to `%s'", s + 1 ); |
---|
[861c199] | 308 | } |
---|
[21167d2] | 309 | |
---|
[8fb1263] | 310 | if( jd->flags & JFLAG_WANT_BIND ) |
---|
| 311 | { |
---|
| 312 | reply = xt_new_node( "bind", NULL, xt_new_node( "resource", set_getstr( &ic->acc->set, "resource" ), NULL ) ); |
---|
| 313 | xt_add_attr( reply, "xmlns", XMLNS_BIND ); |
---|
[315dd4c] | 314 | jd->flags &= ~JFLAG_WANT_BIND; |
---|
[8fb1263] | 315 | } |
---|
| 316 | else if( jd->flags & JFLAG_WANT_SESSION ) |
---|
| 317 | { |
---|
| 318 | reply = xt_new_node( "session", NULL, NULL ); |
---|
| 319 | xt_add_attr( reply, "xmlns", XMLNS_SESSION ); |
---|
[315dd4c] | 320 | jd->flags &= ~JFLAG_WANT_SESSION; |
---|
[8fb1263] | 321 | } |
---|
| 322 | |
---|
| 323 | if( reply != NULL ) |
---|
| 324 | { |
---|
| 325 | reply = jabber_make_packet( "iq", "set", NULL, reply ); |
---|
| 326 | jabber_cache_add( ic, reply, jabber_pkt_bind_sess ); |
---|
| 327 | |
---|
| 328 | if( !jabber_write_packet( ic, reply ) ) |
---|
| 329 | return XT_ABORT; |
---|
| 330 | } |
---|
| 331 | else if( ( jd->flags & ( JFLAG_WANT_BIND | JFLAG_WANT_SESSION ) ) == 0 ) |
---|
[861c199] | 332 | { |
---|
[0da65d5] | 333 | if( !jabber_get_roster( ic ) ) |
---|
[861c199] | 334 | return XT_ABORT; |
---|
| 335 | } |
---|
[21167d2] | 336 | |
---|
[861c199] | 337 | return XT_HANDLED; |
---|
[21167d2] | 338 | } |
---|
[70f6aab8] | 339 | |
---|
[0da65d5] | 340 | int jabber_get_roster( struct im_connection *ic ) |
---|
[70f6aab8] | 341 | { |
---|
| 342 | struct xt_node *node; |
---|
| 343 | int st; |
---|
| 344 | |
---|
[84b045d] | 345 | imcb_log( ic, "Authenticated, requesting buddy list" ); |
---|
[70f6aab8] | 346 | |
---|
| 347 | node = xt_new_node( "query", NULL, NULL ); |
---|
[47d3ac4] | 348 | xt_add_attr( node, "xmlns", XMLNS_ROSTER ); |
---|
[70f6aab8] | 349 | node = jabber_make_packet( "iq", "get", NULL, node ); |
---|
| 350 | |
---|
[0da65d5] | 351 | jabber_cache_add( ic, node, jabber_parse_roster ); |
---|
| 352 | st = jabber_write_packet( ic, node ); |
---|
[70f6aab8] | 353 | |
---|
| 354 | return st; |
---|
| 355 | } |
---|
[cfbb3a6] | 356 | |
---|
[0da65d5] | 357 | static xt_status jabber_parse_roster( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
[861c199] | 358 | { |
---|
| 359 | struct xt_node *query, *c; |
---|
[abbd8ed] | 360 | int initial = ( orig != NULL ); |
---|
[861c199] | 361 | |
---|
[9bcbe48] | 362 | if( !( query = xt_find_node( node->children, "query" ) ) ) |
---|
| 363 | { |
---|
[43462708] | 364 | imcb_log( ic, "Warning: Received NULL roster packet" ); |
---|
[9bcbe48] | 365 | return XT_HANDLED; |
---|
| 366 | } |
---|
[861c199] | 367 | |
---|
| 368 | c = query->children; |
---|
| 369 | while( ( c = xt_find_node( c, "item" ) ) ) |
---|
| 370 | { |
---|
[f0cb961] | 371 | struct xt_node *group = xt_find_node( node->children, "group" ); |
---|
[861c199] | 372 | char *jid = xt_find_attr( c, "jid" ); |
---|
| 373 | char *name = xt_find_attr( c, "name" ); |
---|
| 374 | char *sub = xt_find_attr( c, "subscription" ); |
---|
| 375 | |
---|
[f0cb961] | 376 | if( jid && sub ) |
---|
[abbd8ed] | 377 | { |
---|
| 378 | if( ( strcmp( sub, "both" ) == 0 || strcmp( sub, "to" ) == 0 ) ) |
---|
| 379 | { |
---|
[f0cb961] | 380 | if( initial || imcb_find_buddy( ic, jid ) == NULL ) |
---|
| 381 | imcb_add_buddy( ic, jid, ( group && group->text_len ) ? |
---|
| 382 | group->text : NULL ); |
---|
| 383 | |
---|
[d06eabf] | 384 | if( name ) |
---|
| 385 | imcb_rename_buddy( ic, jid, name ); |
---|
[abbd8ed] | 386 | } |
---|
| 387 | else if( strcmp( sub, "remove" ) == 0 ) |
---|
| 388 | { |
---|
[0da65d5] | 389 | jabber_buddy_remove_bare( ic, jid ); |
---|
[998b103] | 390 | imcb_remove_buddy( ic, jid, NULL ); |
---|
[abbd8ed] | 391 | } |
---|
| 392 | } |
---|
[861c199] | 393 | |
---|
| 394 | c = c->next; |
---|
| 395 | } |
---|
| 396 | |
---|
[abbd8ed] | 397 | if( initial ) |
---|
[84b045d] | 398 | imcb_connected( ic ); |
---|
[861c199] | 399 | |
---|
| 400 | return XT_HANDLED; |
---|
| 401 | } |
---|
| 402 | |
---|
[0da65d5] | 403 | int jabber_get_vcard( struct im_connection *ic, char *bare_jid ) |
---|
[1991be6] | 404 | { |
---|
| 405 | struct xt_node *node; |
---|
| 406 | |
---|
| 407 | if( strchr( bare_jid, '/' ) ) |
---|
| 408 | return 1; /* This was an error, but return 0 should only be done if the connection died... */ |
---|
| 409 | |
---|
| 410 | node = xt_new_node( "vCard", NULL, NULL ); |
---|
[47d3ac4] | 411 | xt_add_attr( node, "xmlns", XMLNS_VCARD ); |
---|
[1991be6] | 412 | node = jabber_make_packet( "iq", "get", bare_jid, node ); |
---|
| 413 | |
---|
[0da65d5] | 414 | jabber_cache_add( ic, node, jabber_iq_display_vcard ); |
---|
| 415 | return jabber_write_packet( ic, node ); |
---|
[1991be6] | 416 | } |
---|
| 417 | |
---|
[0da65d5] | 418 | static xt_status jabber_iq_display_vcard( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
[1991be6] | 419 | { |
---|
[0da65d5] | 420 | struct xt_node *vc, *c, *sc; /* subchild, ic is already in use ;-) */ |
---|
[1991be6] | 421 | GString *reply; |
---|
| 422 | char *s; |
---|
| 423 | |
---|
| 424 | if( ( s = xt_find_attr( node, "type" ) ) == NULL || |
---|
| 425 | strcmp( s, "result" ) != 0 || |
---|
| 426 | ( vc = xt_find_node( node->children, "vCard" ) ) == NULL ) |
---|
| 427 | { |
---|
| 428 | s = xt_find_attr( orig, "to" ); /* If this returns NULL something's wrong.. */ |
---|
[84b045d] | 429 | imcb_log( ic, "Could not retrieve vCard of %s", s ? s : "(NULL)" ); |
---|
[1991be6] | 430 | return XT_HANDLED; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | s = xt_find_attr( orig, "to" ); |
---|
| 434 | reply = g_string_new( "vCard information for " ); |
---|
| 435 | reply = g_string_append( reply, s ? s : "(NULL)" ); |
---|
| 436 | reply = g_string_append( reply, ":\n" ); |
---|
| 437 | |
---|
| 438 | /* I hate this format, I really do... */ |
---|
| 439 | |
---|
| 440 | if( ( c = xt_find_node( vc->children, "FN" ) ) && c->text_len ) |
---|
| 441 | g_string_append_printf( reply, "Name: %s\n", c->text ); |
---|
| 442 | |
---|
| 443 | if( ( c = xt_find_node( vc->children, "N" ) ) && c->children ) |
---|
| 444 | { |
---|
| 445 | reply = g_string_append( reply, "Full name:" ); |
---|
| 446 | |
---|
| 447 | if( ( sc = xt_find_node( c->children, "PREFIX" ) ) && sc->text_len ) |
---|
| 448 | g_string_append_printf( reply, " %s", sc->text ); |
---|
| 449 | if( ( sc = xt_find_node( c->children, "GIVEN" ) ) && sc->text_len ) |
---|
| 450 | g_string_append_printf( reply, " %s", sc->text ); |
---|
| 451 | if( ( sc = xt_find_node( c->children, "MIDDLE" ) ) && sc->text_len ) |
---|
| 452 | g_string_append_printf( reply, " %s", sc->text ); |
---|
| 453 | if( ( sc = xt_find_node( c->children, "FAMILY" ) ) && sc->text_len ) |
---|
| 454 | g_string_append_printf( reply, " %s", sc->text ); |
---|
| 455 | if( ( sc = xt_find_node( c->children, "SUFFIX" ) ) && sc->text_len ) |
---|
| 456 | g_string_append_printf( reply, " %s", sc->text ); |
---|
| 457 | |
---|
| 458 | reply = g_string_append_c( reply, '\n' ); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | if( ( c = xt_find_node( vc->children, "NICKNAME" ) ) && c->text_len ) |
---|
| 462 | g_string_append_printf( reply, "Nickname: %s\n", c->text ); |
---|
| 463 | |
---|
| 464 | if( ( c = xt_find_node( vc->children, "BDAY" ) ) && c->text_len ) |
---|
| 465 | g_string_append_printf( reply, "Date of birth: %s\n", c->text ); |
---|
| 466 | |
---|
| 467 | /* Slightly alternative use of for... ;-) */ |
---|
| 468 | for( c = vc->children; ( c = xt_find_node( c, "EMAIL" ) ); c = c->next ) |
---|
| 469 | { |
---|
| 470 | if( ( sc = xt_find_node( c->children, "USERID" ) ) == NULL || sc->text_len == 0 ) |
---|
| 471 | continue; |
---|
| 472 | |
---|
| 473 | if( xt_find_node( c->children, "HOME" ) ) |
---|
| 474 | s = "Home"; |
---|
| 475 | else if( xt_find_node( c->children, "WORK" ) ) |
---|
| 476 | s = "Work"; |
---|
| 477 | else |
---|
| 478 | s = "Misc."; |
---|
| 479 | |
---|
| 480 | g_string_append_printf( reply, "%s e-mail address: %s\n", s, sc->text ); |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | if( ( c = xt_find_node( vc->children, "URL" ) ) && c->text_len ) |
---|
| 484 | g_string_append_printf( reply, "Homepage: %s\n", c->text ); |
---|
| 485 | |
---|
| 486 | /* Slightly alternative use of for... ;-) */ |
---|
| 487 | for( c = vc->children; ( c = xt_find_node( c, "ADR" ) ); c = c->next ) |
---|
| 488 | { |
---|
| 489 | if( xt_find_node( c->children, "HOME" ) ) |
---|
| 490 | s = "Home"; |
---|
| 491 | else if( xt_find_node( c->children, "WORK" ) ) |
---|
| 492 | s = "Work"; |
---|
| 493 | else |
---|
| 494 | s = "Misc."; |
---|
| 495 | |
---|
| 496 | g_string_append_printf( reply, "%s address: ", s ); |
---|
| 497 | |
---|
| 498 | if( ( sc = xt_find_node( c->children, "STREET" ) ) && sc->text_len ) |
---|
| 499 | g_string_append_printf( reply, "%s ", sc->text ); |
---|
| 500 | if( ( sc = xt_find_node( c->children, "EXTADR" ) ) && sc->text_len ) |
---|
| 501 | g_string_append_printf( reply, "%s, ", sc->text ); |
---|
| 502 | if( ( sc = xt_find_node( c->children, "PCODE" ) ) && sc->text_len ) |
---|
| 503 | g_string_append_printf( reply, "%s, ", sc->text ); |
---|
| 504 | if( ( sc = xt_find_node( c->children, "LOCALITY" ) ) && sc->text_len ) |
---|
| 505 | g_string_append_printf( reply, "%s, ", sc->text ); |
---|
| 506 | if( ( sc = xt_find_node( c->children, "REGION" ) ) && sc->text_len ) |
---|
| 507 | g_string_append_printf( reply, "%s, ", sc->text ); |
---|
| 508 | if( ( sc = xt_find_node( c->children, "CTRY" ) ) && sc->text_len ) |
---|
| 509 | g_string_append_printf( reply, "%s", sc->text ); |
---|
| 510 | |
---|
| 511 | if( reply->str[reply->len-2] == ',' ) |
---|
| 512 | reply = g_string_truncate( reply, reply->len-2 ); |
---|
| 513 | |
---|
| 514 | reply = g_string_append_c( reply, '\n' ); |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | for( c = vc->children; ( c = xt_find_node( c, "TEL" ) ); c = c->next ) |
---|
| 518 | { |
---|
| 519 | if( ( sc = xt_find_node( c->children, "NUMBER" ) ) == NULL || sc->text_len == 0 ) |
---|
| 520 | continue; |
---|
| 521 | |
---|
| 522 | if( xt_find_node( c->children, "HOME" ) ) |
---|
| 523 | s = "Home"; |
---|
| 524 | else if( xt_find_node( c->children, "WORK" ) ) |
---|
| 525 | s = "Work"; |
---|
| 526 | else |
---|
| 527 | s = "Misc."; |
---|
| 528 | |
---|
| 529 | g_string_append_printf( reply, "%s phone number: %s\n", s, sc->text ); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | if( ( c = xt_find_node( vc->children, "DESC" ) ) && c->text_len ) |
---|
| 533 | g_string_append_printf( reply, "Other information:\n%s", c->text ); |
---|
| 534 | |
---|
| 535 | /* *sigh* */ |
---|
| 536 | |
---|
[84b045d] | 537 | imcb_log( ic, "%s", reply->str ); |
---|
[1991be6] | 538 | g_string_free( reply, TRUE ); |
---|
| 539 | |
---|
| 540 | return XT_HANDLED; |
---|
| 541 | } |
---|
| 542 | |
---|
[a73e91a] | 543 | static xt_status jabber_add_to_roster_callback( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); |
---|
| 544 | |
---|
[0da65d5] | 545 | int jabber_add_to_roster( struct im_connection *ic, char *handle, char *name ) |
---|
[cfbb3a6] | 546 | { |
---|
| 547 | struct xt_node *node; |
---|
| 548 | int st; |
---|
| 549 | |
---|
| 550 | /* Build the item entry */ |
---|
| 551 | node = xt_new_node( "item", NULL, NULL ); |
---|
| 552 | xt_add_attr( node, "jid", handle ); |
---|
| 553 | if( name ) |
---|
| 554 | xt_add_attr( node, "name", name ); |
---|
| 555 | |
---|
| 556 | /* And pack it into a roster-add packet */ |
---|
| 557 | node = xt_new_node( "query", NULL, node ); |
---|
[47d3ac4] | 558 | xt_add_attr( node, "xmlns", XMLNS_ROSTER ); |
---|
[cfbb3a6] | 559 | node = jabber_make_packet( "iq", "set", NULL, node ); |
---|
[a73e91a] | 560 | jabber_cache_add( ic, node, jabber_add_to_roster_callback ); |
---|
[cfbb3a6] | 561 | |
---|
[0da65d5] | 562 | st = jabber_write_packet( ic, node ); |
---|
[cfbb3a6] | 563 | |
---|
| 564 | return st; |
---|
| 565 | } |
---|
| 566 | |
---|
[a73e91a] | 567 | static xt_status jabber_add_to_roster_callback( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
| 568 | { |
---|
| 569 | char *s, *jid = NULL; |
---|
| 570 | struct xt_node *c; |
---|
| 571 | |
---|
| 572 | if( ( c = xt_find_node( orig->children, "query" ) ) && |
---|
| 573 | ( c = xt_find_node( c->children, "item" ) ) && |
---|
| 574 | ( jid = xt_find_attr( c, "jid" ) ) && |
---|
| 575 | ( s = xt_find_attr( node, "type" ) ) && |
---|
| 576 | strcmp( s, "result" ) == 0 ) |
---|
| 577 | { |
---|
| 578 | if( imcb_find_buddy( ic, jid ) == NULL ) |
---|
| 579 | imcb_add_buddy( ic, jid, NULL ); |
---|
| 580 | } |
---|
| 581 | else |
---|
| 582 | { |
---|
| 583 | imcb_log( ic, "Error while adding `%s' to your contact list.", |
---|
| 584 | jid ? jid : "(unknown handle)" ); |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | return XT_HANDLED; |
---|
| 588 | } |
---|
| 589 | |
---|
[0da65d5] | 590 | int jabber_remove_from_roster( struct im_connection *ic, char *handle ) |
---|
[cfbb3a6] | 591 | { |
---|
| 592 | struct xt_node *node; |
---|
| 593 | int st; |
---|
| 594 | |
---|
| 595 | /* Build the item entry */ |
---|
| 596 | node = xt_new_node( "item", NULL, NULL ); |
---|
| 597 | xt_add_attr( node, "jid", handle ); |
---|
| 598 | xt_add_attr( node, "subscription", "remove" ); |
---|
| 599 | |
---|
| 600 | /* And pack it into a roster-add packet */ |
---|
| 601 | node = xt_new_node( "query", NULL, node ); |
---|
[47d3ac4] | 602 | xt_add_attr( node, "xmlns", XMLNS_ROSTER ); |
---|
[cfbb3a6] | 603 | node = jabber_make_packet( "iq", "set", NULL, node ); |
---|
| 604 | |
---|
[0da65d5] | 605 | st = jabber_write_packet( ic, node ); |
---|
[cfbb3a6] | 606 | |
---|
| 607 | xt_free_node( node ); |
---|
| 608 | return st; |
---|
| 609 | } |
---|