source: protocols/jabber/jabber.c @ 4ac647d

Last change on this file since 4ac647d was 4ac647d, checked in by ulim <a.sporto+bee@…>, at 2008-08-04T14:21:49Z

moved some stuff around in preperation for MSN merge.

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