source: protocols/jabber/jabber.c @ f32c14c

Last change on this file since f32c14c was daae10f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-07T16:33:02Z

OpenSolaris (non-gcc) fixes, patches from Dagobert Michelsen <dam@…>
with some changes.

  • Property mode set to 100644
File size: 15.9 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        g_snprintf( str, sizeof( str ), "%d", jabber_port_list[0] );
63        s = set_add( &acc->set, "port", str, set_eval_int, acc );
64        s->flags |= ACC_SET_OFFLINE_ONLY;
65       
66        s = set_add( &acc->set, "priority", "0", set_eval_priority, acc );
67
68        s = set_add( &acc->set, "proxy", "<local>;<auto>", NULL, acc );
69       
70        s = set_add( &acc->set, "resource", "BitlBee", NULL, acc );
71        s->flags |= ACC_SET_OFFLINE_ONLY;
72       
73        s = set_add( &acc->set, "resource_select", "activity", NULL, acc );
74       
75        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
76        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK;
77       
78        s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc );
79        s->flags |= ACC_SET_OFFLINE_ONLY;
80       
81        s = set_add( &acc->set, "tls", "try", set_eval_tls, acc );
82        s->flags |= ACC_SET_OFFLINE_ONLY;
83       
84        s = set_add( &acc->set, "user_agent", "BitlBee", NULL, acc );
85       
86        s = set_add( &acc->set, "xmlconsole", "false", set_eval_bool, acc );
87        s->flags |= ACC_SET_OFFLINE_ONLY;
88       
89        acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE;
90}
91
92static void jabber_generate_id_hash( struct jabber_data *jd );
93
94static void jabber_login( account_t *acc )
95{
96        struct im_connection *ic = imcb_new( acc );
97        struct jabber_data *jd = g_new0( struct jabber_data, 1 );
98        struct ns_srv_reply *srv = NULL;
99        char *connect_to, *s;
100        int i;
101       
102        /* For now this is needed in the _connected() handlers if using
103           GLib event handling, to make sure we're not handling events
104           on dead connections. */
105        jabber_connections = g_slist_prepend( jabber_connections, ic );
106       
107        jd->ic = ic;
108        ic->proto_data = jd;
109       
110        jd->username = g_strdup( acc->user );
111        jd->server = strchr( jd->username, '@' );
112       
113        jd->fd = jd->r_inpa = jd->w_inpa = -1;
114       
115        if( jd->server == NULL )
116        {
117                imcb_error( ic, "Incomplete account name (format it like <username@jabberserver.name>)" );
118                imc_logout( ic, FALSE );
119                return;
120        }
121       
122        /* So don't think of free()ing jd->server.. :-) */
123        *jd->server = 0;
124        jd->server ++;
125       
126        if( ( s = strchr( jd->server, '/' ) ) )
127        {
128                *s = 0;
129                set_setstr( &acc->set, "resource", s + 1 );
130               
131                /* Also remove the /resource from the original variable so we
132                   won't have to do this again every time. */
133                s = strchr( acc->user, '/' );
134                *s = 0;
135        }
136       
137        /* This code isn't really pretty. Backwards compatibility never is... */
138        s = acc->server;
139        while( s )
140        {
141                static int had_port = 0;
142               
143                if( strncmp( s, "ssl", 3 ) == 0 )
144                {
145                        set_setstr( &acc->set, "ssl", "true" );
146                       
147                        /* Flush this part so that (if this was the first
148                           part of the server string) acc->server gets
149                           flushed. We don't want to have to do this another
150                           time. :-) */
151                        *s = 0;
152                        s ++;
153                       
154                        /* Only set this if the user didn't specify a custom
155                           port number already... */
156                        if( !had_port )
157                                set_setint( &acc->set, "port", 5223 );
158                }
159                else if( isdigit( *s ) )
160                {
161                        int i;
162                       
163                        /* The first character is a digit. It could be an
164                           IP address though. Only accept this as a port#
165                           if there are only digits. */
166                        for( i = 0; isdigit( s[i] ); i ++ );
167                       
168                        /* If the first non-digit character is a colon or
169                           the end of the string, save the port number
170                           where it should be. */
171                        if( s[i] == ':' || s[i] == 0 )
172                        {
173                                sscanf( s, "%d", &i );
174                                set_setint( &acc->set, "port", i );
175                               
176                                /* See above. */
177                                *s = 0;
178                                s ++;
179                        }
180                       
181                        had_port = 1;
182                }
183               
184                s = strchr( s, ':' );
185                if( s )
186                {
187                        *s = 0;
188                        s ++;
189                }
190        }
191       
192        jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free );
193        jd->buddies = g_hash_table_new( g_str_hash, g_str_equal );
194       
195        /* Figure out the hostname to connect to. */
196        if( acc->server && *acc->server )
197                connect_to = acc->server;
198        else if( ( srv = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
199                 ( srv = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
200                connect_to = srv->name;
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        g_free( srv );
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->away_message );
309        g_free( jd->username );
310        g_free( jd );
311       
312        jabber_connections = g_slist_remove( jabber_connections, ic );
313}
314
315static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
316{
317        struct jabber_data *jd = ic->proto_data;
318        struct jabber_buddy *bud;
319        struct xt_node *node;
320        char *s;
321        int st;
322       
323        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
324                return jabber_write( ic, message, strlen( message ) );
325       
326        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
327                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
328        else
329                bud = jabber_buddy_by_jid( ic, who, GET_BUDDY_BARE_OK );
330       
331        node = xt_new_node( "body", message, NULL );
332        node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node );
333       
334        if( bud && ( jd->flags & JFLAG_WANT_TYPING ) &&
335            ( ( bud->flags & JBFLAG_DOES_XEP85 ) ||
336             !( bud->flags & JBFLAG_PROBED_XEP85 ) ) )
337        {
338                struct xt_node *act;
339               
340                /* If the user likes typing notification and if we don't know
341                   (and didn't probe before) if this resource supports XEP85,
342                   include a probe in this packet now. Also, if we know this
343                   buddy does support XEP85, we have to send this <active/>
344                   tag to tell that the user stopped typing (well, that's what
345                   we guess when s/he pressed Enter...). */
346                act = xt_new_node( "active", NULL, NULL );
347                xt_add_attr( act, "xmlns", XMLNS_CHATSTATES );
348                xt_add_child( node, act );
349               
350                /* Just make sure we do this only once. */
351                bud->flags |= JBFLAG_PROBED_XEP85;
352        }
353       
354        st = jabber_write_packet( ic, node );
355        xt_free_node( node );
356       
357        return st;
358}
359
360static GList *jabber_away_states( struct im_connection *ic )
361{
362        static GList *l = NULL;
363        int i;
364       
365        if( l == NULL )
366                for( i = 0; jabber_away_state_list[i].full_name; i ++ )
367                        l = g_list_append( l, (void*) jabber_away_state_list[i].full_name );
368       
369        return l;
370}
371
372static void jabber_get_info( struct im_connection *ic, char *who )
373{
374        struct jabber_buddy *bud;
375       
376        bud = jabber_buddy_by_jid( ic, who, GET_BUDDY_FIRST );
377       
378        while( bud )
379        {
380                imcb_log( ic, "Buddy %s (%d) information:", bud->full_jid, bud->priority );
381                if( bud->away_state )
382                        imcb_log( ic, "Away state: %s", bud->away_state->full_name );
383                imcb_log( ic, "Status message: %s", bud->away_message ? bud->away_message : "(none)" );
384               
385                bud = bud->next;
386        }
387       
388        jabber_get_vcard( ic, bud ? bud->full_jid : who );
389}
390
391static void jabber_set_away( struct im_connection *ic, char *state_txt, char *message )
392{
393        struct jabber_data *jd = ic->proto_data;
394       
395        /* state_txt == NULL -> Not away.
396           Unknown state -> fall back to the first defined away state. */
397        if( state_txt == NULL )
398                jd->away_state = NULL;
399        else if( ( jd->away_state = jabber_away_state_by_name( state_txt ) ) == NULL )
400                jd->away_state = jabber_away_state_list;
401       
402        g_free( jd->away_message );
403        jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL;
404       
405        presence_send_update( ic );
406}
407
408static void jabber_add_buddy( struct im_connection *ic, char *who, char *group )
409{
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       
419        if( jabber_add_to_roster( ic, who, NULL, group ) )
420                presence_send_request( ic, who, "subscribe" );
421}
422
423static void jabber_remove_buddy( struct im_connection *ic, char *who, char *group )
424{
425        struct jabber_data *jd = ic->proto_data;
426       
427        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
428        {
429                jd->flags &= ~JFLAG_XMLCONSOLE;
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.
433                imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
434                */
435                return;
436        }
437       
438        /* We should always do this part. Clean up our administration a little bit. */
439        jabber_buddy_remove_bare( ic, who );
440       
441        if( jabber_remove_from_roster( ic, who ) )
442                presence_send_request( ic, who, "unsubscribe" );
443}
444
445static struct groupchat *jabber_chat_join_( struct im_connection *ic, const char *room, const char *nick, const char *password, set_t **sets )
446{
447        if( strchr( room, '@' ) == NULL )
448                imcb_error( ic, "Invalid room name: %s", room );
449        else if( jabber_chat_by_jid( ic, room ) )
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
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
463static void jabber_chat_topic_( struct groupchat *c, char *topic )
464{
465        if( c && topic )
466                jabber_chat_topic( c, topic );
467}
468
469static void jabber_chat_leave_( struct groupchat *c )
470{
471        if( c )
472                jabber_chat_leave( c, NULL );
473}
474
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
489static void jabber_keepalive( struct im_connection *ic )
490{
491        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
492        if( !jabber_write( ic, "\n", 1 ) )
493                return;
494       
495        /* This runs the garbage collection every minute, which means every packet
496           is in the cache for about a minute (which should be enough AFAIK). */
497        jabber_cache_clean( ic );
498}
499
500static int jabber_send_typing( struct im_connection *ic, char *who, int typing )
501{
502        struct jabber_data *jd = ic->proto_data;
503        struct jabber_buddy *bud;
504       
505        /* Enable typing notification related code from now. */
506        jd->flags |= JFLAG_WANT_TYPING;
507       
508        if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL )
509        {
510                /* Sending typing notifications to unknown buddies is
511                   unsupported for now. Shouldn't be a problem, I think. */
512                return 0;
513        }
514       
515        if( bud->flags & JBFLAG_DOES_XEP85 )
516        {
517                /* We're only allowed to send this stuff if we know the other
518                   side supports it. */
519               
520                struct xt_node *node;
521                char *type;
522                int st;
523               
524                if( typing & OPT_TYPING )
525                        type = "composing";
526                else if( typing & OPT_THINKING )
527                        type = "paused";
528                else
529                        type = "active";
530               
531                node = xt_new_node( type, NULL, NULL );
532                xt_add_attr( node, "xmlns", XMLNS_CHATSTATES );
533                node = jabber_make_packet( "message", "chat", bud->full_jid, node );
534               
535                st = jabber_write_packet( ic, node );
536                xt_free_node( node );
537               
538                return st;
539        }
540       
541        return 1;
542}
543
544void jabber_initmodule()
545{
546        struct prpl *ret = g_new0( struct prpl, 1 );
547       
548        ret->name = "jabber";
549        ret->login = jabber_login;
550        ret->init = jabber_init;
551        ret->logout = jabber_logout;
552        ret->buddy_msg = jabber_buddy_msg;
553        ret->away_states = jabber_away_states;
554        ret->set_away = jabber_set_away;
555//      ret->set_info = jabber_set_info;
556        ret->get_info = jabber_get_info;
557        ret->add_buddy = jabber_add_buddy;
558        ret->remove_buddy = jabber_remove_buddy;
559        ret->chat_msg = jabber_chat_msg_;
560        ret->chat_topic = jabber_chat_topic_;
561        ret->chat_invite = jabber_chat_invite_;
562        ret->chat_leave = jabber_chat_leave_;
563        ret->chat_join = jabber_chat_join_;
564        ret->keepalive = jabber_keepalive;
565        ret->send_typing = jabber_send_typing;
566        ret->handle_cmp = g_strcasecmp;
567        ret->transfer_request = jabber_si_transfer_request;
568
569        register_protocol( ret );
570}
Note: See TracBrowser for help on using the repository browser.