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