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