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