source: protocols/jabber/jabber.c @ 68286eb

Last change on this file since 68286eb was 68286eb, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-12-20T16:45:53Z

Detect JID changes at login time and warn the user about them.

  • Property mode set to 100644
File size: 17.8 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
36GSList *jabber_connections;
37
38/* First enty is the default */
39static 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
55static 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        s = set_add( &acc->set, "oauth", "false", set_eval_bool, acc );
63
64        g_snprintf( str, sizeof( str ), "%d", jabber_port_list[0] );
65        s = set_add( &acc->set, "port", str, set_eval_int, acc );
66        s->flags |= ACC_SET_OFFLINE_ONLY;
67       
68        s = set_add( &acc->set, "priority", "0", set_eval_priority, acc );
69
70        s = set_add( &acc->set, "proxy", "<local>;<auto>", NULL, acc );
71       
72        s = set_add( &acc->set, "resource", "BitlBee", NULL, acc );
73        s->flags |= ACC_SET_OFFLINE_ONLY;
74       
75        s = set_add( &acc->set, "resource_select", "activity", NULL, acc );
76       
77        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
78        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK;
79       
80        s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc );
81        s->flags |= ACC_SET_OFFLINE_ONLY;
82       
83        s = set_add( &acc->set, "tls", "try", set_eval_tls, acc );
84        s->flags |= ACC_SET_OFFLINE_ONLY;
85       
86        s = set_add( &acc->set, "sasl", "true", set_eval_bool, acc );
87        s->flags |= ACC_SET_OFFLINE_ONLY | SET_HIDDEN_DEFAULT;
88
89        s = set_add( &acc->set, "user_agent", "BitlBee", NULL, acc );
90       
91        s = set_add( &acc->set, "xmlconsole", "false", set_eval_bool, acc );
92        s->flags |= ACC_SET_OFFLINE_ONLY;
93       
94        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE;
95}
96
97static void jabber_generate_id_hash( struct jabber_data *jd );
98
99static void jabber_login( account_t *acc )
100{
101        struct im_connection *ic = imcb_new( acc );
102        struct jabber_data *jd = g_new0( struct jabber_data, 1 );
103        char *s;
104       
105        /* For now this is needed in the _connected() handlers if using
106           GLib event handling, to make sure we're not handling events
107           on dead connections. */
108        jabber_connections = g_slist_prepend( jabber_connections, ic );
109       
110        jd->ic = ic;
111        ic->proto_data = jd;
112       
113        jabber_set_me( ic, acc->user );
114       
115        jd->fd = jd->r_inpa = jd->w_inpa = -1;
116       
117        if( jd->server == NULL )
118        {
119                imcb_error( ic, "Incomplete account name (format it like <username@jabberserver.name>)" );
120                imc_logout( ic, FALSE );
121                return;
122        }
123       
124        if( ( s = strchr( jd->server, '/' ) ) )
125        {
126                *s = 0;
127                set_setstr( &acc->set, "resource", s + 1 );
128               
129                /* Also remove the /resource from the original variable so we
130                   won't have to do this again every time. */
131                s = strchr( acc->user, '/' );
132                *s = 0;
133        }
134       
135        jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free );
136        jd->buddies = g_hash_table_new( g_str_hash, g_str_equal );
137       
138        if( set_getbool( &acc->set, "oauth" ) )
139        {
140                jd->fd = jd->r_inpa = jd->w_inpa = -1;
141               
142                if( strstr( jd->server, ".live.com" ) )
143                        jd->oauth2_service = &oauth2_service_mslive;
144                else if( strstr( jd->server, ".facebook.com" ) )
145                        jd->oauth2_service = &oauth2_service_facebook;
146                else
147                        jd->oauth2_service = &oauth2_service_google;
148               
149                /* First see if we have a refresh token, in which case any
150                   access token we *might* have has probably expired already
151                   anyway. */
152                if( strstr( acc->pass, "refresh_token=" ) )
153                {
154                        sasl_oauth2_refresh( ic, acc->pass + 14 );
155                }
156                /* If we don't have a refresh token, let's hope the access
157                   token is still usable. */
158                else if( strstr( acc->pass, "access_token=" ) )
159                {
160                        sasl_oauth2_load_access_token( ic );
161                        jabber_connect( ic );
162                }
163                /* If we don't have any, start the OAuth process now. Don't
164                   even open an XMPP connection yet. */
165                else
166                {
167                        sasl_oauth2_init( ic );
168                        ic->flags |= OPT_SLOW_LOGIN;
169                }
170        }
171        else
172                jabber_connect( ic );
173}
174
175/* Separate this from jabber_login() so we can do OAuth first if necessary.
176   Putting this in io.c would probably be more correct. */
177void jabber_connect( struct im_connection *ic )
178{
179        account_t *acc = ic->acc;
180        struct jabber_data *jd = ic->proto_data;
181        int i;
182        char *connect_to;
183        struct ns_srv_reply **srvl = NULL, *srv = NULL;
184       
185        /* Figure out the hostname to connect to. */
186        if( acc->server && *acc->server )
187                connect_to = acc->server;
188        else if( ( srvl = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
189                 ( srvl = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
190        {
191                /* Find the lowest-priority one. These usually come
192                   back in random/shuffled order. Not looking at
193                   weights etc for now. */
194                srv = *srvl;
195                for( i = 1; srvl[i]; i ++ )
196                        if( srvl[i]->prio < srv->prio )
197                                srv = srvl[i];
198               
199                connect_to = srv->name;
200        }
201        else
202                connect_to = jd->server;
203       
204        imcb_log( ic, "Connecting" );
205       
206        for( i = 0; jabber_port_list[i] > 0; i ++ )
207                if( set_getint( &acc->set, "port" ) == jabber_port_list[i] )
208                        break;
209
210        if( jabber_port_list[i] == 0 )
211        {
212                imcb_log( ic, "Illegal port number" );
213                imc_logout( ic, FALSE );
214                return;
215        }
216       
217        /* For non-SSL connections we can try to use the port # from the SRV
218           reply, but let's not do that when using SSL, SSL usually runs on
219           non-standard ports... */
220        if( set_getbool( &acc->set, "ssl" ) )
221        {
222                jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, ic );
223                jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1;
224        }
225        else
226        {
227                jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic );
228        }
229        srv_free( srvl );
230       
231        if( jd->fd == -1 )
232        {
233                imcb_error( ic, "Could not connect to server" );
234                imc_logout( ic, TRUE );
235               
236                return;
237        }
238       
239        if( set_getbool( &acc->set, "xmlconsole" ) )
240        {
241                jd->flags |= JFLAG_XMLCONSOLE;
242                /* Shouldn't really do this at this stage already, maybe. But
243                   I think this shouldn't break anything. */
244                imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
245        }
246       
247        jabber_generate_id_hash( jd );
248}
249
250/* This generates an unfinished md5_state_t variable. Every time we generate
251   an ID, we finish the state by adding a sequence number and take the hash. */
252static void jabber_generate_id_hash( struct jabber_data *jd )
253{
254        md5_byte_t binbuf[4];
255        char *s;
256       
257        md5_init( &jd->cached_id_prefix );
258        md5_append( &jd->cached_id_prefix, (unsigned char *) jd->username, strlen( jd->username ) );
259        md5_append( &jd->cached_id_prefix, (unsigned char *) jd->server, strlen( jd->server ) );
260        s = set_getstr( &jd->ic->acc->set, "resource" );
261        md5_append( &jd->cached_id_prefix, (unsigned char *) s, strlen( s ) );
262        random_bytes( binbuf, 4 );
263        md5_append( &jd->cached_id_prefix, binbuf, 4 );
264}
265
266static void jabber_logout( struct im_connection *ic )
267{
268        struct jabber_data *jd = ic->proto_data;
269       
270        while( jd->filetransfers )
271                imcb_file_canceled( ic, ( ( struct jabber_transfer *) jd->filetransfers->data )->ft, "Logging out" );
272
273        while( jd->streamhosts )
274        {
275                jabber_streamhost_t *sh = jd->streamhosts->data;
276                jd->streamhosts = g_slist_remove( jd->streamhosts, sh );
277                g_free( sh->jid );
278                g_free( sh->host );
279                g_free( sh );
280        }
281
282        if( jd->fd >= 0 )
283                jabber_end_stream( ic );
284       
285        while( ic->groupchats )
286                jabber_chat_free( ic->groupchats->data );
287       
288        if( jd->r_inpa >= 0 )
289                b_event_remove( jd->r_inpa );
290        if( jd->w_inpa >= 0 )
291                b_event_remove( jd->w_inpa );
292       
293        if( jd->ssl )
294                ssl_disconnect( jd->ssl );
295        if( jd->fd >= 0 )
296                closesocket( jd->fd );
297       
298        if( jd->tx_len )
299                g_free( jd->txq );
300       
301        if( jd->node_cache )
302                g_hash_table_destroy( jd->node_cache );
303       
304        jabber_buddy_remove_all( ic );
305       
306        xt_free( jd->xt );
307       
308        g_free( jd->oauth2_access_token );
309        g_free( jd->away_message );
310        g_free( jd->username );
311        g_free( jd->me );
312        g_free( jd );
313       
314        jabber_connections = g_slist_remove( jabber_connections, ic );
315}
316
317static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
318{
319        struct jabber_data *jd = ic->proto_data;
320        struct jabber_buddy *bud;
321        struct xt_node *node;
322        char *s;
323        int st;
324       
325        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
326                return jabber_write( ic, message, strlen( message ) );
327       
328        if( g_strcasecmp( who, "jabber_oauth" ) == 0 )
329        {
330                if( sasl_oauth2_get_refresh_token( ic, message ) )
331                {
332                        return 1;
333                }
334                else
335                {
336                        imcb_error( ic, "OAuth failure" );
337                        imc_logout( ic, TRUE );
338                        return 0;
339                }
340        }
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
376static 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
388static 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
407static 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
424static 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
439static 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
461static 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
473static 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
479static void jabber_chat_topic_( struct groupchat *c, char *topic )
480{
481        if( c && topic )
482                jabber_chat_topic( c, topic );
483}
484
485static void jabber_chat_leave_( struct groupchat *c )
486{
487        if( c )
488                jabber_chat_leave( c, NULL );
489}
490
491static void jabber_chat_invite_( struct groupchat *c, char *who, char *msg )
492{
493        struct jabber_data *jd = c->ic->proto_data;
494        struct jabber_chat *jc = c->data;
495        gchar *msg_alt = NULL;
496
497        if( msg == NULL )
498                msg_alt = g_strdup_printf( "%s invited you to %s", jd->me, jc->name );
499       
500        if( c && who )
501                jabber_chat_invite( c, who, msg ? msg : msg_alt );
502       
503        g_free( msg_alt );
504}
505
506static void jabber_keepalive( struct im_connection *ic )
507{
508        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
509        if( !jabber_write( ic, "\n", 1 ) )
510                return;
511       
512        /* This runs the garbage collection every minute, which means every packet
513           is in the cache for about a minute (which should be enough AFAIK). */
514        jabber_cache_clean( ic );
515}
516
517static int jabber_send_typing( struct im_connection *ic, char *who, int typing )
518{
519        struct jabber_data *jd = ic->proto_data;
520        struct jabber_buddy *bud;
521       
522        /* Enable typing notification related code from now. */
523        jd->flags |= JFLAG_WANT_TYPING;
524       
525        if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL )
526        {
527                /* Sending typing notifications to unknown buddies is
528                   unsupported for now. Shouldn't be a problem, I think. */
529                return 0;
530        }
531       
532        if( bud->flags & JBFLAG_DOES_XEP85 )
533        {
534                /* We're only allowed to send this stuff if we know the other
535                   side supports it. */
536               
537                struct xt_node *node;
538                char *type;
539                int st;
540               
541                if( typing & OPT_TYPING )
542                        type = "composing";
543                else if( typing & OPT_THINKING )
544                        type = "paused";
545                else
546                        type = "active";
547               
548                node = xt_new_node( type, NULL, NULL );
549                xt_add_attr( node, "xmlns", XMLNS_CHATSTATES );
550                node = jabber_make_packet( "message", "chat", bud->full_jid, node );
551               
552                st = jabber_write_packet( ic, node );
553                xt_free_node( node );
554               
555                return st;
556        }
557       
558        return 1;
559}
560
561void jabber_chat_add_settings( account_t *acc, set_t **head )
562{
563        /* Meh. Stupid room passwords. Not trying to obfuscate/hide
564           them from the user for now. */
565        set_add( head, "password", NULL, NULL, NULL );
566}
567
568void jabber_chat_free_settings( account_t *acc, set_t **head )
569{
570        set_del( head, "password" );
571}
572
573GList *jabber_buddy_action_list( bee_user_t *bu )
574{
575        static GList *ret = NULL;
576       
577        if( ret == NULL )
578        {
579                static const struct buddy_action ba[2] = {
580                        { "VERSION", "Get client (version) information" },
581                };
582               
583                ret = g_list_prepend( ret, (void*) ba + 0 );
584        }
585       
586        return ret;
587}
588
589void *jabber_buddy_action( struct bee_user *bu, const char *action, char * const args[], void *data )
590{
591        if( g_strcasecmp( action, "VERSION" ) == 0 )
592        {
593                struct jabber_buddy *bud;
594               
595                if( ( bud = jabber_buddy_by_ext_jid( bu->ic, bu->handle, 0 ) ) == NULL )
596                        bud = jabber_buddy_by_jid( bu->ic, bu->handle, GET_BUDDY_FIRST );
597                for( ; bud; bud = bud->next )
598                        jabber_iq_version_send( bu->ic, bud, data );
599        }
600       
601        return NULL;
602}
603
604void jabber_initmodule()
605{
606        struct prpl *ret = g_new0( struct prpl, 1 );
607       
608        ret->name = "jabber";
609        ret->mms = 0;                        /* no limit */
610        ret->login = jabber_login;
611        ret->init = jabber_init;
612        ret->logout = jabber_logout;
613        ret->buddy_msg = jabber_buddy_msg;
614        ret->away_states = jabber_away_states;
615        ret->set_away = jabber_set_away;
616//      ret->set_info = jabber_set_info;
617        ret->get_info = jabber_get_info;
618        ret->add_buddy = jabber_add_buddy;
619        ret->remove_buddy = jabber_remove_buddy;
620        ret->chat_msg = jabber_chat_msg_;
621        ret->chat_topic = jabber_chat_topic_;
622        ret->chat_invite = jabber_chat_invite_;
623        ret->chat_leave = jabber_chat_leave_;
624        ret->chat_join = jabber_chat_join_;
625        ret->chat_add_settings = jabber_chat_add_settings;
626        ret->chat_free_settings = jabber_chat_free_settings;
627        ret->keepalive = jabber_keepalive;
628        ret->send_typing = jabber_send_typing;
629        ret->handle_cmp = g_strcasecmp;
630        ret->transfer_request = jabber_si_transfer_request;
631        ret->buddy_action_list = jabber_buddy_action_list;
632        ret->buddy_action = jabber_buddy_action;
633
634        register_protocol( ret );
635}
Note: See TracBrowser for help on using the repository browser.