source: protocols/jabber/jabber.c @ 8a90001

Last change on this file since 8a90001 was 8a90001, checked in by ulim <a.sporto+bee@…>, at 2008-07-22T12:37:49Z

Added an account setting 'proxy'.

Note that this is only used for sending. The default <local>;<auto> means let
the receiver try a direct connection first and then the proxy discovered from
the server (if any). If you know you're firewalled you can remove the <local>.
If you want to provide your own proxy try something like
"<local>;JID,HOST,PORT". E.g.
"<local>;proxy.somewhere.org,123.123.123.123,7777".

  • Property mode set to 100644
File size: 15.2 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        if( jd->fd >= 0 )
248                jabber_end_stream( ic );
249       
250        while( ic->groupchats )
251                jabber_chat_free( ic->groupchats );
252       
253        if( jd->r_inpa >= 0 )
254                b_event_remove( jd->r_inpa );
255        if( jd->w_inpa >= 0 )
256                b_event_remove( jd->w_inpa );
257       
258        if( jd->ssl )
259                ssl_disconnect( jd->ssl );
260        if( jd->fd >= 0 )
261                closesocket( jd->fd );
262       
263        if( jd->tx_len )
264                g_free( jd->txq );
265       
266        if( jd->node_cache )
267                g_hash_table_destroy( jd->node_cache );
268       
269        xt_free( jd->xt );
270       
271        g_free( jd->cached_id_prefix );
272        g_free( jd->away_message );
273        g_free( jd->username );
274        g_free( jd );
275       
276        jabber_connections = g_slist_remove( jabber_connections, ic );
277}
278
279static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
280{
281        struct jabber_data *jd = ic->proto_data;
282        struct jabber_buddy *bud;
283        struct xt_node *node;
284        char *s;
285        int st;
286       
287        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
288                return jabber_write( ic, message, strlen( message ) );
289       
290        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
291                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
292        else
293                bud = jabber_buddy_by_jid( ic, who, 0 );
294       
295        node = xt_new_node( "body", message, NULL );
296        node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node );
297       
298        if( bud && ( jd->flags & JFLAG_WANT_TYPING ) &&
299            ( ( bud->flags & JBFLAG_DOES_XEP85 ) ||
300             !( bud->flags & JBFLAG_PROBED_XEP85 ) ) )
301        {
302                struct xt_node *act;
303               
304                /* If the user likes typing notification and if we don't know
305                   (and didn't probe before) if this resource supports XEP85,
306                   include a probe in this packet now. Also, if we know this
307                   buddy does support XEP85, we have to send this <active/>
308                   tag to tell that the user stopped typing (well, that's what
309                   we guess when s/he pressed Enter...). */
310                act = xt_new_node( "active", NULL, NULL );
311                xt_add_attr( act, "xmlns", XMLNS_CHATSTATES );
312                xt_add_child( node, act );
313               
314                /* Just make sure we do this only once. */
315                bud->flags |= JBFLAG_PROBED_XEP85;
316        }
317       
318        st = jabber_write_packet( ic, node );
319        xt_free_node( node );
320       
321        return st;
322}
323
324static GList *jabber_away_states( struct im_connection *ic )
325{
326        static GList *l = NULL;
327        int i;
328       
329        if( l == NULL )
330                for( i = 0; jabber_away_state_list[i].full_name; i ++ )
331                        l = g_list_append( l, (void*) jabber_away_state_list[i].full_name );
332       
333        return l;
334}
335
336static void jabber_get_info( struct im_connection *ic, char *who )
337{
338        struct jabber_data *jd = ic->proto_data;
339        struct jabber_buddy *bud;
340       
341        if( strchr( who, '/' ) )
342                bud = jabber_buddy_by_jid( ic, who, 0 );
343        else
344        {
345                char *s = jabber_normalize( who );
346                bud = g_hash_table_lookup( jd->buddies, s );
347                g_free( s );
348        }
349       
350        while( bud )
351        {
352                imcb_log( ic, "Buddy %s (%d) information:\nAway state: %s\nAway message: %s",
353                                   bud->full_jid, bud->priority,
354                                   bud->away_state ? bud->away_state->full_name : "(none)",
355                                   bud->away_message ? : "(none)" );
356                bud = bud->next;
357        }
358       
359        jabber_get_vcard( ic, bud ? bud->full_jid : who );
360}
361
362static void jabber_set_away( struct im_connection *ic, char *state_txt, char *message )
363{
364        struct jabber_data *jd = ic->proto_data;
365        struct jabber_away_state *state;
366       
367        /* Save all this info. We need it, for example, when changing the priority setting. */
368        state = (void *) jabber_away_state_by_name( state_txt );
369        jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */
370        g_free( jd->away_message );
371        jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL;
372       
373        presence_send_update( ic );
374}
375
376static void jabber_add_buddy( struct im_connection *ic, char *who, char *group )
377{
378        struct jabber_data *jd = ic->proto_data;
379       
380        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
381        {
382                jd->flags |= JFLAG_XMLCONSOLE;
383                imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
384                return;
385        }
386       
387        if( jabber_add_to_roster( ic, who, NULL ) )
388                presence_send_request( ic, who, "subscribe" );
389}
390
391static void jabber_remove_buddy( struct im_connection *ic, char *who, char *group )
392{
393        struct jabber_data *jd = ic->proto_data;
394       
395        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
396        {
397                jd->flags &= ~JFLAG_XMLCONSOLE;
398                /* Not necessary for now. And for now the code isn't too
399                   happy if the buddy is completely gone right after calling
400                   this function already.
401                imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
402                */
403                return;
404        }
405       
406        /* We should always do this part. Clean up our administration a little bit. */
407        jabber_buddy_remove_bare( ic, who );
408       
409        if( jabber_remove_from_roster( ic, who ) )
410                presence_send_request( ic, who, "unsubscribe" );
411}
412
413static struct groupchat *jabber_chat_join_( struct im_connection *ic, char *room, char *nick, char *password )
414{
415        if( strchr( room, '@' ) == NULL )
416                imcb_error( ic, "Invalid room name: %s", room );
417        else if( jabber_chat_by_jid( ic, room ) )
418                imcb_error( ic, "Already present in chat `%s'", room );
419        else
420                return jabber_chat_join( ic, room, nick, password );
421       
422        return NULL;
423}
424
425static void jabber_chat_msg_( struct groupchat *c, char *message, int flags )
426{
427        if( c && message )
428                jabber_chat_msg( c, message, flags );
429}
430
431static void jabber_chat_topic_( struct groupchat *c, char *topic )
432{
433        if( c && topic )
434                jabber_chat_topic( c, topic );
435}
436
437static void jabber_chat_leave_( struct groupchat *c )
438{
439        if( c )
440                jabber_chat_leave( c, NULL );
441}
442
443static void jabber_chat_invite_( struct groupchat *c, char *who, char *msg )
444{
445        struct jabber_chat *jc = c->data;
446        gchar *msg_alt = NULL;
447
448        if( msg == NULL )
449                msg_alt = g_strdup_printf( "%s invited you to %s", c->ic->acc->user, jc->name );
450       
451        if( c && who )
452                jabber_chat_invite( c, who, msg ? msg : msg_alt );
453       
454        g_free( msg_alt );
455}
456
457static void jabber_keepalive( struct im_connection *ic )
458{
459        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
460        jabber_write( ic, "\n", 1 );
461       
462        /* This runs the garbage collection every minute, which means every packet
463           is in the cache for about a minute (which should be enough AFAIK). */
464        jabber_cache_clean( ic );
465}
466
467static int jabber_send_typing( struct im_connection *ic, char *who, int typing )
468{
469        struct jabber_data *jd = ic->proto_data;
470        struct jabber_buddy *bud;
471       
472        /* Enable typing notification related code from now. */
473        jd->flags |= JFLAG_WANT_TYPING;
474       
475        if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL )
476        {
477                /* Sending typing notifications to unknown buddies is
478                   unsupported for now. Shouldn't be a problem, I think. */
479                return 0;
480        }
481       
482        if( bud->flags & JBFLAG_DOES_XEP85 )
483        {
484                /* We're only allowed to send this stuff if we know the other
485                   side supports it. */
486               
487                struct xt_node *node;
488                char *type;
489                int st;
490               
491                if( typing & OPT_TYPING )
492                        type = "composing";
493                else if( typing & OPT_THINKING )
494                        type = "paused";
495                else
496                        type = "active";
497               
498                node = xt_new_node( type, NULL, NULL );
499                xt_add_attr( node, "xmlns", XMLNS_CHATSTATES );
500                node = jabber_make_packet( "message", "chat", bud->full_jid, node );
501               
502                st = jabber_write_packet( ic, node );
503                xt_free_node( node );
504               
505                return st;
506        }
507       
508        return 1;
509}
510
511void jabber_initmodule()
512{
513        struct prpl *ret = g_new0( struct prpl, 1 );
514       
515        ret->name = "jabber";
516        ret->login = jabber_login;
517        ret->init = jabber_init;
518        ret->logout = jabber_logout;
519        ret->buddy_msg = jabber_buddy_msg;
520        ret->away_states = jabber_away_states;
521        ret->set_away = jabber_set_away;
522//      ret->set_info = jabber_set_info;
523        ret->get_info = jabber_get_info;
524        ret->add_buddy = jabber_add_buddy;
525        ret->remove_buddy = jabber_remove_buddy;
526        ret->chat_msg = jabber_chat_msg_;
527        ret->chat_topic = jabber_chat_topic_;
528        ret->chat_invite = jabber_chat_invite_;
529        ret->chat_leave = jabber_chat_leave_;
530        ret->chat_join = jabber_chat_join_;
531        ret->keepalive = jabber_keepalive;
532        ret->send_typing = jabber_send_typing;
533        ret->handle_cmp = g_strcasecmp;
534        ret->transfer_request = jabber_si_transfer_request;
535
536        register_protocol( ret );
537}
Note: See TracBrowser for help on using the repository browser.