[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" |
---|
| 34 | |
---|
| 35 | static void jabber_acc_init( account_t *acc ) |
---|
| 36 | { |
---|
| 37 | set_t *s; |
---|
| 38 | |
---|
| 39 | s = set_add( &acc->set, "port", "5222", set_eval_int, acc ); |
---|
| 40 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 41 | |
---|
[ebe7b36] | 42 | s = set_add( &acc->set, "priority", "0", set_eval_priority, acc ); |
---|
[f06894d] | 43 | |
---|
[ebe7b36] | 44 | s = set_add( &acc->set, "resource", "BitlBee", NULL, acc ); |
---|
| 45 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[f06894d] | 46 | |
---|
[a21a8ac] | 47 | s = set_add( &acc->set, "resource_select", "priority", NULL, acc ); |
---|
| 48 | |
---|
[f06894d] | 49 | s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); |
---|
| 50 | s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; |
---|
| 51 | |
---|
| 52 | s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc ); |
---|
| 53 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 54 | |
---|
| 55 | s = set_add( &acc->set, "tls", "try", set_eval_tls, acc ); |
---|
| 56 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | static void jabber_login( account_t *acc ) |
---|
| 60 | { |
---|
[21167d2] | 61 | struct gaim_connection *gc = new_gaim_conn( acc ); |
---|
| 62 | struct jabber_data *jd = g_new0( struct jabber_data, 1 ); |
---|
[36e9f62] | 63 | struct ns_srv_reply *srv = NULL; |
---|
[3b3cd693] | 64 | char *connect_to, *s; |
---|
[21167d2] | 65 | |
---|
| 66 | jd->gc = gc; |
---|
| 67 | gc->proto_data = jd; |
---|
| 68 | |
---|
| 69 | jd->username = g_strdup( acc->user ); |
---|
| 70 | jd->server = strchr( jd->username, '@' ); |
---|
| 71 | |
---|
| 72 | if( jd->server == NULL ) |
---|
| 73 | { |
---|
| 74 | hide_login_progress( gc, "Incomplete account name (format it like <username@jabberserver.name>)" ); |
---|
| 75 | signoff( gc ); |
---|
| 76 | return; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /* So don't think of free()ing jd->server.. :-) */ |
---|
| 80 | *jd->server = 0; |
---|
| 81 | jd->server ++; |
---|
| 82 | |
---|
[3b3cd693] | 83 | if( ( s = strchr( jd->server, '/' ) ) ) |
---|
| 84 | { |
---|
| 85 | *s = 0; |
---|
| 86 | set_setstr( &acc->set, "resource", s + 1 ); |
---|
| 87 | |
---|
| 88 | /* Also remove the /resource from the original variable so we |
---|
| 89 | won't have to do this again every time. */ |
---|
| 90 | s = strchr( acc->user, '/' ); |
---|
| 91 | *s = 0; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /* This code isn't really pretty. Backwards compatibility never is... */ |
---|
| 95 | s = acc->server; |
---|
| 96 | while( s ) |
---|
| 97 | { |
---|
| 98 | static int had_port = 0; |
---|
| 99 | |
---|
| 100 | if( strncmp( s, "ssl", 3 ) == 0 ) |
---|
| 101 | { |
---|
| 102 | set_setstr( &acc->set, "ssl", "true" ); |
---|
| 103 | |
---|
| 104 | /* Flush this part so that (if this was the first |
---|
| 105 | part of the server string) acc->server gets |
---|
| 106 | flushed. We don't want to have to do this another |
---|
| 107 | time. :-) */ |
---|
| 108 | *s = 0; |
---|
| 109 | s ++; |
---|
| 110 | |
---|
| 111 | /* Only set this if the user didn't specify a custom |
---|
| 112 | port number already... */ |
---|
| 113 | if( !had_port ) |
---|
| 114 | set_setint( &acc->set, "port", 5223 ); |
---|
| 115 | } |
---|
| 116 | else if( isdigit( *s ) ) |
---|
| 117 | { |
---|
| 118 | int i; |
---|
| 119 | |
---|
| 120 | /* The first character is a digit. It could be an |
---|
| 121 | IP address though. Only accept this as a port# |
---|
| 122 | if there are only digits. */ |
---|
| 123 | for( i = 0; isdigit( s[i] ); i ++ ); |
---|
| 124 | |
---|
| 125 | /* If the first non-digit character is a colon or |
---|
| 126 | the end of the string, save the port number |
---|
| 127 | where it should be. */ |
---|
| 128 | if( s[i] == ':' || s[i] == 0 ) |
---|
| 129 | { |
---|
| 130 | sscanf( s, "%d", &i ); |
---|
| 131 | set_setint( &acc->set, "port", i ); |
---|
| 132 | |
---|
| 133 | /* See above. */ |
---|
| 134 | *s = 0; |
---|
| 135 | s ++; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | had_port = 1; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | s = strchr( s, ':' ); |
---|
| 142 | if( s ) |
---|
| 143 | { |
---|
| 144 | *s = 0; |
---|
| 145 | s ++; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
[038d17f] | 149 | jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free ); |
---|
[6a1128d] | 150 | jd->buddies = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
[fe7a554] | 151 | |
---|
[36e9f62] | 152 | /* Figure out the hostname to connect to. */ |
---|
[3b3cd693] | 153 | if( acc->server && *acc->server ) |
---|
[36e9f62] | 154 | connect_to = acc->server; |
---|
| 155 | else if( ( srv = srv_lookup( "xmpp-client", "tcp", jd->server ) ) || |
---|
| 156 | ( srv = srv_lookup( "jabber-client", "tcp", jd->server ) ) ) |
---|
| 157 | connect_to = srv->name; |
---|
| 158 | else |
---|
| 159 | connect_to = jd->server; |
---|
| 160 | |
---|
[4b0d805] | 161 | set_login_progress( gc, 0, "Connecting" ); |
---|
[35f6677] | 162 | |
---|
[36e9f62] | 163 | /* For non-SSL connections we can try to use the port # from the SRV |
---|
| 164 | reply, but let's not do that when using SSL, SSL usually runs on |
---|
| 165 | non-standard ports... */ |
---|
[21167d2] | 166 | if( set_getbool( &acc->set, "ssl" ) ) |
---|
| 167 | { |
---|
[36e9f62] | 168 | jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, gc ); |
---|
[3b3cd693] | 169 | jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1; |
---|
[21167d2] | 170 | } |
---|
| 171 | else |
---|
| 172 | { |
---|
[36e9f62] | 173 | jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, gc ); |
---|
[21167d2] | 174 | } |
---|
[36e9f62] | 175 | g_free( srv ); |
---|
[35f6677] | 176 | |
---|
| 177 | if( jd->fd == -1 ) |
---|
| 178 | { |
---|
| 179 | hide_login_progress( gc, "Could not connect to server" ); |
---|
| 180 | signoff( gc ); |
---|
| 181 | } |
---|
[f06894d] | 182 | } |
---|
| 183 | |
---|
| 184 | static void jabber_close( struct gaim_connection *gc ) |
---|
| 185 | { |
---|
[21167d2] | 186 | struct jabber_data *jd = gc->proto_data; |
---|
| 187 | |
---|
[4a0614e] | 188 | jabber_end_stream( gc ); |
---|
| 189 | |
---|
[21167d2] | 190 | if( jd->r_inpa >= 0 ) |
---|
| 191 | b_event_remove( jd->r_inpa ); |
---|
| 192 | if( jd->w_inpa >= 0 ) |
---|
| 193 | b_event_remove( jd->w_inpa ); |
---|
| 194 | |
---|
| 195 | if( jd->ssl ) |
---|
| 196 | ssl_disconnect( jd->ssl ); |
---|
| 197 | if( jd->fd >= 0 ) |
---|
| 198 | closesocket( jd->fd ); |
---|
| 199 | |
---|
[fe7a554] | 200 | if( jd->tx_len ) |
---|
| 201 | g_free( jd->txq ); |
---|
| 202 | |
---|
[038d17f] | 203 | g_hash_table_destroy( jd->node_cache ); |
---|
| 204 | |
---|
[70f6aab8] | 205 | xt_free( jd->xt ); |
---|
| 206 | |
---|
[5e202b0] | 207 | g_free( jd->away_message ); |
---|
[21167d2] | 208 | g_free( jd->username ); |
---|
| 209 | g_free( jd ); |
---|
[f06894d] | 210 | } |
---|
| 211 | |
---|
| 212 | static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away ) |
---|
| 213 | { |
---|
[a21a8ac] | 214 | struct jabber_data *jd = gc->proto_data; |
---|
| 215 | struct jabber_buddy *bud; |
---|
[cc2cb2d] | 216 | struct xt_node *node; |
---|
[4a0614e] | 217 | int st; |
---|
| 218 | |
---|
[0d3f30f] | 219 | bud = jabber_buddy_by_jid( gc, who, 0 ); |
---|
[a214954] | 220 | |
---|
[4a0614e] | 221 | node = xt_new_node( "body", message, NULL ); |
---|
[788a1af] | 222 | node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node ); |
---|
[a21a8ac] | 223 | |
---|
[0d3f30f] | 224 | if( bud && ( jd->flags & JFLAG_WANT_TYPING ) && |
---|
[788a1af] | 225 | ( ( bud->flags & JBFLAG_DOES_XEP85 ) || |
---|
| 226 | !( bud->flags & JBFLAG_PROBED_XEP85 ) ) ) |
---|
[a21a8ac] | 227 | { |
---|
| 228 | struct xt_node *act; |
---|
| 229 | |
---|
| 230 | /* If the user likes typing notification and if we don't know |
---|
[788a1af] | 231 | (and didn't probe before) if this resource supports XEP85, |
---|
[abbd8ed] | 232 | include a probe in this packet now. Also, if we know this |
---|
| 233 | buddy does support XEP85, we have to send this <active/> |
---|
| 234 | tag to tell that the user stopped typing (well, that's what |
---|
| 235 | we guess when s/he pressed Enter...). */ |
---|
[a21a8ac] | 236 | act = xt_new_node( "active", NULL, NULL ); |
---|
[47d3ac4] | 237 | xt_add_attr( act, "xmlns", XMLNS_CHATSTATES ); |
---|
[a21a8ac] | 238 | xt_add_child( node, act ); |
---|
| 239 | |
---|
| 240 | /* Just make sure we do this only once. */ |
---|
[788a1af] | 241 | bud->flags |= JBFLAG_PROBED_XEP85; |
---|
[a21a8ac] | 242 | } |
---|
| 243 | |
---|
[4a0614e] | 244 | st = jabber_write_packet( gc, node ); |
---|
| 245 | xt_free_node( node ); |
---|
| 246 | |
---|
| 247 | return st; |
---|
[f06894d] | 248 | } |
---|
| 249 | |
---|
[dd788bb] | 250 | static GList *jabber_away_states( struct gaim_connection *gc ) |
---|
| 251 | { |
---|
[5e202b0] | 252 | static GList *l = NULL; |
---|
| 253 | int i; |
---|
[dd788bb] | 254 | |
---|
[5e202b0] | 255 | if( l == NULL ) |
---|
| 256 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
| 257 | l = g_list_append( l, (void*) jabber_away_state_list[i].full_name ); |
---|
[dd788bb] | 258 | |
---|
[5e202b0] | 259 | return l; |
---|
[dd788bb] | 260 | } |
---|
| 261 | |
---|
[038d17f] | 262 | static void jabber_get_info( struct gaim_connection *gc, char *who ) |
---|
| 263 | { |
---|
[7e83adca] | 264 | struct jabber_data *jd = gc->proto_data; |
---|
[6a1128d] | 265 | struct jabber_buddy *bud; |
---|
[038d17f] | 266 | |
---|
[7e83adca] | 267 | if( strchr( who, '/' ) ) |
---|
[0d3f30f] | 268 | bud = jabber_buddy_by_jid( gc, who, 0 ); |
---|
[7e83adca] | 269 | else |
---|
[0d3f30f] | 270 | { |
---|
| 271 | char *s = jabber_normalize( who ); |
---|
| 272 | bud = g_hash_table_lookup( jd->buddies, s ); |
---|
| 273 | g_free( s ); |
---|
| 274 | } |
---|
[7e83adca] | 275 | |
---|
[6a1128d] | 276 | while( bud ) |
---|
| 277 | { |
---|
[0d3f30f] | 278 | serv_got_crap( gc, "Buddy %s (%d) information:\nAway state: %s\nAway message: %s", |
---|
| 279 | bud->full_jid, bud->priority, |
---|
[6a1128d] | 280 | bud->away_state ? bud->away_state->full_name : "(none)", |
---|
| 281 | bud->away_message ? : "(none)" ); |
---|
| 282 | bud = bud->next; |
---|
| 283 | } |
---|
[1991be6] | 284 | |
---|
[0d3f30f] | 285 | jabber_get_vcard( gc, bud ? bud->full_jid : who ); |
---|
[038d17f] | 286 | } |
---|
| 287 | |
---|
[5e202b0] | 288 | static void jabber_set_away( struct gaim_connection *gc, char *state_txt, char *message ) |
---|
[dd788bb] | 289 | { |
---|
[5e202b0] | 290 | struct jabber_data *jd = gc->proto_data; |
---|
| 291 | struct jabber_away_state *state; |
---|
| 292 | |
---|
| 293 | /* Save all this info. We need it, for example, when changing the priority setting. */ |
---|
| 294 | state = (void *) jabber_away_state_by_name( state_txt ); |
---|
| 295 | jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */ |
---|
| 296 | g_free( jd->away_message ); |
---|
| 297 | jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL; |
---|
| 298 | |
---|
[172a73f1] | 299 | presence_send_update( gc ); |
---|
[deff040] | 300 | } |
---|
| 301 | |
---|
[cfbb3a6] | 302 | static void jabber_add_buddy( struct gaim_connection *gc, char *who ) |
---|
| 303 | { |
---|
| 304 | if( jabber_add_to_roster( gc, who, NULL ) ) |
---|
| 305 | presence_send_request( gc, who, "subscribe" ); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | static void jabber_remove_buddy( struct gaim_connection *gc, char *who, char *group ) |
---|
| 309 | { |
---|
[788a1af] | 310 | /* We should always do this part. Clean up our administration a little bit. */ |
---|
| 311 | jabber_buddy_remove_bare( gc, who ); |
---|
| 312 | |
---|
[cfbb3a6] | 313 | if( jabber_remove_from_roster( gc, who ) ) |
---|
| 314 | presence_send_request( gc, who, "unsubscribe" ); |
---|
| 315 | } |
---|
| 316 | |
---|
[deff040] | 317 | static void jabber_keepalive( struct gaim_connection *gc ) |
---|
| 318 | { |
---|
| 319 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
| 320 | jabber_write( gc, "\n", 1 ); |
---|
[a214954] | 321 | |
---|
[038d17f] | 322 | /* This runs the garbage collection every minute, which means every packet |
---|
| 323 | is in the cache for about a minute (which should be enough AFAIK). */ |
---|
| 324 | jabber_cache_clean( gc ); |
---|
[dd788bb] | 325 | } |
---|
| 326 | |
---|
[a21a8ac] | 327 | static int jabber_send_typing( struct gaim_connection *gc, char *who, int typing ) |
---|
| 328 | { |
---|
| 329 | struct jabber_data *jd = gc->proto_data; |
---|
| 330 | struct jabber_buddy *bud; |
---|
| 331 | |
---|
| 332 | /* Enable typing notification related code from now. */ |
---|
| 333 | jd->flags |= JFLAG_WANT_TYPING; |
---|
| 334 | |
---|
[0d3f30f] | 335 | if( ( bud = jabber_buddy_by_jid( gc, who, 0 ) ) == NULL ) |
---|
[788a1af] | 336 | { |
---|
| 337 | /* Sending typing notifications to unknown buddies is |
---|
| 338 | unsupported for now. Shouldn't be a problem, I think. */ |
---|
| 339 | return 0; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | if( bud->flags & JBFLAG_DOES_XEP85 ) |
---|
[a21a8ac] | 343 | { |
---|
| 344 | /* We're only allowed to send this stuff if we know the other |
---|
| 345 | side supports it. */ |
---|
| 346 | |
---|
| 347 | struct xt_node *node; |
---|
| 348 | char *type; |
---|
| 349 | int st; |
---|
| 350 | |
---|
| 351 | if( typing == 0 ) |
---|
| 352 | type = "active"; |
---|
| 353 | else if( typing == 2 ) |
---|
| 354 | type = "paused"; |
---|
| 355 | else /* if( typing == 1 ) */ |
---|
| 356 | type = "composing"; |
---|
| 357 | |
---|
| 358 | node = xt_new_node( type, NULL, NULL ); |
---|
[47d3ac4] | 359 | xt_add_attr( node, "xmlns", XMLNS_CHATSTATES ); |
---|
[a21a8ac] | 360 | node = jabber_make_packet( "message", "chat", bud->full_jid, node ); |
---|
| 361 | |
---|
| 362 | st = jabber_write_packet( gc, node ); |
---|
| 363 | xt_free_node( node ); |
---|
| 364 | |
---|
| 365 | return st; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | return 1; |
---|
| 369 | } |
---|
| 370 | |
---|
[f06894d] | 371 | void jabber_init() |
---|
| 372 | { |
---|
[deff040] | 373 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
[f06894d] | 374 | |
---|
| 375 | ret->name = "jabber"; |
---|
| 376 | ret->login = jabber_login; |
---|
| 377 | ret->acc_init = jabber_acc_init; |
---|
| 378 | ret->close = jabber_close; |
---|
| 379 | ret->send_im = jabber_send_im; |
---|
[dd788bb] | 380 | ret->away_states = jabber_away_states; |
---|
[f06894d] | 381 | // ret->get_status_string = jabber_get_status_string; |
---|
[dd788bb] | 382 | ret->set_away = jabber_set_away; |
---|
[f06894d] | 383 | // ret->set_info = jabber_set_info; |
---|
[038d17f] | 384 | ret->get_info = jabber_get_info; |
---|
[cfbb3a6] | 385 | ret->add_buddy = jabber_add_buddy; |
---|
| 386 | ret->remove_buddy = jabber_remove_buddy; |
---|
[f06894d] | 387 | // ret->chat_send = jabber_chat_send; |
---|
| 388 | // ret->chat_invite = jabber_chat_invite; |
---|
| 389 | // ret->chat_leave = jabber_chat_leave; |
---|
| 390 | // ret->chat_open = jabber_chat_open; |
---|
[deff040] | 391 | ret->keepalive = jabber_keepalive; |
---|
[a21a8ac] | 392 | ret->send_typing = jabber_send_typing; |
---|
[f06894d] | 393 | ret->handle_cmp = g_strcasecmp; |
---|
| 394 | |
---|
[deff040] | 395 | register_protocol( ret ); |
---|
[f06894d] | 396 | } |
---|