[f06894d] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - Main file * |
---|
| 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 <glib.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <unistd.h> |
---|
| 27 | #include <ctype.h> |
---|
| 28 | #include <stdio.h> |
---|
| 29 | |
---|
[21167d2] | 30 | #include "ssl_client.h" |
---|
[f06894d] | 31 | #include "xmltree.h" |
---|
| 32 | #include "bitlbee.h" |
---|
| 33 | #include "jabber.h" |
---|
[e14b47b8] | 34 | #include "oauth.h" |
---|
[608f8cf] | 35 | #include "md5.h" |
---|
[f06894d] | 36 | |
---|
[b5c8a34] | 37 | GSList *jabber_connections; |
---|
| 38 | |
---|
[7f69740] | 39 | /* First enty is the default */ |
---|
| 40 | static const int jabber_port_list[] = { |
---|
| 41 | 5222, |
---|
| 42 | 5223, |
---|
| 43 | 5220, |
---|
| 44 | 5221, |
---|
| 45 | 5224, |
---|
| 46 | 5225, |
---|
| 47 | 5226, |
---|
| 48 | 5227, |
---|
| 49 | 5228, |
---|
| 50 | 5229, |
---|
| 51 | 80, |
---|
| 52 | 443, |
---|
| 53 | 0 |
---|
| 54 | }; |
---|
| 55 | |
---|
[0da65d5] | 56 | static void jabber_init( account_t *acc ) |
---|
[f06894d] | 57 | { |
---|
| 58 | set_t *s; |
---|
[7f69740] | 59 | char str[16]; |
---|
[f06894d] | 60 | |
---|
[76c85b4c] | 61 | s = set_add( &acc->set, "activity_timeout", "600", set_eval_int, acc ); |
---|
| 62 | |
---|
[ce199b7] | 63 | s = set_add( &acc->set, "oauth", "false", set_eval_oauth, acc ); |
---|
[57b4525] | 64 | |
---|
[7f69740] | 65 | g_snprintf( str, sizeof( str ), "%d", jabber_port_list[0] ); |
---|
| 66 | s = set_add( &acc->set, "port", str, set_eval_int, acc ); |
---|
[f06894d] | 67 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 68 | |
---|
[ebe7b36] | 69 | s = set_add( &acc->set, "priority", "0", set_eval_priority, acc ); |
---|
[1c3008a] | 70 | |
---|
| 71 | s = set_add( &acc->set, "proxy", "<local>;<auto>", NULL, acc ); |
---|
[f06894d] | 72 | |
---|
[ebe7b36] | 73 | s = set_add( &acc->set, "resource", "BitlBee", NULL, acc ); |
---|
| 74 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[f06894d] | 75 | |
---|
[76c85b4c] | 76 | s = set_add( &acc->set, "resource_select", "activity", NULL, acc ); |
---|
[a21a8ac] | 77 | |
---|
[ce199b7] | 78 | s = set_add( &acc->set, "sasl", "true", set_eval_bool, acc ); |
---|
| 79 | s->flags |= ACC_SET_OFFLINE_ONLY | SET_HIDDEN_DEFAULT; |
---|
| 80 | |
---|
[f06894d] | 81 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
[7125cb3] | 82 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK; |
---|
[f06894d] | 83 | |
---|
| 84 | s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc ); |
---|
| 85 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 86 | |
---|
| 87 | s = set_add( &acc->set, "tls", "try", set_eval_tls, acc ); |
---|
| 88 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[06b5893] | 89 | |
---|
[4eef271] | 90 | s = set_add( &acc->set, "user_agent", "BitlBee", NULL, acc ); |
---|
| 91 | |
---|
[a6df0b5] | 92 | s = set_add( &acc->set, "xmlconsole", "false", set_eval_bool, acc ); |
---|
| 93 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[840bba8] | 94 | |
---|
| 95 | acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE; |
---|
[f06894d] | 96 | } |
---|
| 97 | |
---|
[608f8cf] | 98 | static void jabber_generate_id_hash( struct jabber_data *jd ); |
---|
| 99 | |
---|
[f06894d] | 100 | static void jabber_login( account_t *acc ) |
---|
| 101 | { |
---|
[84b045d] | 102 | struct im_connection *ic = imcb_new( acc ); |
---|
[21167d2] | 103 | struct jabber_data *jd = g_new0( struct jabber_data, 1 ); |
---|
[4a5d885] | 104 | char *s; |
---|
[21167d2] | 105 | |
---|
[b5c8a34] | 106 | /* For now this is needed in the _connected() handlers if using |
---|
| 107 | GLib event handling, to make sure we're not handling events |
---|
| 108 | on dead connections. */ |
---|
| 109 | jabber_connections = g_slist_prepend( jabber_connections, ic ); |
---|
| 110 | |
---|
[0da65d5] | 111 | jd->ic = ic; |
---|
| 112 | ic->proto_data = jd; |
---|
[21167d2] | 113 | |
---|
[68286eb] | 114 | jabber_set_me( ic, acc->user ); |
---|
[21167d2] | 115 | |
---|
[de03374] | 116 | jd->fd = jd->r_inpa = jd->w_inpa = -1; |
---|
| 117 | |
---|
[21167d2] | 118 | if( jd->server == NULL ) |
---|
| 119 | { |
---|
[84b045d] | 120 | imcb_error( ic, "Incomplete account name (format it like <username@jabberserver.name>)" ); |
---|
[c2fb3809] | 121 | imc_logout( ic, FALSE ); |
---|
[21167d2] | 122 | return; |
---|
| 123 | } |
---|
| 124 | |
---|
[3b3cd693] | 125 | if( ( s = strchr( jd->server, '/' ) ) ) |
---|
| 126 | { |
---|
| 127 | *s = 0; |
---|
| 128 | set_setstr( &acc->set, "resource", s + 1 ); |
---|
| 129 | |
---|
| 130 | /* Also remove the /resource from the original variable so we |
---|
| 131 | won't have to do this again every time. */ |
---|
| 132 | s = strchr( acc->user, '/' ); |
---|
| 133 | *s = 0; |
---|
| 134 | } |
---|
| 135 | |
---|
[038d17f] | 136 | jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free ); |
---|
[6a1128d] | 137 | jd->buddies = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
[fe7a554] | 138 | |
---|
[4a5d885] | 139 | if( set_getbool( &acc->set, "oauth" ) ) |
---|
| 140 | { |
---|
[e14b47b8] | 141 | GSList *p_in = NULL; |
---|
| 142 | const char *tok; |
---|
| 143 | |
---|
[f138bd2] | 144 | jd->fd = jd->r_inpa = jd->w_inpa = -1; |
---|
| 145 | |
---|
[18c6d36] | 146 | if( strstr( jd->server, ".live.com" ) ) |
---|
| 147 | jd->oauth2_service = &oauth2_service_mslive; |
---|
| 148 | else if( strstr( jd->server, ".facebook.com" ) ) |
---|
| 149 | jd->oauth2_service = &oauth2_service_facebook; |
---|
| 150 | else |
---|
| 151 | jd->oauth2_service = &oauth2_service_google; |
---|
| 152 | |
---|
[e14b47b8] | 153 | oauth_params_parse( &p_in, ic->acc->pass ); |
---|
| 154 | |
---|
[64b6635] | 155 | /* First see if we have a refresh token, in which case any |
---|
| 156 | access token we *might* have has probably expired already |
---|
| 157 | anyway. */ |
---|
[e14b47b8] | 158 | if( ( tok = oauth_params_get( &p_in, "refresh_token" ) ) ) |
---|
[64b6635] | 159 | { |
---|
[e14b47b8] | 160 | sasl_oauth2_refresh( ic, tok ); |
---|
[64b6635] | 161 | } |
---|
| 162 | /* If we don't have a refresh token, let's hope the access |
---|
| 163 | token is still usable. */ |
---|
[e14b47b8] | 164 | else if( ( tok = oauth_params_get( &p_in, "access_token" ) ) ) |
---|
[64b6635] | 165 | { |
---|
[e14b47b8] | 166 | jd->oauth2_access_token = g_strdup( tok ); |
---|
[64b6635] | 167 | jabber_connect( ic ); |
---|
| 168 | } |
---|
| 169 | /* If we don't have any, start the OAuth process now. Don't |
---|
| 170 | even open an XMPP connection yet. */ |
---|
| 171 | else |
---|
[f988ad3] | 172 | { |
---|
[4a5d885] | 173 | sasl_oauth2_init( ic ); |
---|
[f988ad3] | 174 | ic->flags |= OPT_SLOW_LOGIN; |
---|
| 175 | } |
---|
[e14b47b8] | 176 | |
---|
| 177 | oauth_params_free( &p_in ); |
---|
[4a5d885] | 178 | } |
---|
| 179 | else |
---|
| 180 | jabber_connect( ic ); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /* Separate this from jabber_login() so we can do OAuth first if necessary. |
---|
| 184 | Putting this in io.c would probably be more correct. */ |
---|
| 185 | void jabber_connect( struct im_connection *ic ) |
---|
| 186 | { |
---|
| 187 | account_t *acc = ic->acc; |
---|
| 188 | struct jabber_data *jd = ic->proto_data; |
---|
| 189 | int i; |
---|
| 190 | char *connect_to; |
---|
| 191 | struct ns_srv_reply **srvl = NULL, *srv = NULL; |
---|
| 192 | |
---|
[36e9f62] | 193 | /* Figure out the hostname to connect to. */ |
---|
[3b3cd693] | 194 | if( acc->server && *acc->server ) |
---|
[36e9f62] | 195 | connect_to = acc->server; |
---|
[ffdf2e7] | 196 | else if( ( srvl = srv_lookup( "xmpp-client", "tcp", jd->server ) ) || |
---|
| 197 | ( srvl = srv_lookup( "jabber-client", "tcp", jd->server ) ) ) |
---|
| 198 | { |
---|
| 199 | /* Find the lowest-priority one. These usually come |
---|
| 200 | back in random/shuffled order. Not looking at |
---|
| 201 | weights etc for now. */ |
---|
| 202 | srv = *srvl; |
---|
| 203 | for( i = 1; srvl[i]; i ++ ) |
---|
| 204 | if( srvl[i]->prio < srv->prio ) |
---|
| 205 | srv = srvl[i]; |
---|
| 206 | |
---|
[36e9f62] | 207 | connect_to = srv->name; |
---|
[ffdf2e7] | 208 | } |
---|
[36e9f62] | 209 | else |
---|
| 210 | connect_to = jd->server; |
---|
| 211 | |
---|
[84b045d] | 212 | imcb_log( ic, "Connecting" ); |
---|
[35f6677] | 213 | |
---|
[7f69740] | 214 | for( i = 0; jabber_port_list[i] > 0; i ++ ) |
---|
| 215 | if( set_getint( &acc->set, "port" ) == jabber_port_list[i] ) |
---|
| 216 | break; |
---|
| 217 | |
---|
| 218 | if( jabber_port_list[i] == 0 ) |
---|
[0f4c1bb5] | 219 | { |
---|
[7f69740] | 220 | imcb_log( ic, "Illegal port number" ); |
---|
[c2fb3809] | 221 | imc_logout( ic, FALSE ); |
---|
[0f4c1bb5] | 222 | return; |
---|
| 223 | } |
---|
| 224 | |
---|
[36e9f62] | 225 | /* For non-SSL connections we can try to use the port # from the SRV |
---|
| 226 | reply, but let's not do that when using SSL, SSL usually runs on |
---|
| 227 | non-standard ports... */ |
---|
[21167d2] | 228 | if( set_getbool( &acc->set, "ssl" ) ) |
---|
| 229 | { |
---|
[0da65d5] | 230 | jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, ic ); |
---|
[3b3cd693] | 231 | jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1; |
---|
[21167d2] | 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
[0da65d5] | 235 | jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic ); |
---|
[21167d2] | 236 | } |
---|
[ffdf2e7] | 237 | srv_free( srvl ); |
---|
[35f6677] | 238 | |
---|
| 239 | if( jd->fd == -1 ) |
---|
| 240 | { |
---|
[84b045d] | 241 | imcb_error( ic, "Could not connect to server" ); |
---|
[c2fb3809] | 242 | imc_logout( ic, TRUE ); |
---|
[fb4ebcc5] | 243 | |
---|
| 244 | return; |
---|
[35f6677] | 245 | } |
---|
[a6df0b5] | 246 | |
---|
| 247 | if( set_getbool( &acc->set, "xmlconsole" ) ) |
---|
| 248 | { |
---|
| 249 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
| 250 | /* Shouldn't really do this at this stage already, maybe. But |
---|
| 251 | I think this shouldn't break anything. */ |
---|
| 252 | imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
| 253 | } |
---|
[608f8cf] | 254 | |
---|
| 255 | jabber_generate_id_hash( jd ); |
---|
| 256 | } |
---|
| 257 | |
---|
[89d736a] | 258 | /* This generates an unfinished md5_state_t variable. Every time we generate |
---|
| 259 | an ID, we finish the state by adding a sequence number and take the hash. */ |
---|
[608f8cf] | 260 | static void jabber_generate_id_hash( struct jabber_data *jd ) |
---|
| 261 | { |
---|
[89d736a] | 262 | md5_byte_t binbuf[4]; |
---|
[608f8cf] | 263 | char *s; |
---|
| 264 | |
---|
[89d736a] | 265 | md5_init( &jd->cached_id_prefix ); |
---|
| 266 | md5_append( &jd->cached_id_prefix, (unsigned char *) jd->username, strlen( jd->username ) ); |
---|
| 267 | md5_append( &jd->cached_id_prefix, (unsigned char *) jd->server, strlen( jd->server ) ); |
---|
[608f8cf] | 268 | s = set_getstr( &jd->ic->acc->set, "resource" ); |
---|
[89d736a] | 269 | md5_append( &jd->cached_id_prefix, (unsigned char *) s, strlen( s ) ); |
---|
| 270 | random_bytes( binbuf, 4 ); |
---|
| 271 | md5_append( &jd->cached_id_prefix, binbuf, 4 ); |
---|
[f06894d] | 272 | } |
---|
| 273 | |
---|
[0da65d5] | 274 | static void jabber_logout( struct im_connection *ic ) |
---|
[f06894d] | 275 | { |
---|
[0da65d5] | 276 | struct jabber_data *jd = ic->proto_data; |
---|
[21167d2] | 277 | |
---|
[4ac647d] | 278 | while( jd->filetransfers ) |
---|
[17a6ee9] | 279 | imcb_file_canceled( ic, ( ( struct jabber_transfer *) jd->filetransfers->data )->ft, "Logging out" ); |
---|
[4ac647d] | 280 | |
---|
| 281 | while( jd->streamhosts ) |
---|
| 282 | { |
---|
| 283 | jabber_streamhost_t *sh = jd->streamhosts->data; |
---|
| 284 | jd->streamhosts = g_slist_remove( jd->streamhosts, sh ); |
---|
| 285 | g_free( sh->jid ); |
---|
| 286 | g_free( sh->host ); |
---|
| 287 | g_free( sh ); |
---|
| 288 | } |
---|
| 289 | |
---|
[de03374] | 290 | if( jd->fd >= 0 ) |
---|
| 291 | jabber_end_stream( ic ); |
---|
[4a0614e] | 292 | |
---|
[c377417] | 293 | while( ic->groupchats ) |
---|
[aea8b68] | 294 | jabber_chat_free( ic->groupchats->data ); |
---|
[c377417] | 295 | |
---|
[21167d2] | 296 | if( jd->r_inpa >= 0 ) |
---|
| 297 | b_event_remove( jd->r_inpa ); |
---|
| 298 | if( jd->w_inpa >= 0 ) |
---|
| 299 | b_event_remove( jd->w_inpa ); |
---|
| 300 | |
---|
| 301 | if( jd->ssl ) |
---|
| 302 | ssl_disconnect( jd->ssl ); |
---|
| 303 | if( jd->fd >= 0 ) |
---|
| 304 | closesocket( jd->fd ); |
---|
| 305 | |
---|
[fe7a554] | 306 | if( jd->tx_len ) |
---|
| 307 | g_free( jd->txq ); |
---|
| 308 | |
---|
[de03374] | 309 | if( jd->node_cache ) |
---|
| 310 | g_hash_table_destroy( jd->node_cache ); |
---|
[038d17f] | 311 | |
---|
[04a927c] | 312 | jabber_buddy_remove_all( ic ); |
---|
| 313 | |
---|
[70f6aab8] | 314 | xt_free( jd->xt ); |
---|
| 315 | |
---|
[f138bd2] | 316 | g_free( jd->oauth2_access_token ); |
---|
[5e202b0] | 317 | g_free( jd->away_message ); |
---|
[21167d2] | 318 | g_free( jd->username ); |
---|
[68286eb] | 319 | g_free( jd->me ); |
---|
[21167d2] | 320 | g_free( jd ); |
---|
[b5c8a34] | 321 | |
---|
| 322 | jabber_connections = g_slist_remove( jabber_connections, ic ); |
---|
[f06894d] | 323 | } |
---|
| 324 | |
---|
[f6c963b] | 325 | static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
[f06894d] | 326 | { |
---|
[0da65d5] | 327 | struct jabber_data *jd = ic->proto_data; |
---|
[a21a8ac] | 328 | struct jabber_buddy *bud; |
---|
[cc2cb2d] | 329 | struct xt_node *node; |
---|
[b9f8b87] | 330 | char *s; |
---|
[4a0614e] | 331 | int st; |
---|
| 332 | |
---|
[bb95d43] | 333 | if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 ) |
---|
| 334 | return jabber_write( ic, message, strlen( message ) ); |
---|
| 335 | |
---|
[4a5d885] | 336 | if( g_strcasecmp( who, "jabber_oauth" ) == 0 ) |
---|
| 337 | { |
---|
| 338 | if( sasl_oauth2_get_refresh_token( ic, message ) ) |
---|
| 339 | { |
---|
| 340 | return 1; |
---|
| 341 | } |
---|
| 342 | else |
---|
| 343 | { |
---|
| 344 | imcb_error( ic, "OAuth failure" ); |
---|
| 345 | imc_logout( ic, TRUE ); |
---|
[911d97a] | 346 | return 0; |
---|
[4a5d885] | 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
[5bd21df] | 350 | if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) ) |
---|
[b9f8b87] | 351 | bud = jabber_buddy_by_ext_jid( ic, who, 0 ); |
---|
| 352 | else |
---|
[76c85b4c] | 353 | bud = jabber_buddy_by_jid( ic, who, GET_BUDDY_BARE_OK ); |
---|
[a214954] | 354 | |
---|
[4a0614e] | 355 | node = xt_new_node( "body", message, NULL ); |
---|
[788a1af] | 356 | node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node ); |
---|
[a21a8ac] | 357 | |
---|
[0d3f30f] | 358 | if( bud && ( jd->flags & JFLAG_WANT_TYPING ) && |
---|
[788a1af] | 359 | ( ( bud->flags & JBFLAG_DOES_XEP85 ) || |
---|
| 360 | !( bud->flags & JBFLAG_PROBED_XEP85 ) ) ) |
---|
[a21a8ac] | 361 | { |
---|
| 362 | struct xt_node *act; |
---|
| 363 | |
---|
| 364 | /* If the user likes typing notification and if we don't know |
---|
[788a1af] | 365 | (and didn't probe before) if this resource supports XEP85, |
---|
[abbd8ed] | 366 | include a probe in this packet now. Also, if we know this |
---|
| 367 | buddy does support XEP85, we have to send this <active/> |
---|
| 368 | tag to tell that the user stopped typing (well, that's what |
---|
| 369 | we guess when s/he pressed Enter...). */ |
---|
[a21a8ac] | 370 | act = xt_new_node( "active", NULL, NULL ); |
---|
[47d3ac4] | 371 | xt_add_attr( act, "xmlns", XMLNS_CHATSTATES ); |
---|
[a21a8ac] | 372 | xt_add_child( node, act ); |
---|
| 373 | |
---|
| 374 | /* Just make sure we do this only once. */ |
---|
[788a1af] | 375 | bud->flags |= JBFLAG_PROBED_XEP85; |
---|
[a21a8ac] | 376 | } |
---|
| 377 | |
---|
[0da65d5] | 378 | st = jabber_write_packet( ic, node ); |
---|
[4a0614e] | 379 | xt_free_node( node ); |
---|
| 380 | |
---|
| 381 | return st; |
---|
[f06894d] | 382 | } |
---|
| 383 | |
---|
[0da65d5] | 384 | static GList *jabber_away_states( struct im_connection *ic ) |
---|
[dd788bb] | 385 | { |
---|
[5e202b0] | 386 | static GList *l = NULL; |
---|
| 387 | int i; |
---|
[dd788bb] | 388 | |
---|
[5e202b0] | 389 | if( l == NULL ) |
---|
| 390 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 391 | l = g_list_append( l, (void*) jabber_away_state_list[i].full_name ); |
---|
[dd788bb] | 392 | |
---|
[5e202b0] | 393 | return l; |
---|
[dd788bb] | 394 | } |
---|
| 395 | |
---|
[0da65d5] | 396 | static void jabber_get_info( struct im_connection *ic, char *who ) |
---|
[038d17f] | 397 | { |
---|
[6a1128d] | 398 | struct jabber_buddy *bud; |
---|
[038d17f] | 399 | |
---|
[76c85b4c] | 400 | bud = jabber_buddy_by_jid( ic, who, GET_BUDDY_FIRST ); |
---|
[7e83adca] | 401 | |
---|
[6a1128d] | 402 | while( bud ) |
---|
| 403 | { |
---|
[63770b4] | 404 | imcb_log( ic, "Buddy %s (%d) information:", bud->full_jid, bud->priority ); |
---|
| 405 | if( bud->away_state ) |
---|
| 406 | imcb_log( ic, "Away state: %s", bud->away_state->full_name ); |
---|
[daae10f] | 407 | imcb_log( ic, "Status message: %s", bud->away_message ? bud->away_message : "(none)" ); |
---|
[63770b4] | 408 | |
---|
[6a1128d] | 409 | bud = bud->next; |
---|
| 410 | } |
---|
[1991be6] | 411 | |
---|
[0da65d5] | 412 | jabber_get_vcard( ic, bud ? bud->full_jid : who ); |
---|
[038d17f] | 413 | } |
---|
| 414 | |
---|
[0da65d5] | 415 | static void jabber_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
[dd788bb] | 416 | { |
---|
[0da65d5] | 417 | struct jabber_data *jd = ic->proto_data; |
---|
[5e202b0] | 418 | |
---|
[840bba8] | 419 | /* state_txt == NULL -> Not away. |
---|
| 420 | Unknown state -> fall back to the first defined away state. */ |
---|
[daae10f] | 421 | if( state_txt == NULL ) |
---|
| 422 | jd->away_state = NULL; |
---|
| 423 | else if( ( jd->away_state = jabber_away_state_by_name( state_txt ) ) == NULL ) |
---|
| 424 | jd->away_state = jabber_away_state_list; |
---|
[840bba8] | 425 | |
---|
[5e202b0] | 426 | g_free( jd->away_message ); |
---|
| 427 | jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL; |
---|
| 428 | |
---|
[0da65d5] | 429 | presence_send_update( ic ); |
---|
[deff040] | 430 | } |
---|
| 431 | |
---|
[0da65d5] | 432 | static void jabber_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[cfbb3a6] | 433 | { |
---|
[bb95d43] | 434 | struct jabber_data *jd = ic->proto_data; |
---|
| 435 | |
---|
| 436 | if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 ) |
---|
| 437 | { |
---|
| 438 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
| 439 | imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
| 440 | return; |
---|
| 441 | } |
---|
| 442 | |
---|
[46d215d] | 443 | if( jabber_add_to_roster( ic, who, NULL, group ) ) |
---|
[0da65d5] | 444 | presence_send_request( ic, who, "subscribe" ); |
---|
[cfbb3a6] | 445 | } |
---|
| 446 | |
---|
[0da65d5] | 447 | static void jabber_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[cfbb3a6] | 448 | { |
---|
[bb95d43] | 449 | struct jabber_data *jd = ic->proto_data; |
---|
| 450 | |
---|
| 451 | if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 ) |
---|
| 452 | { |
---|
| 453 | jd->flags &= ~JFLAG_XMLCONSOLE; |
---|
[a3d5766] | 454 | /* Not necessary for now. And for now the code isn't too |
---|
| 455 | happy if the buddy is completely gone right after calling |
---|
| 456 | this function already. |
---|
[998b103] | 457 | imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
[a3d5766] | 458 | */ |
---|
[bb95d43] | 459 | return; |
---|
| 460 | } |
---|
| 461 | |
---|
[788a1af] | 462 | /* We should always do this part. Clean up our administration a little bit. */ |
---|
[0da65d5] | 463 | jabber_buddy_remove_bare( ic, who ); |
---|
[788a1af] | 464 | |
---|
[0da65d5] | 465 | if( jabber_remove_from_roster( ic, who ) ) |
---|
| 466 | presence_send_request( ic, who, "unsubscribe" ); |
---|
[cfbb3a6] | 467 | } |
---|
| 468 | |
---|
[03f3828] | 469 | static struct groupchat *jabber_chat_join_( struct im_connection *ic, const char *room, const char *nick, const char *password, set_t **sets ) |
---|
[e35d1a1] | 470 | { |
---|
| 471 | if( strchr( room, '@' ) == NULL ) |
---|
| 472 | imcb_error( ic, "Invalid room name: %s", room ); |
---|
[5bd21df] | 473 | else if( jabber_chat_by_jid( ic, room ) ) |
---|
[e35d1a1] | 474 | imcb_error( ic, "Already present in chat `%s'", room ); |
---|
| 475 | else |
---|
[6d544a1] | 476 | return jabber_chat_join( ic, room, nick, set_getstr( sets, "password" ) ); |
---|
[e35d1a1] | 477 | |
---|
| 478 | return NULL; |
---|
| 479 | } |
---|
| 480 | |
---|
[43671b9] | 481 | static void jabber_chat_msg_( struct groupchat *c, char *message, int flags ) |
---|
| 482 | { |
---|
| 483 | if( c && message ) |
---|
| 484 | jabber_chat_msg( c, message, flags ); |
---|
| 485 | } |
---|
| 486 | |
---|
[ef5c185] | 487 | static void jabber_chat_topic_( struct groupchat *c, char *topic ) |
---|
| 488 | { |
---|
| 489 | if( c && topic ) |
---|
| 490 | jabber_chat_topic( c, topic ); |
---|
| 491 | } |
---|
| 492 | |
---|
[e35d1a1] | 493 | static void jabber_chat_leave_( struct groupchat *c ) |
---|
| 494 | { |
---|
| 495 | if( c ) |
---|
| 496 | jabber_chat_leave( c, NULL ); |
---|
| 497 | } |
---|
| 498 | |
---|
[c058ff9] | 499 | static void jabber_chat_invite_( struct groupchat *c, char *who, char *msg ) |
---|
| 500 | { |
---|
[68286eb] | 501 | struct jabber_data *jd = c->ic->proto_data; |
---|
[c058ff9] | 502 | struct jabber_chat *jc = c->data; |
---|
| 503 | gchar *msg_alt = NULL; |
---|
| 504 | |
---|
| 505 | if( msg == NULL ) |
---|
[68286eb] | 506 | msg_alt = g_strdup_printf( "%s invited you to %s", jd->me, jc->name ); |
---|
[c058ff9] | 507 | |
---|
| 508 | if( c && who ) |
---|
| 509 | jabber_chat_invite( c, who, msg ? msg : msg_alt ); |
---|
| 510 | |
---|
| 511 | g_free( msg_alt ); |
---|
| 512 | } |
---|
| 513 | |
---|
[0da65d5] | 514 | static void jabber_keepalive( struct im_connection *ic ) |
---|
[deff040] | 515 | { |
---|
| 516 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
[38ff846] | 517 | if( !jabber_write( ic, "\n", 1 ) ) |
---|
| 518 | return; |
---|
[a214954] | 519 | |
---|
[038d17f] | 520 | /* This runs the garbage collection every minute, which means every packet |
---|
| 521 | is in the cache for about a minute (which should be enough AFAIK). */ |
---|
[0da65d5] | 522 | jabber_cache_clean( ic ); |
---|
[dd788bb] | 523 | } |
---|
| 524 | |
---|
[0da65d5] | 525 | static int jabber_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
[a21a8ac] | 526 | { |
---|
[0da65d5] | 527 | struct jabber_data *jd = ic->proto_data; |
---|
[a21a8ac] | 528 | struct jabber_buddy *bud; |
---|
| 529 | |
---|
| 530 | /* Enable typing notification related code from now. */ |
---|
| 531 | jd->flags |= JFLAG_WANT_TYPING; |
---|
| 532 | |
---|
[0da65d5] | 533 | if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL ) |
---|
[788a1af] | 534 | { |
---|
| 535 | /* Sending typing notifications to unknown buddies is |
---|
| 536 | unsupported for now. Shouldn't be a problem, I think. */ |
---|
| 537 | return 0; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | if( bud->flags & JBFLAG_DOES_XEP85 ) |
---|
[a21a8ac] | 541 | { |
---|
| 542 | /* We're only allowed to send this stuff if we know the other |
---|
| 543 | side supports it. */ |
---|
| 544 | |
---|
| 545 | struct xt_node *node; |
---|
| 546 | char *type; |
---|
| 547 | int st; |
---|
| 548 | |
---|
[df1fb67] | 549 | if( typing & OPT_TYPING ) |
---|
[a21a8ac] | 550 | type = "composing"; |
---|
[df1fb67] | 551 | else if( typing & OPT_THINKING ) |
---|
| 552 | type = "paused"; |
---|
| 553 | else |
---|
| 554 | type = "active"; |
---|
[a21a8ac] | 555 | |
---|
| 556 | node = xt_new_node( type, NULL, NULL ); |
---|
[47d3ac4] | 557 | xt_add_attr( node, "xmlns", XMLNS_CHATSTATES ); |
---|
[a21a8ac] | 558 | node = jabber_make_packet( "message", "chat", bud->full_jid, node ); |
---|
| 559 | |
---|
[0da65d5] | 560 | st = jabber_write_packet( ic, node ); |
---|
[a21a8ac] | 561 | xt_free_node( node ); |
---|
| 562 | |
---|
| 563 | return st; |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | return 1; |
---|
| 567 | } |
---|
| 568 | |
---|
[6d544a1] | 569 | void jabber_chat_add_settings( account_t *acc, set_t **head ) |
---|
| 570 | { |
---|
| 571 | /* Meh. Stupid room passwords. Not trying to obfuscate/hide |
---|
| 572 | them from the user for now. */ |
---|
| 573 | set_add( head, "password", NULL, NULL, NULL ); |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | void jabber_chat_free_settings( account_t *acc, set_t **head ) |
---|
| 577 | { |
---|
| 578 | set_del( head, "password" ); |
---|
| 579 | } |
---|
| 580 | |
---|
[d88c92a] | 581 | GList *jabber_buddy_action_list( bee_user_t *bu ) |
---|
| 582 | { |
---|
| 583 | static GList *ret = NULL; |
---|
| 584 | |
---|
| 585 | if( ret == NULL ) |
---|
| 586 | { |
---|
[a97a336] | 587 | static const struct buddy_action ba[2] = { |
---|
[d88c92a] | 588 | { "VERSION", "Get client (version) information" }, |
---|
| 589 | }; |
---|
| 590 | |
---|
[a97a336] | 591 | ret = g_list_prepend( ret, (void*) ba + 0 ); |
---|
[d88c92a] | 592 | } |
---|
| 593 | |
---|
| 594 | return ret; |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | void *jabber_buddy_action( struct bee_user *bu, const char *action, char * const args[], void *data ) |
---|
| 598 | { |
---|
| 599 | if( g_strcasecmp( action, "VERSION" ) == 0 ) |
---|
| 600 | { |
---|
| 601 | struct jabber_buddy *bud; |
---|
| 602 | |
---|
| 603 | if( ( bud = jabber_buddy_by_ext_jid( bu->ic, bu->handle, 0 ) ) == NULL ) |
---|
| 604 | bud = jabber_buddy_by_jid( bu->ic, bu->handle, GET_BUDDY_FIRST ); |
---|
| 605 | for( ; bud; bud = bud->next ) |
---|
| 606 | jabber_iq_version_send( bu->ic, bud, data ); |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | return NULL; |
---|
| 610 | } |
---|
| 611 | |
---|
[0da65d5] | 612 | void jabber_initmodule() |
---|
[f06894d] | 613 | { |
---|
[deff040] | 614 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
[f06894d] | 615 | |
---|
| 616 | ret->name = "jabber"; |
---|
[6d544a1] | 617 | ret->mms = 0; /* no limit */ |
---|
[f06894d] | 618 | ret->login = jabber_login; |
---|
[0da65d5] | 619 | ret->init = jabber_init; |
---|
| 620 | ret->logout = jabber_logout; |
---|
[f6c963b] | 621 | ret->buddy_msg = jabber_buddy_msg; |
---|
[dd788bb] | 622 | ret->away_states = jabber_away_states; |
---|
| 623 | ret->set_away = jabber_set_away; |
---|
[f06894d] | 624 | // ret->set_info = jabber_set_info; |
---|
[038d17f] | 625 | ret->get_info = jabber_get_info; |
---|
[cfbb3a6] | 626 | ret->add_buddy = jabber_add_buddy; |
---|
| 627 | ret->remove_buddy = jabber_remove_buddy; |
---|
[43671b9] | 628 | ret->chat_msg = jabber_chat_msg_; |
---|
[ef5c185] | 629 | ret->chat_topic = jabber_chat_topic_; |
---|
[c058ff9] | 630 | ret->chat_invite = jabber_chat_invite_; |
---|
[e35d1a1] | 631 | ret->chat_leave = jabber_chat_leave_; |
---|
| 632 | ret->chat_join = jabber_chat_join_; |
---|
[6d544a1] | 633 | ret->chat_add_settings = jabber_chat_add_settings; |
---|
| 634 | ret->chat_free_settings = jabber_chat_free_settings; |
---|
[deff040] | 635 | ret->keepalive = jabber_keepalive; |
---|
[a21a8ac] | 636 | ret->send_typing = jabber_send_typing; |
---|
[f06894d] | 637 | ret->handle_cmp = g_strcasecmp; |
---|
[2ff2076] | 638 | ret->transfer_request = jabber_si_transfer_request; |
---|
[d88c92a] | 639 | ret->buddy_action_list = jabber_buddy_action_list; |
---|
| 640 | ret->buddy_action = jabber_buddy_action; |
---|
[f06894d] | 641 | |
---|
[deff040] | 642 | register_protocol( ret ); |
---|
[f06894d] | 643 | } |
---|