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