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