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