source: protocols/jabber/jabber.c @ 998b103

Last change on this file since 998b103 was 998b103, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-06-13T23:31:39Z

Added imcb_remove_buddy() so deletions in Jabber roster pushes actually
work. This also solves the issue of underscores appearing and disappearing
in their nicknames when people leave/join a chat.

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