source: protocols/jabber/jabber.c @ ef5c185

Last change on this file since ef5c185 was ef5c185, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-11-19T23:14:39Z

Added Jabber groupchat topic support.

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