source: protocols/jabber/jabber.c @ 08135df

Last change on this file since 08135df was 08135df, checked in by ulim <a.sporto+bee@…>, at 2007-12-04T01:08:43Z

Merged in current devel

Wilmer van der Gaast 2007-12-02 Imported setuid() patch from Simo Leone <simo@archlinux...> with some

Wilmer van der Gaast 2007-12-02 Forgot to return something in jabber_chat_join_failed().
Wilmer van der Gaast 2007-12-02 Merging a change I should've pulled before committing three other changes.
Wilmer van der Gaast 2007-12-02 Added charset checks on incoming msgs (from the IRC side) to prevent possible
Wilmer van der Gaast 2007-12-02 Handling of presence-error packets (only useful for groupchats now), moved
Wilmer van der Gaast 2007-12-02 Defining DEBUG via CFLAGS so that it'll always be there, even when a file
Wilmer van der Gaast 2007-12-02 Removed retarded printf() (ARGH) and moved the event handling handling of
Wilmer van der Gaast 2007-11-29 printf() in daemons considered harmful.
Wilmer van der Gaast 2007-11-28 Fixed the epoll+ForkDaemon combination. The libevent event handling

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