source: protocols/jabber/jabber.c @ c058ff9

Last change on this file since c058ff9 was c058ff9, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-12-09T23:19:35Z

Added /invite support for Jabber chatrooms (and fixed the argument order
to chat_invite).

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