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