[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" |
---|
| 35 | #include "base64.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 | |
---|
[7f69740] | 61 | g_snprintf( str, sizeof( str ), "%d", jabber_port_list[0] ); |
---|
| 62 | s = set_add( &acc->set, "port", str, set_eval_int, acc ); |
---|
[f06894d] | 63 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 64 | |
---|
[ebe7b36] | 65 | s = set_add( &acc->set, "priority", "0", set_eval_priority, acc ); |
---|
[f06894d] | 66 | |
---|
[ebe7b36] | 67 | s = set_add( &acc->set, "resource", "BitlBee", NULL, acc ); |
---|
| 68 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[f06894d] | 69 | |
---|
[a21a8ac] | 70 | s = set_add( &acc->set, "resource_select", "priority", NULL, acc ); |
---|
| 71 | |
---|
[f06894d] | 72 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 73 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 74 | |
---|
| 75 | s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc ); |
---|
| 76 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 77 | |
---|
| 78 | s = set_add( &acc->set, "tls", "try", set_eval_tls, acc ); |
---|
| 79 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[a6df0b5] | 80 | |
---|
| 81 | s = set_add( &acc->set, "xmlconsole", "false", set_eval_bool, acc ); |
---|
| 82 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[f06894d] | 83 | } |
---|
| 84 | |
---|
[608f8cf] | 85 | static void jabber_generate_id_hash( struct jabber_data *jd ); |
---|
| 86 | |
---|
[f06894d] | 87 | static void jabber_login( account_t *acc ) |
---|
| 88 | { |
---|
[84b045d] | 89 | struct im_connection *ic = imcb_new( acc ); |
---|
[21167d2] | 90 | struct jabber_data *jd = g_new0( struct jabber_data, 1 ); |
---|
[36e9f62] | 91 | struct ns_srv_reply *srv = NULL; |
---|
[3b3cd693] | 92 | char *connect_to, *s; |
---|
[7f69740] | 93 | int i; |
---|
[21167d2] | 94 | |
---|
[b5c8a34] | 95 | /* For now this is needed in the _connected() handlers if using |
---|
| 96 | GLib event handling, to make sure we're not handling events |
---|
| 97 | on dead connections. */ |
---|
| 98 | jabber_connections = g_slist_prepend( jabber_connections, ic ); |
---|
| 99 | |
---|
[0da65d5] | 100 | jd->ic = ic; |
---|
| 101 | ic->proto_data = jd; |
---|
[21167d2] | 102 | |
---|
| 103 | jd->username = g_strdup( acc->user ); |
---|
| 104 | jd->server = strchr( jd->username, '@' ); |
---|
| 105 | |
---|
[de03374] | 106 | jd->fd = jd->r_inpa = jd->w_inpa = -1; |
---|
| 107 | |
---|
[21167d2] | 108 | if( jd->server == NULL ) |
---|
| 109 | { |
---|
[84b045d] | 110 | imcb_error( ic, "Incomplete account name (format it like <username@jabberserver.name>)" ); |
---|
[c2fb3809] | 111 | imc_logout( ic, FALSE ); |
---|
[21167d2] | 112 | return; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /* So don't think of free()ing jd->server.. :-) */ |
---|
| 116 | *jd->server = 0; |
---|
| 117 | jd->server ++; |
---|
| 118 | |
---|
[3b3cd693] | 119 | if( ( s = strchr( jd->server, '/' ) ) ) |
---|
| 120 | { |
---|
| 121 | *s = 0; |
---|
| 122 | set_setstr( &acc->set, "resource", s + 1 ); |
---|
| 123 | |
---|
| 124 | /* Also remove the /resource from the original variable so we |
---|
| 125 | won't have to do this again every time. */ |
---|
| 126 | s = strchr( acc->user, '/' ); |
---|
| 127 | *s = 0; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* This code isn't really pretty. Backwards compatibility never is... */ |
---|
| 131 | s = acc->server; |
---|
| 132 | while( s ) |
---|
| 133 | { |
---|
| 134 | static int had_port = 0; |
---|
| 135 | |
---|
| 136 | if( strncmp( s, "ssl", 3 ) == 0 ) |
---|
| 137 | { |
---|
| 138 | set_setstr( &acc->set, "ssl", "true" ); |
---|
| 139 | |
---|
| 140 | /* Flush this part so that (if this was the first |
---|
| 141 | part of the server string) acc->server gets |
---|
| 142 | flushed. We don't want to have to do this another |
---|
| 143 | time. :-) */ |
---|
| 144 | *s = 0; |
---|
| 145 | s ++; |
---|
| 146 | |
---|
| 147 | /* Only set this if the user didn't specify a custom |
---|
| 148 | port number already... */ |
---|
| 149 | if( !had_port ) |
---|
| 150 | set_setint( &acc->set, "port", 5223 ); |
---|
| 151 | } |
---|
| 152 | else if( isdigit( *s ) ) |
---|
| 153 | { |
---|
| 154 | int i; |
---|
| 155 | |
---|
| 156 | /* The first character is a digit. It could be an |
---|
| 157 | IP address though. Only accept this as a port# |
---|
| 158 | if there are only digits. */ |
---|
| 159 | for( i = 0; isdigit( s[i] ); i ++ ); |
---|
| 160 | |
---|
| 161 | /* If the first non-digit character is a colon or |
---|
| 162 | the end of the string, save the port number |
---|
| 163 | where it should be. */ |
---|
| 164 | if( s[i] == ':' || s[i] == 0 ) |
---|
| 165 | { |
---|
| 166 | sscanf( s, "%d", &i ); |
---|
| 167 | set_setint( &acc->set, "port", i ); |
---|
| 168 | |
---|
| 169 | /* See above. */ |
---|
| 170 | *s = 0; |
---|
| 171 | s ++; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | had_port = 1; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | s = strchr( s, ':' ); |
---|
| 178 | if( s ) |
---|
| 179 | { |
---|
| 180 | *s = 0; |
---|
| 181 | s ++; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[038d17f] | 185 | jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free ); |
---|
[6a1128d] | 186 | jd->buddies = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
[fe7a554] | 187 | |
---|
[36e9f62] | 188 | /* Figure out the hostname to connect to. */ |
---|
[3b3cd693] | 189 | if( acc->server && *acc->server ) |
---|
[36e9f62] | 190 | connect_to = acc->server; |
---|
| 191 | else if( ( srv = srv_lookup( "xmpp-client", "tcp", jd->server ) ) || |
---|
| 192 | ( srv = srv_lookup( "jabber-client", "tcp", jd->server ) ) ) |
---|
| 193 | connect_to = srv->name; |
---|
| 194 | else |
---|
| 195 | connect_to = jd->server; |
---|
| 196 | |
---|
[84b045d] | 197 | imcb_log( ic, "Connecting" ); |
---|
[35f6677] | 198 | |
---|
[7f69740] | 199 | for( i = 0; jabber_port_list[i] > 0; i ++ ) |
---|
| 200 | if( set_getint( &acc->set, "port" ) == jabber_port_list[i] ) |
---|
| 201 | break; |
---|
| 202 | |
---|
| 203 | if( jabber_port_list[i] == 0 ) |
---|
[0f4c1bb5] | 204 | { |
---|
[7f69740] | 205 | imcb_log( ic, "Illegal port number" ); |
---|
[c2fb3809] | 206 | imc_logout( ic, FALSE ); |
---|
[0f4c1bb5] | 207 | return; |
---|
| 208 | } |
---|
| 209 | |
---|
[36e9f62] | 210 | /* For non-SSL connections we can try to use the port # from the SRV |
---|
| 211 | reply, but let's not do that when using SSL, SSL usually runs on |
---|
| 212 | non-standard ports... */ |
---|
[21167d2] | 213 | if( set_getbool( &acc->set, "ssl" ) ) |
---|
| 214 | { |
---|
[0da65d5] | 215 | jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, ic ); |
---|
[3b3cd693] | 216 | jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1; |
---|
[21167d2] | 217 | } |
---|
| 218 | else |
---|
| 219 | { |
---|
[0da65d5] | 220 | jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic ); |
---|
[21167d2] | 221 | } |
---|
[36e9f62] | 222 | g_free( srv ); |
---|
[35f6677] | 223 | |
---|
| 224 | if( jd->fd == -1 ) |
---|
| 225 | { |
---|
[84b045d] | 226 | imcb_error( ic, "Could not connect to server" ); |
---|
[c2fb3809] | 227 | imc_logout( ic, TRUE ); |
---|
[fb4ebcc5] | 228 | |
---|
| 229 | return; |
---|
[35f6677] | 230 | } |
---|
[a6df0b5] | 231 | |
---|
| 232 | if( set_getbool( &acc->set, "xmlconsole" ) ) |
---|
| 233 | { |
---|
| 234 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
| 235 | /* Shouldn't really do this at this stage already, maybe. But |
---|
| 236 | I think this shouldn't break anything. */ |
---|
| 237 | imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
| 238 | } |
---|
[608f8cf] | 239 | |
---|
| 240 | jabber_generate_id_hash( jd ); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | static void jabber_generate_id_hash( struct jabber_data *jd ) |
---|
| 244 | { |
---|
| 245 | md5_state_t id_hash; |
---|
| 246 | md5_byte_t binbuf[16]; |
---|
| 247 | char *s; |
---|
| 248 | |
---|
| 249 | md5_init( &id_hash ); |
---|
| 250 | md5_append( &id_hash, (unsigned char *) jd->username, strlen( jd->username ) ); |
---|
| 251 | md5_append( &id_hash, (unsigned char *) jd->server, strlen( jd->server ) ); |
---|
| 252 | s = set_getstr( &jd->ic->acc->set, "resource" ); |
---|
| 253 | md5_append( &id_hash, (unsigned char *) s, strlen( s ) ); |
---|
| 254 | random_bytes( binbuf, 16 ); |
---|
| 255 | md5_append( &id_hash, binbuf, 16 ); |
---|
| 256 | md5_finish( &id_hash, binbuf ); |
---|
| 257 | |
---|
| 258 | s = base64_encode( binbuf, 9 ); |
---|
| 259 | jd->cached_id_prefix = g_strdup_printf( "%s%s", JABBER_CACHED_ID, s ); |
---|
| 260 | g_free( s ); |
---|
[f06894d] | 261 | } |
---|
| 262 | |
---|
[0da65d5] | 263 | static void jabber_logout( struct im_connection *ic ) |
---|
[f06894d] | 264 | { |
---|
[0da65d5] | 265 | struct jabber_data *jd = ic->proto_data; |
---|
[21167d2] | 266 | |
---|
[de03374] | 267 | if( jd->fd >= 0 ) |
---|
| 268 | jabber_end_stream( ic ); |
---|
[4a0614e] | 269 | |
---|
[c377417] | 270 | while( ic->groupchats ) |
---|
[9da0bbf] | 271 | jabber_chat_free( ic->groupchats ); |
---|
[c377417] | 272 | |
---|
[21167d2] | 273 | if( jd->r_inpa >= 0 ) |
---|
| 274 | b_event_remove( jd->r_inpa ); |
---|
| 275 | if( jd->w_inpa >= 0 ) |
---|
| 276 | b_event_remove( jd->w_inpa ); |
---|
| 277 | |
---|
| 278 | if( jd->ssl ) |
---|
| 279 | ssl_disconnect( jd->ssl ); |
---|
| 280 | if( jd->fd >= 0 ) |
---|
| 281 | closesocket( jd->fd ); |
---|
| 282 | |
---|
[fe7a554] | 283 | if( jd->tx_len ) |
---|
| 284 | g_free( jd->txq ); |
---|
| 285 | |
---|
[de03374] | 286 | if( jd->node_cache ) |
---|
| 287 | g_hash_table_destroy( jd->node_cache ); |
---|
[038d17f] | 288 | |
---|
[70f6aab8] | 289 | xt_free( jd->xt ); |
---|
| 290 | |
---|
[e960a52] | 291 | g_free( jd->cached_id_prefix ); |
---|
[5e202b0] | 292 | g_free( jd->away_message ); |
---|
[21167d2] | 293 | g_free( jd->username ); |
---|
| 294 | g_free( jd ); |
---|
[b5c8a34] | 295 | |
---|
| 296 | jabber_connections = g_slist_remove( jabber_connections, ic ); |
---|
[f06894d] | 297 | } |
---|
| 298 | |
---|
[f6c963b] | 299 | static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) |
---|
[f06894d] | 300 | { |
---|
[0da65d5] | 301 | struct jabber_data *jd = ic->proto_data; |
---|
[a21a8ac] | 302 | struct jabber_buddy *bud; |
---|
[cc2cb2d] | 303 | struct xt_node *node; |
---|
[b9f8b87] | 304 | char *s; |
---|
[4a0614e] | 305 | int st; |
---|
| 306 | |
---|
[bb95d43] | 307 | if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 ) |
---|
| 308 | return jabber_write( ic, message, strlen( message ) ); |
---|
| 309 | |
---|
[5bd21df] | 310 | if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) ) |
---|
[b9f8b87] | 311 | bud = jabber_buddy_by_ext_jid( ic, who, 0 ); |
---|
| 312 | else |
---|
| 313 | bud = jabber_buddy_by_jid( ic, who, 0 ); |
---|
[a214954] | 314 | |
---|
[4a0614e] | 315 | node = xt_new_node( "body", message, NULL ); |
---|
[788a1af] | 316 | node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node ); |
---|
[a21a8ac] | 317 | |
---|
[0d3f30f] | 318 | if( bud && ( jd->flags & JFLAG_WANT_TYPING ) && |
---|
[788a1af] | 319 | ( ( bud->flags & JBFLAG_DOES_XEP85 ) || |
---|
| 320 | !( bud->flags & JBFLAG_PROBED_XEP85 ) ) ) |
---|
[a21a8ac] | 321 | { |
---|
| 322 | struct xt_node *act; |
---|
| 323 | |
---|
| 324 | /* If the user likes typing notification and if we don't know |
---|
[788a1af] | 325 | (and didn't probe before) if this resource supports XEP85, |
---|
[abbd8ed] | 326 | include a probe in this packet now. Also, if we know this |
---|
| 327 | buddy does support XEP85, we have to send this <active/> |
---|
| 328 | tag to tell that the user stopped typing (well, that's what |
---|
| 329 | we guess when s/he pressed Enter...). */ |
---|
[a21a8ac] | 330 | act = xt_new_node( "active", NULL, NULL ); |
---|
[47d3ac4] | 331 | xt_add_attr( act, "xmlns", XMLNS_CHATSTATES ); |
---|
[a21a8ac] | 332 | xt_add_child( node, act ); |
---|
| 333 | |
---|
| 334 | /* Just make sure we do this only once. */ |
---|
[788a1af] | 335 | bud->flags |= JBFLAG_PROBED_XEP85; |
---|
[a21a8ac] | 336 | } |
---|
| 337 | |
---|
[0da65d5] | 338 | st = jabber_write_packet( ic, node ); |
---|
[4a0614e] | 339 | xt_free_node( node ); |
---|
| 340 | |
---|
| 341 | return st; |
---|
[f06894d] | 342 | } |
---|
| 343 | |
---|
[0da65d5] | 344 | static GList *jabber_away_states( struct im_connection *ic ) |
---|
[dd788bb] | 345 | { |
---|
[5e202b0] | 346 | static GList *l = NULL; |
---|
| 347 | int i; |
---|
[dd788bb] | 348 | |
---|
[5e202b0] | 349 | if( l == NULL ) |
---|
| 350 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 351 | l = g_list_append( l, (void*) jabber_away_state_list[i].full_name ); |
---|
[dd788bb] | 352 | |
---|
[5e202b0] | 353 | return l; |
---|
[dd788bb] | 354 | } |
---|
| 355 | |
---|
[0da65d5] | 356 | static void jabber_get_info( struct im_connection *ic, char *who ) |
---|
[038d17f] | 357 | { |
---|
[0da65d5] | 358 | struct jabber_data *jd = ic->proto_data; |
---|
[6a1128d] | 359 | struct jabber_buddy *bud; |
---|
[038d17f] | 360 | |
---|
[7e83adca] | 361 | if( strchr( who, '/' ) ) |
---|
[0da65d5] | 362 | bud = jabber_buddy_by_jid( ic, who, 0 ); |
---|
[7e83adca] | 363 | else |
---|
[0d3f30f] | 364 | { |
---|
| 365 | char *s = jabber_normalize( who ); |
---|
| 366 | bud = g_hash_table_lookup( jd->buddies, s ); |
---|
| 367 | g_free( s ); |
---|
| 368 | } |
---|
[7e83adca] | 369 | |
---|
[6a1128d] | 370 | while( bud ) |
---|
| 371 | { |
---|
[84b045d] | 372 | imcb_log( ic, "Buddy %s (%d) information:\nAway state: %s\nAway message: %s", |
---|
[0d3f30f] | 373 | bud->full_jid, bud->priority, |
---|
[6a1128d] | 374 | bud->away_state ? bud->away_state->full_name : "(none)", |
---|
| 375 | bud->away_message ? : "(none)" ); |
---|
| 376 | bud = bud->next; |
---|
| 377 | } |
---|
[1991be6] | 378 | |
---|
[0da65d5] | 379 | jabber_get_vcard( ic, bud ? bud->full_jid : who ); |
---|
[038d17f] | 380 | } |
---|
| 381 | |
---|
[0da65d5] | 382 | static void jabber_set_away( struct im_connection *ic, char *state_txt, char *message ) |
---|
[dd788bb] | 383 | { |
---|
[0da65d5] | 384 | struct jabber_data *jd = ic->proto_data; |
---|
[5e202b0] | 385 | struct jabber_away_state *state; |
---|
| 386 | |
---|
| 387 | /* Save all this info. We need it, for example, when changing the priority setting. */ |
---|
| 388 | state = (void *) jabber_away_state_by_name( state_txt ); |
---|
| 389 | jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */ |
---|
| 390 | g_free( jd->away_message ); |
---|
| 391 | jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL; |
---|
| 392 | |
---|
[0da65d5] | 393 | presence_send_update( ic ); |
---|
[deff040] | 394 | } |
---|
| 395 | |
---|
[0da65d5] | 396 | static void jabber_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[cfbb3a6] | 397 | { |
---|
[bb95d43] | 398 | struct jabber_data *jd = ic->proto_data; |
---|
| 399 | |
---|
| 400 | if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 ) |
---|
| 401 | { |
---|
| 402 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
| 403 | imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
| 404 | return; |
---|
| 405 | } |
---|
| 406 | |
---|
[0da65d5] | 407 | if( jabber_add_to_roster( ic, who, NULL ) ) |
---|
| 408 | presence_send_request( ic, who, "subscribe" ); |
---|
[cfbb3a6] | 409 | } |
---|
| 410 | |
---|
[0da65d5] | 411 | static void jabber_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[cfbb3a6] | 412 | { |
---|
[bb95d43] | 413 | struct jabber_data *jd = ic->proto_data; |
---|
| 414 | |
---|
| 415 | if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 ) |
---|
| 416 | { |
---|
| 417 | jd->flags &= ~JFLAG_XMLCONSOLE; |
---|
[a3d5766] | 418 | /* Not necessary for now. And for now the code isn't too |
---|
| 419 | happy if the buddy is completely gone right after calling |
---|
| 420 | this function already. |
---|
[998b103] | 421 | imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
[a3d5766] | 422 | */ |
---|
[bb95d43] | 423 | return; |
---|
| 424 | } |
---|
| 425 | |
---|
[788a1af] | 426 | /* We should always do this part. Clean up our administration a little bit. */ |
---|
[0da65d5] | 427 | jabber_buddy_remove_bare( ic, who ); |
---|
[788a1af] | 428 | |
---|
[0da65d5] | 429 | if( jabber_remove_from_roster( ic, who ) ) |
---|
| 430 | presence_send_request( ic, who, "unsubscribe" ); |
---|
[cfbb3a6] | 431 | } |
---|
| 432 | |
---|
[e35d1a1] | 433 | static struct groupchat *jabber_chat_join_( struct im_connection *ic, char *room, char *nick, char *password ) |
---|
| 434 | { |
---|
| 435 | if( strchr( room, '@' ) == NULL ) |
---|
| 436 | imcb_error( ic, "Invalid room name: %s", room ); |
---|
[5bd21df] | 437 | else if( jabber_chat_by_jid( ic, room ) ) |
---|
[e35d1a1] | 438 | imcb_error( ic, "Already present in chat `%s'", room ); |
---|
| 439 | else |
---|
| 440 | return jabber_chat_join( ic, room, nick, password ); |
---|
| 441 | |
---|
| 442 | return NULL; |
---|
| 443 | } |
---|
| 444 | |
---|
[43671b9] | 445 | static void jabber_chat_msg_( struct groupchat *c, char *message, int flags ) |
---|
| 446 | { |
---|
| 447 | if( c && message ) |
---|
| 448 | jabber_chat_msg( c, message, flags ); |
---|
| 449 | } |
---|
| 450 | |
---|
[ef5c185] | 451 | static void jabber_chat_topic_( struct groupchat *c, char *topic ) |
---|
| 452 | { |
---|
| 453 | if( c && topic ) |
---|
| 454 | jabber_chat_topic( c, topic ); |
---|
| 455 | } |
---|
| 456 | |
---|
[e35d1a1] | 457 | static void jabber_chat_leave_( struct groupchat *c ) |
---|
| 458 | { |
---|
| 459 | if( c ) |
---|
| 460 | jabber_chat_leave( c, NULL ); |
---|
| 461 | } |
---|
| 462 | |
---|
[c058ff9] | 463 | static void jabber_chat_invite_( struct groupchat *c, char *who, char *msg ) |
---|
| 464 | { |
---|
| 465 | struct jabber_chat *jc = c->data; |
---|
| 466 | gchar *msg_alt = NULL; |
---|
| 467 | |
---|
| 468 | if( msg == NULL ) |
---|
| 469 | msg_alt = g_strdup_printf( "%s invited you to %s", c->ic->acc->user, jc->name ); |
---|
| 470 | |
---|
| 471 | if( c && who ) |
---|
| 472 | jabber_chat_invite( c, who, msg ? msg : msg_alt ); |
---|
| 473 | |
---|
| 474 | g_free( msg_alt ); |
---|
| 475 | } |
---|
| 476 | |
---|
[0da65d5] | 477 | static void jabber_keepalive( struct im_connection *ic ) |
---|
[deff040] | 478 | { |
---|
| 479 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
[0da65d5] | 480 | jabber_write( ic, "\n", 1 ); |
---|
[a214954] | 481 | |
---|
[038d17f] | 482 | /* This runs the garbage collection every minute, which means every packet |
---|
| 483 | is in the cache for about a minute (which should be enough AFAIK). */ |
---|
[0da65d5] | 484 | jabber_cache_clean( ic ); |
---|
[dd788bb] | 485 | } |
---|
| 486 | |
---|
[0da65d5] | 487 | static int jabber_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
[a21a8ac] | 488 | { |
---|
[0da65d5] | 489 | struct jabber_data *jd = ic->proto_data; |
---|
[a21a8ac] | 490 | struct jabber_buddy *bud; |
---|
| 491 | |
---|
| 492 | /* Enable typing notification related code from now. */ |
---|
| 493 | jd->flags |= JFLAG_WANT_TYPING; |
---|
| 494 | |
---|
[0da65d5] | 495 | if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL ) |
---|
[788a1af] | 496 | { |
---|
| 497 | /* Sending typing notifications to unknown buddies is |
---|
| 498 | unsupported for now. Shouldn't be a problem, I think. */ |
---|
| 499 | return 0; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | if( bud->flags & JBFLAG_DOES_XEP85 ) |
---|
[a21a8ac] | 503 | { |
---|
| 504 | /* We're only allowed to send this stuff if we know the other |
---|
| 505 | side supports it. */ |
---|
| 506 | |
---|
| 507 | struct xt_node *node; |
---|
| 508 | char *type; |
---|
| 509 | int st; |
---|
| 510 | |
---|
[df1fb67] | 511 | if( typing & OPT_TYPING ) |
---|
[a21a8ac] | 512 | type = "composing"; |
---|
[df1fb67] | 513 | else if( typing & OPT_THINKING ) |
---|
| 514 | type = "paused"; |
---|
| 515 | else |
---|
| 516 | type = "active"; |
---|
[a21a8ac] | 517 | |
---|
| 518 | node = xt_new_node( type, NULL, NULL ); |
---|
[47d3ac4] | 519 | xt_add_attr( node, "xmlns", XMLNS_CHATSTATES ); |
---|
[a21a8ac] | 520 | node = jabber_make_packet( "message", "chat", bud->full_jid, node ); |
---|
| 521 | |
---|
[0da65d5] | 522 | st = jabber_write_packet( ic, node ); |
---|
[a21a8ac] | 523 | xt_free_node( node ); |
---|
| 524 | |
---|
| 525 | return st; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | return 1; |
---|
| 529 | } |
---|
| 530 | |
---|
[0da65d5] | 531 | void jabber_initmodule() |
---|
[f06894d] | 532 | { |
---|
[deff040] | 533 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
[f06894d] | 534 | |
---|
| 535 | ret->name = "jabber"; |
---|
| 536 | ret->login = jabber_login; |
---|
[0da65d5] | 537 | ret->init = jabber_init; |
---|
| 538 | ret->logout = jabber_logout; |
---|
[f6c963b] | 539 | ret->buddy_msg = jabber_buddy_msg; |
---|
[dd788bb] | 540 | ret->away_states = jabber_away_states; |
---|
| 541 | ret->set_away = jabber_set_away; |
---|
[f06894d] | 542 | // ret->set_info = jabber_set_info; |
---|
[038d17f] | 543 | ret->get_info = jabber_get_info; |
---|
[cfbb3a6] | 544 | ret->add_buddy = jabber_add_buddy; |
---|
| 545 | ret->remove_buddy = jabber_remove_buddy; |
---|
[43671b9] | 546 | ret->chat_msg = jabber_chat_msg_; |
---|
[ef5c185] | 547 | ret->chat_topic = jabber_chat_topic_; |
---|
[c058ff9] | 548 | ret->chat_invite = jabber_chat_invite_; |
---|
[e35d1a1] | 549 | ret->chat_leave = jabber_chat_leave_; |
---|
| 550 | ret->chat_join = jabber_chat_join_; |
---|
[deff040] | 551 | ret->keepalive = jabber_keepalive; |
---|
[a21a8ac] | 552 | ret->send_typing = jabber_send_typing; |
---|
[f06894d] | 553 | ret->handle_cmp = g_strcasecmp; |
---|
| 554 | |
---|
[deff040] | 555 | register_protocol( ret ); |
---|
[f06894d] | 556 | } |
---|