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