source: protocols/jabber/jabber.c @ dc0ba9c

Last change on this file since dc0ba9c 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
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
37static void jabber_init( account_t *acc )
38{
39        set_t *s;
40       
41        s = set_add( &acc->set, "port", JABBER_PORT_DEFAULT, set_eval_int, acc );
42        s->flags |= ACC_SET_OFFLINE_ONLY;
43       
44        s = set_add( &acc->set, "priority", "0", set_eval_priority, acc );
45       
46        s = set_add( &acc->set, "resource", "BitlBee", NULL, acc );
47        s->flags |= ACC_SET_OFFLINE_ONLY;
48       
49        s = set_add( &acc->set, "resource_select", "priority", NULL, acc );
50       
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;
59       
60        s = set_add( &acc->set, "xmlconsole", "false", set_eval_bool, acc );
61        s->flags |= ACC_SET_OFFLINE_ONLY;
62}
63
64static void jabber_generate_id_hash( struct jabber_data *jd );
65
66static void jabber_login( account_t *acc )
67{
68        struct im_connection *ic = imcb_new( acc );
69        struct jabber_data *jd = g_new0( struct jabber_data, 1 );
70        struct ns_srv_reply *srv = NULL;
71        char *connect_to, *s;
72       
73        jd->ic = ic;
74        ic->proto_data = jd;
75       
76        jd->username = g_strdup( acc->user );
77        jd->server = strchr( jd->username, '@' );
78       
79        if( jd->server == NULL )
80        {
81                imcb_error( ic, "Incomplete account name (format it like <username@jabberserver.name>)" );
82                imc_logout( ic, FALSE );
83                return;
84        }
85       
86        /* So don't think of free()ing jd->server.. :-) */
87        *jd->server = 0;
88        jd->server ++;
89       
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       
156        jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free );
157        jd->buddies = g_hash_table_new( g_str_hash, g_str_equal );
158       
159        /* Figure out the hostname to connect to. */
160        if( acc->server && *acc->server )
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       
168        imcb_log( ic, "Connecting" );
169       
170        if( set_getint( &acc->set, "port" ) < JABBER_PORT_MIN ||
171            set_getint( &acc->set, "port" ) > JABBER_PORT_MAX )
172        {
173                imcb_log( ic, "Incorrect port number, must be in the %d-%d range",
174                               JABBER_PORT_MIN, JABBER_PORT_MAX );
175                imc_logout( ic, FALSE );
176                return;
177        }
178       
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... */
182        if( set_getbool( &acc->set, "ssl" ) )
183        {
184                jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, ic );
185                jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1;
186        }
187        else
188        {
189                jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic );
190        }
191        g_free( srv );
192       
193        if( jd->fd == -1 )
194        {
195                imcb_error( ic, "Could not connect to server" );
196                imc_logout( ic, TRUE );
197        }
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        }
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 );
228}
229
230static void jabber_logout( struct im_connection *ic )
231{
232        struct jabber_data *jd = ic->proto_data;
233       
234        jabber_end_stream( ic );
235       
236        while( ic->groupchats )
237                jabber_chat_free( ic->groupchats );
238       
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       
249        if( jd->tx_len )
250                g_free( jd->txq );
251       
252        g_hash_table_destroy( jd->node_cache );
253       
254        xt_free( jd->xt );
255       
256        g_free( jd->away_message );
257        g_free( jd->username );
258        g_free( jd );
259}
260
261static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
262{
263        struct jabber_data *jd = ic->proto_data;
264        struct jabber_buddy *bud;
265        struct xt_node *node;
266        char *s;
267        int st;
268       
269        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
270                return jabber_write( ic, message, strlen( message ) );
271       
272        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
273                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
274        else
275                bud = jabber_buddy_by_jid( ic, who, 0 );
276       
277        node = xt_new_node( "body", message, NULL );
278        node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node );
279       
280        if( bud && ( jd->flags & JFLAG_WANT_TYPING ) &&
281            ( ( bud->flags & JBFLAG_DOES_XEP85 ) ||
282             !( bud->flags & JBFLAG_PROBED_XEP85 ) ) )
283        {
284                struct xt_node *act;
285               
286                /* If the user likes typing notification and if we don't know
287                   (and didn't probe before) if this resource supports XEP85,
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...). */
292                act = xt_new_node( "active", NULL, NULL );
293                xt_add_attr( act, "xmlns", XMLNS_CHATSTATES );
294                xt_add_child( node, act );
295               
296                /* Just make sure we do this only once. */
297                bud->flags |= JBFLAG_PROBED_XEP85;
298        }
299       
300        st = jabber_write_packet( ic, node );
301        xt_free_node( node );
302       
303        return st;
304}
305
306static GList *jabber_away_states( struct im_connection *ic )
307{
308        static GList *l = NULL;
309        int i;
310       
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 );
314       
315        return l;
316}
317
318static void jabber_get_info( struct im_connection *ic, char *who )
319{
320        struct jabber_data *jd = ic->proto_data;
321        struct jabber_buddy *bud;
322       
323        if( strchr( who, '/' ) )
324                bud = jabber_buddy_by_jid( ic, who, 0 );
325        else
326        {
327                char *s = jabber_normalize( who );
328                bud = g_hash_table_lookup( jd->buddies, s );
329                g_free( s );
330        }
331       
332        while( bud )
333        {
334                imcb_log( ic, "Buddy %s (%d) information:\nAway state: %s\nAway message: %s",
335                                   bud->full_jid, bud->priority,
336                                   bud->away_state ? bud->away_state->full_name : "(none)",
337                                   bud->away_message ? : "(none)" );
338                bud = bud->next;
339        }
340       
341        jabber_get_vcard( ic, bud ? bud->full_jid : who );
342}
343
344static void jabber_set_away( struct im_connection *ic, char *state_txt, char *message )
345{
346        struct jabber_data *jd = ic->proto_data;
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       
355        presence_send_update( ic );
356}
357
358static void jabber_add_buddy( struct im_connection *ic, char *who, char *group )
359{
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       
369        if( jabber_add_to_roster( ic, who, NULL ) )
370                presence_send_request( ic, who, "subscribe" );
371}
372
373static void jabber_remove_buddy( struct im_connection *ic, char *who, char *group )
374{
375        struct jabber_data *jd = ic->proto_data;
376       
377        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
378        {
379                jd->flags &= ~JFLAG_XMLCONSOLE;
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.
383                imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
384                */
385                return;
386        }
387       
388        /* We should always do this part. Clean up our administration a little bit. */
389        jabber_buddy_remove_bare( ic, who );
390       
391        if( jabber_remove_from_roster( ic, who ) )
392                presence_send_request( ic, who, "unsubscribe" );
393}
394
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 );
399        else if( jabber_chat_by_jid( ic, room ) )
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
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
413static void jabber_chat_topic_( struct groupchat *c, char *topic )
414{
415        if( c && topic )
416                jabber_chat_topic( c, topic );
417}
418
419static void jabber_chat_leave_( struct groupchat *c )
420{
421        if( c )
422                jabber_chat_leave( c, NULL );
423}
424
425static void jabber_keepalive( struct im_connection *ic )
426{
427        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
428        jabber_write( ic, "\n", 1 );
429       
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). */
432        jabber_cache_clean( ic );
433}
434
435static int jabber_send_typing( struct im_connection *ic, char *who, int typing )
436{
437        struct jabber_data *jd = ic->proto_data;
438        struct jabber_buddy *bud;
439       
440        /* Enable typing notification related code from now. */
441        jd->flags |= JFLAG_WANT_TYPING;
442       
443        if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL )
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 )
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               
459                if( typing & OPT_TYPING )
460                        type = "composing";
461                else if( typing & OPT_THINKING )
462                        type = "paused";
463                else
464                        type = "active";
465               
466                node = xt_new_node( type, NULL, NULL );
467                xt_add_attr( node, "xmlns", XMLNS_CHATSTATES );
468                node = jabber_make_packet( "message", "chat", bud->full_jid, node );
469               
470                st = jabber_write_packet( ic, node );
471                xt_free_node( node );
472               
473                return st;
474        }
475       
476        return 1;
477}
478
479void jabber_initmodule()
480{
481        struct prpl *ret = g_new0( struct prpl, 1 );
482       
483        ret->name = "jabber";
484        ret->login = jabber_login;
485        ret->init = jabber_init;
486        ret->logout = jabber_logout;
487        ret->buddy_msg = jabber_buddy_msg;
488        ret->away_states = jabber_away_states;
489        ret->set_away = jabber_set_away;
490//      ret->set_info = jabber_set_info;
491        ret->get_info = jabber_get_info;
492        ret->add_buddy = jabber_add_buddy;
493        ret->remove_buddy = jabber_remove_buddy;
494        ret->chat_msg = jabber_chat_msg_;
495        ret->chat_topic = jabber_chat_topic_;
496//      ret->chat_invite = jabber_chat_invite;
497        ret->chat_leave = jabber_chat_leave_;
498        ret->chat_join = jabber_chat_join_;
499        ret->keepalive = jabber_keepalive;
500        ret->send_typing = jabber_send_typing;
501        ret->handle_cmp = g_strcasecmp;
502        ret->transfer_request = jabber_si_transfer_request;
503
504        register_protocol( ret );
505}
Note: See TracBrowser for help on using the repository browser.