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