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