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