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