source: protocols/jabber/jabber.c @ 78d254f1

Last change on this file since 78d254f1 was e8c8d00, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-17T15:15:19Z

Merging mainline.

  • Property mode set to 100644
File size: 15.7 KB
RevLine 
[f06894d]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - Main file                                                *
5*                                                                           *
6*  Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net>                   *
7*                                                                           *
8*  This program is free software; you can redistribute it and/or modify     *
9*  it under the terms of the GNU General Public License as published by     *
10*  the Free Software Foundation; either version 2 of the License, or        *
11*  (at your option) any later version.                                      *
12*                                                                           *
13*  This program is distributed in the hope that it will be useful,          *
14*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
15*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
16*  GNU General Public License for more details.                             *
17*                                                                           *
18*  You should have received a copy of the GNU General Public License along  *
19*  with this program; if not, write to the Free Software Foundation, Inc.,  *
20*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
21*                                                                           *
22\***************************************************************************/
23
24#include <glib.h>
25#include <string.h>
26#include <unistd.h>
27#include <ctype.h>
28#include <stdio.h>
29
[21167d2]30#include "ssl_client.h"
[f06894d]31#include "xmltree.h"
32#include "bitlbee.h"
33#include "jabber.h"
[608f8cf]34#include "md5.h"
[f06894d]35
[b5c8a34]36GSList *jabber_connections;
37
[7f69740]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
[0da65d5]55static void jabber_init( account_t *acc )
[f06894d]56{
57        set_t *s;
[7f69740]58        char str[16];
[f06894d]59       
[7f69740]60        g_snprintf( str, sizeof( str ), "%d", jabber_port_list[0] );
61        s = set_add( &acc->set, "port", str, set_eval_int, acc );
[f06894d]62        s->flags |= ACC_SET_OFFLINE_ONLY;
63       
[ebe7b36]64        s = set_add( &acc->set, "priority", "0", set_eval_priority, acc );
[1c3008a]65
66        s = set_add( &acc->set, "proxy", "<local>;<auto>", NULL, acc );
[f06894d]67       
[ebe7b36]68        s = set_add( &acc->set, "resource", "BitlBee", NULL, acc );
69        s->flags |= ACC_SET_OFFLINE_ONLY;
[f06894d]70       
[f9928cb]71        s = set_add( &acc->set, "resource_select", "activity", NULL, acc );
[a21a8ac]72       
[f06894d]73        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
[7125cb3]74        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK;
[f06894d]75       
76        s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc );
77        s->flags |= ACC_SET_OFFLINE_ONLY;
78       
79        s = set_add( &acc->set, "tls", "try", set_eval_tls, acc );
80        s->flags |= ACC_SET_OFFLINE_ONLY;
[a6df0b5]81       
82        s = set_add( &acc->set, "xmlconsole", "false", set_eval_bool, acc );
83        s->flags |= ACC_SET_OFFLINE_ONLY;
[840bba8]84       
85        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE;
[f06894d]86}
87
[608f8cf]88static void jabber_generate_id_hash( struct jabber_data *jd );
89
[f06894d]90static void jabber_login( account_t *acc )
91{
[84b045d]92        struct im_connection *ic = imcb_new( acc );
[21167d2]93        struct jabber_data *jd = g_new0( struct jabber_data, 1 );
[36e9f62]94        struct ns_srv_reply *srv = NULL;
[3b3cd693]95        char *connect_to, *s;
[7f69740]96        int i;
[21167d2]97       
[b5c8a34]98        /* For now this is needed in the _connected() handlers if using
99           GLib event handling, to make sure we're not handling events
100           on dead connections. */
101        jabber_connections = g_slist_prepend( jabber_connections, ic );
102       
[0da65d5]103        jd->ic = ic;
104        ic->proto_data = jd;
[21167d2]105       
106        jd->username = g_strdup( acc->user );
107        jd->server = strchr( jd->username, '@' );
108       
[de03374]109        jd->fd = jd->r_inpa = jd->w_inpa = -1;
110       
[21167d2]111        if( jd->server == NULL )
112        {
[84b045d]113                imcb_error( ic, "Incomplete account name (format it like <username@jabberserver.name>)" );
[c2fb3809]114                imc_logout( ic, FALSE );
[21167d2]115                return;
116        }
117       
118        /* So don't think of free()ing jd->server.. :-) */
119        *jd->server = 0;
120        jd->server ++;
121       
[3b3cd693]122        if( ( s = strchr( jd->server, '/' ) ) )
123        {
124                *s = 0;
125                set_setstr( &acc->set, "resource", s + 1 );
126               
127                /* Also remove the /resource from the original variable so we
128                   won't have to do this again every time. */
129                s = strchr( acc->user, '/' );
130                *s = 0;
131        }
132       
133        /* This code isn't really pretty. Backwards compatibility never is... */
134        s = acc->server;
135        while( s )
136        {
137                static int had_port = 0;
138               
139                if( strncmp( s, "ssl", 3 ) == 0 )
140                {
141                        set_setstr( &acc->set, "ssl", "true" );
142                       
143                        /* Flush this part so that (if this was the first
144                           part of the server string) acc->server gets
145                           flushed. We don't want to have to do this another
146                           time. :-) */
147                        *s = 0;
148                        s ++;
149                       
150                        /* Only set this if the user didn't specify a custom
151                           port number already... */
152                        if( !had_port )
153                                set_setint( &acc->set, "port", 5223 );
154                }
155                else if( isdigit( *s ) )
156                {
157                        int i;
158                       
159                        /* The first character is a digit. It could be an
160                           IP address though. Only accept this as a port#
161                           if there are only digits. */
162                        for( i = 0; isdigit( s[i] ); i ++ );
163                       
164                        /* If the first non-digit character is a colon or
165                           the end of the string, save the port number
166                           where it should be. */
167                        if( s[i] == ':' || s[i] == 0 )
168                        {
169                                sscanf( s, "%d", &i );
170                                set_setint( &acc->set, "port", i );
171                               
172                                /* See above. */
173                                *s = 0;
174                                s ++;
175                        }
176                       
177                        had_port = 1;
178                }
179               
180                s = strchr( s, ':' );
181                if( s )
182                {
183                        *s = 0;
184                        s ++;
185                }
186        }
187       
[038d17f]188        jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free );
[6a1128d]189        jd->buddies = g_hash_table_new( g_str_hash, g_str_equal );
[fe7a554]190       
[36e9f62]191        /* Figure out the hostname to connect to. */
[3b3cd693]192        if( acc->server && *acc->server )
[36e9f62]193                connect_to = acc->server;
194        else if( ( srv = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
195                 ( srv = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
196                connect_to = srv->name;
197        else
198                connect_to = jd->server;
199       
[84b045d]200        imcb_log( ic, "Connecting" );
[35f6677]201       
[7f69740]202        for( i = 0; jabber_port_list[i] > 0; i ++ )
203                if( set_getint( &acc->set, "port" ) == jabber_port_list[i] )
204                        break;
205
206        if( jabber_port_list[i] == 0 )
[0f4c1bb5]207        {
[7f69740]208                imcb_log( ic, "Illegal port number" );
[c2fb3809]209                imc_logout( ic, FALSE );
[0f4c1bb5]210                return;
211        }
212       
[36e9f62]213        /* For non-SSL connections we can try to use the port # from the SRV
214           reply, but let's not do that when using SSL, SSL usually runs on
215           non-standard ports... */
[21167d2]216        if( set_getbool( &acc->set, "ssl" ) )
217        {
[0da65d5]218                jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, ic );
[3b3cd693]219                jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1;
[21167d2]220        }
221        else
222        {
[0da65d5]223                jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic );
[21167d2]224        }
[36e9f62]225        g_free( srv );
[35f6677]226       
227        if( jd->fd == -1 )
228        {
[84b045d]229                imcb_error( ic, "Could not connect to server" );
[c2fb3809]230                imc_logout( ic, TRUE );
[fb4ebcc5]231               
232                return;
[35f6677]233        }
[a6df0b5]234       
235        if( set_getbool( &acc->set, "xmlconsole" ) )
236        {
237                jd->flags |= JFLAG_XMLCONSOLE;
238                /* Shouldn't really do this at this stage already, maybe. But
239                   I think this shouldn't break anything. */
240                imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
241        }
[608f8cf]242       
243        jabber_generate_id_hash( jd );
244}
245
[89d736a]246/* This generates an unfinished md5_state_t variable. Every time we generate
247   an ID, we finish the state by adding a sequence number and take the hash. */
[608f8cf]248static void jabber_generate_id_hash( struct jabber_data *jd )
249{
[89d736a]250        md5_byte_t binbuf[4];
[608f8cf]251        char *s;
252       
[89d736a]253        md5_init( &jd->cached_id_prefix );
254        md5_append( &jd->cached_id_prefix, (unsigned char *) jd->username, strlen( jd->username ) );
255        md5_append( &jd->cached_id_prefix, (unsigned char *) jd->server, strlen( jd->server ) );
[608f8cf]256        s = set_getstr( &jd->ic->acc->set, "resource" );
[89d736a]257        md5_append( &jd->cached_id_prefix, (unsigned char *) s, strlen( s ) );
258        random_bytes( binbuf, 4 );
259        md5_append( &jd->cached_id_prefix, binbuf, 4 );
[f06894d]260}
261
[0da65d5]262static void jabber_logout( struct im_connection *ic )
[f06894d]263{
[0da65d5]264        struct jabber_data *jd = ic->proto_data;
[21167d2]265       
[4ac647d]266        while( jd->filetransfers )
267                imcb_file_canceled( ( ( struct jabber_transfer *) jd->filetransfers->data )->ft, "Logging out" );
268
269        while( jd->streamhosts )
270        {
271                jabber_streamhost_t *sh = jd->streamhosts->data;
272                jd->streamhosts = g_slist_remove( jd->streamhosts, sh );
273                g_free( sh->jid );
274                g_free( sh->host );
275                g_free( sh );
276        }
277
[de03374]278        if( jd->fd >= 0 )
279                jabber_end_stream( ic );
[4a0614e]280       
[c377417]281        while( ic->groupchats )
[9da0bbf]282                jabber_chat_free( ic->groupchats );
[c377417]283       
[21167d2]284        if( jd->r_inpa >= 0 )
285                b_event_remove( jd->r_inpa );
286        if( jd->w_inpa >= 0 )
287                b_event_remove( jd->w_inpa );
288       
289        if( jd->ssl )
290                ssl_disconnect( jd->ssl );
291        if( jd->fd >= 0 )
292                closesocket( jd->fd );
293       
[fe7a554]294        if( jd->tx_len )
295                g_free( jd->txq );
296       
[de03374]297        if( jd->node_cache )
298                g_hash_table_destroy( jd->node_cache );
[038d17f]299       
[70f6aab8]300        xt_free( jd->xt );
301       
[5e202b0]302        g_free( jd->away_message );
[21167d2]303        g_free( jd->username );
304        g_free( jd );
[b5c8a34]305       
306        jabber_connections = g_slist_remove( jabber_connections, ic );
[f06894d]307}
308
[f6c963b]309static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
[f06894d]310{
[0da65d5]311        struct jabber_data *jd = ic->proto_data;
[a21a8ac]312        struct jabber_buddy *bud;
[cc2cb2d]313        struct xt_node *node;
[b9f8b87]314        char *s;
[4a0614e]315        int st;
316       
[bb95d43]317        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
318                return jabber_write( ic, message, strlen( message ) );
319       
[5bd21df]320        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
[b9f8b87]321                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
322        else
323                bud = jabber_buddy_by_jid( ic, who, 0 );
[a214954]324       
[4a0614e]325        node = xt_new_node( "body", message, NULL );
[788a1af]326        node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node );
[a21a8ac]327       
[0d3f30f]328        if( bud && ( jd->flags & JFLAG_WANT_TYPING ) &&
[788a1af]329            ( ( bud->flags & JBFLAG_DOES_XEP85 ) ||
330             !( bud->flags & JBFLAG_PROBED_XEP85 ) ) )
[a21a8ac]331        {
332                struct xt_node *act;
333               
334                /* If the user likes typing notification and if we don't know
[788a1af]335                   (and didn't probe before) if this resource supports XEP85,
[abbd8ed]336                   include a probe in this packet now. Also, if we know this
337                   buddy does support XEP85, we have to send this <active/>
338                   tag to tell that the user stopped typing (well, that's what
339                   we guess when s/he pressed Enter...). */
[a21a8ac]340                act = xt_new_node( "active", NULL, NULL );
[47d3ac4]341                xt_add_attr( act, "xmlns", XMLNS_CHATSTATES );
[a21a8ac]342                xt_add_child( node, act );
343               
344                /* Just make sure we do this only once. */
[788a1af]345                bud->flags |= JBFLAG_PROBED_XEP85;
[a21a8ac]346        }
347       
[0da65d5]348        st = jabber_write_packet( ic, node );
[4a0614e]349        xt_free_node( node );
350       
351        return st;
[f06894d]352}
353
[0da65d5]354static GList *jabber_away_states( struct im_connection *ic )
[dd788bb]355{
[5e202b0]356        static GList *l = NULL;
357        int i;
[dd788bb]358       
[5e202b0]359        if( l == NULL )
360                for( i = 0; jabber_away_state_list[i].full_name; i ++ )
361                        l = g_list_append( l, (void*) jabber_away_state_list[i].full_name );
[dd788bb]362       
[5e202b0]363        return l;
[dd788bb]364}
365
[0da65d5]366static void jabber_get_info( struct im_connection *ic, char *who )
[038d17f]367{
[0da65d5]368        struct jabber_data *jd = ic->proto_data;
[6a1128d]369        struct jabber_buddy *bud;
[038d17f]370       
[7e83adca]371        if( strchr( who, '/' ) )
[0da65d5]372                bud = jabber_buddy_by_jid( ic, who, 0 );
[7e83adca]373        else
[0d3f30f]374        {
375                char *s = jabber_normalize( who );
376                bud = g_hash_table_lookup( jd->buddies, s );
377                g_free( s );
378        }
[7e83adca]379       
[6a1128d]380        while( bud )
381        {
[63770b4]382                imcb_log( ic, "Buddy %s (%d) information:", bud->full_jid, bud->priority );
383                if( bud->away_state )
384                        imcb_log( ic, "Away state: %s", bud->away_state->full_name );
385                imcb_log( ic, "Status message: %s", bud->away_message ? : "(none)" );
386               
[6a1128d]387                bud = bud->next;
388        }
[1991be6]389       
[0da65d5]390        jabber_get_vcard( ic, bud ? bud->full_jid : who );
[038d17f]391}
392
[0da65d5]393static void jabber_set_away( struct im_connection *ic, char *state_txt, char *message )
[dd788bb]394{
[0da65d5]395        struct jabber_data *jd = ic->proto_data;
[5e202b0]396       
[840bba8]397        /* state_txt == NULL -> Not away.
398           Unknown state -> fall back to the first defined away state. */
399        jd->away_state = state_txt ? jabber_away_state_by_name( state_txt )
400                         ? : jabber_away_state_list : NULL;
401       
[5e202b0]402        g_free( jd->away_message );
403        jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL;
404       
[0da65d5]405        presence_send_update( ic );
[deff040]406}
407
[0da65d5]408static void jabber_add_buddy( struct im_connection *ic, char *who, char *group )
[cfbb3a6]409{
[bb95d43]410        struct jabber_data *jd = ic->proto_data;
411       
412        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
413        {
414                jd->flags |= JFLAG_XMLCONSOLE;
415                imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
416                return;
417        }
418       
[0da65d5]419        if( jabber_add_to_roster( ic, who, NULL ) )
420                presence_send_request( ic, who, "subscribe" );
[cfbb3a6]421}
422
[0da65d5]423static void jabber_remove_buddy( struct im_connection *ic, char *who, char *group )
[cfbb3a6]424{
[bb95d43]425        struct jabber_data *jd = ic->proto_data;
426       
427        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
428        {
429                jd->flags &= ~JFLAG_XMLCONSOLE;
[a3d5766]430                /* Not necessary for now. And for now the code isn't too
431                   happy if the buddy is completely gone right after calling
432                   this function already.
[998b103]433                imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
[a3d5766]434                */
[bb95d43]435                return;
436        }
437       
[788a1af]438        /* We should always do this part. Clean up our administration a little bit. */
[0da65d5]439        jabber_buddy_remove_bare( ic, who );
[788a1af]440       
[0da65d5]441        if( jabber_remove_from_roster( ic, who ) )
442                presence_send_request( ic, who, "unsubscribe" );
[cfbb3a6]443}
444
[94acdd0]445static struct groupchat *jabber_chat_join_( struct im_connection *ic, const char *room, const char *nick, const char *password )
[e35d1a1]446{
447        if( strchr( room, '@' ) == NULL )
448                imcb_error( ic, "Invalid room name: %s", room );
[5bd21df]449        else if( jabber_chat_by_jid( ic, room ) )
[e35d1a1]450                imcb_error( ic, "Already present in chat `%s'", room );
451        else
452                return jabber_chat_join( ic, room, nick, password );
453       
454        return NULL;
455}
456
[43671b9]457static void jabber_chat_msg_( struct groupchat *c, char *message, int flags )
458{
459        if( c && message )
460                jabber_chat_msg( c, message, flags );
461}
462
[ef5c185]463static void jabber_chat_topic_( struct groupchat *c, char *topic )
464{
465        if( c && topic )
466                jabber_chat_topic( c, topic );
467}
468
[e35d1a1]469static void jabber_chat_leave_( struct groupchat *c )
470{
471        if( c )
472                jabber_chat_leave( c, NULL );
473}
474
[eded1f7]475static void jabber_chat_invite_( struct groupchat *c, char *who, char *msg )
476{
477        struct jabber_chat *jc = c->data;
478        gchar *msg_alt = NULL;
479
480        if( msg == NULL )
481                msg_alt = g_strdup_printf( "%s invited you to %s", c->ic->acc->user, jc->name );
482       
483        if( c && who )
484                jabber_chat_invite( c, who, msg ? msg : msg_alt );
485       
486        g_free( msg_alt );
487}
488
[0da65d5]489static void jabber_keepalive( struct im_connection *ic )
[deff040]490{
491        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
[0da65d5]492        jabber_write( ic, "\n", 1 );
[a214954]493       
[038d17f]494        /* This runs the garbage collection every minute, which means every packet
495           is in the cache for about a minute (which should be enough AFAIK). */
[0da65d5]496        jabber_cache_clean( ic );
[dd788bb]497}
498
[0da65d5]499static int jabber_send_typing( struct im_connection *ic, char *who, int typing )
[a21a8ac]500{
[0da65d5]501        struct jabber_data *jd = ic->proto_data;
[a21a8ac]502        struct jabber_buddy *bud;
503       
504        /* Enable typing notification related code from now. */
505        jd->flags |= JFLAG_WANT_TYPING;
506       
[0da65d5]507        if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL )
[788a1af]508        {
509                /* Sending typing notifications to unknown buddies is
510                   unsupported for now. Shouldn't be a problem, I think. */
511                return 0;
512        }
513       
514        if( bud->flags & JBFLAG_DOES_XEP85 )
[a21a8ac]515        {
516                /* We're only allowed to send this stuff if we know the other
517                   side supports it. */
518               
519                struct xt_node *node;
520                char *type;
521                int st;
522               
[df1fb67]523                if( typing & OPT_TYPING )
[a21a8ac]524                        type = "composing";
[df1fb67]525                else if( typing & OPT_THINKING )
526                        type = "paused";
527                else
528                        type = "active";
[a21a8ac]529               
530                node = xt_new_node( type, NULL, NULL );
[47d3ac4]531                xt_add_attr( node, "xmlns", XMLNS_CHATSTATES );
[a21a8ac]532                node = jabber_make_packet( "message", "chat", bud->full_jid, node );
533               
[0da65d5]534                st = jabber_write_packet( ic, node );
[a21a8ac]535                xt_free_node( node );
536               
537                return st;
538        }
539       
540        return 1;
541}
542
[0da65d5]543void jabber_initmodule()
[f06894d]544{
[deff040]545        struct prpl *ret = g_new0( struct prpl, 1 );
[f06894d]546       
547        ret->name = "jabber";
548        ret->login = jabber_login;
[0da65d5]549        ret->init = jabber_init;
550        ret->logout = jabber_logout;
[f6c963b]551        ret->buddy_msg = jabber_buddy_msg;
[dd788bb]552        ret->away_states = jabber_away_states;
553        ret->set_away = jabber_set_away;
[f06894d]554//      ret->set_info = jabber_set_info;
[038d17f]555        ret->get_info = jabber_get_info;
[cfbb3a6]556        ret->add_buddy = jabber_add_buddy;
557        ret->remove_buddy = jabber_remove_buddy;
[43671b9]558        ret->chat_msg = jabber_chat_msg_;
[ef5c185]559        ret->chat_topic = jabber_chat_topic_;
[eded1f7]560        ret->chat_invite = jabber_chat_invite_;
[e35d1a1]561        ret->chat_leave = jabber_chat_leave_;
562        ret->chat_join = jabber_chat_join_;
[deff040]563        ret->keepalive = jabber_keepalive;
[a21a8ac]564        ret->send_typing = jabber_send_typing;
[f06894d]565        ret->handle_cmp = g_strcasecmp;
[2ff2076]566        ret->transfer_request = jabber_si_transfer_request;
[f06894d]567
[deff040]568        register_protocol( ret );
[f06894d]569}
Note: See TracBrowser for help on using the repository browser.