source: protocols/jabber/jabber.c @ 3b3cd693

Last change on this file since 3b3cd693 was 3b3cd693, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-20T19:58:09Z

Added backwards compatibility with the old "account add" syntax for Jabber
accounts. Very ugly code, so it won't stay long. ;-)

Plus checking of the ssl_connect() return value, because of course it can
return NULL too... Have to add general *_connect() error checking to
jabber_login() soon!

  • Property mode set to 100644
File size: 11.1 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
35static void jabber_acc_init( account_t *acc )
36{
37        set_t *s;
38       
39        s = set_add( &acc->set, "port", "5222", set_eval_int, acc );
40        s->flags |= ACC_SET_OFFLINE_ONLY;
41       
42        s = set_add( &acc->set, "priority", "0", set_eval_priority, acc );
43       
44        s = set_add( &acc->set, "resource", "BitlBee", NULL, acc );
45        s->flags |= ACC_SET_OFFLINE_ONLY;
46       
47        s = set_add( &acc->set, "resource_select", "priority", NULL, acc );
48       
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{
61        struct gaim_connection *gc = new_gaim_conn( acc );
62        struct jabber_data *jd = g_new0( struct jabber_data, 1 );
63        struct ns_srv_reply *srv = NULL;
64        char *connect_to, *s;
65       
66        jd->gc = gc;
67        gc->proto_data = jd;
68       
69        jd->username = g_strdup( acc->user );
70        jd->server = strchr( jd->username, '@' );
71       
72        if( jd->server == NULL )
73        {
74                hide_login_progress( gc, "Incomplete account name (format it like <username@jabberserver.name>)" );
75                signoff( gc );
76                return;
77        }
78       
79        /* So don't think of free()ing jd->server.. :-) */
80        *jd->server = 0;
81        jd->server ++;
82       
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       
149        jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free );
150        jd->buddies = g_hash_table_new( g_str_hash, g_str_equal );
151       
152        /* Figure out the hostname to connect to. */
153        if( acc->server && *acc->server )
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       
161        /* For non-SSL connections we can try to use the port # from the SRV
162           reply, but let's not do that when using SSL, SSL usually runs on
163           non-standard ports... */
164        if( set_getbool( &acc->set, "ssl" ) )
165        {
166                jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, gc );
167                jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1;
168        }
169        else
170        {
171                jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, gc );
172        }
173       
174        g_free( srv );
175}
176
177static void jabber_close( struct gaim_connection *gc )
178{
179        struct jabber_data *jd = gc->proto_data;
180       
181        jabber_end_stream( gc );
182       
183        if( jd->r_inpa >= 0 )
184                b_event_remove( jd->r_inpa );
185        if( jd->w_inpa >= 0 )
186                b_event_remove( jd->w_inpa );
187       
188        if( jd->ssl )
189                ssl_disconnect( jd->ssl );
190        if( jd->fd >= 0 )
191                closesocket( jd->fd );
192       
193        if( jd->tx_len )
194                g_free( jd->txq );
195       
196        g_hash_table_destroy( jd->node_cache );
197       
198        xt_free( jd->xt );
199       
200        g_free( jd->away_message );
201        g_free( jd->username );
202        g_free( jd );
203}
204
205static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away )
206{
207        struct jabber_data *jd = gc->proto_data;
208        struct jabber_buddy *bud;
209        struct xt_node *node;
210        int st;
211       
212        bud = jabber_buddy_by_jid( gc, who );
213       
214        node = xt_new_node( "body", message, NULL );
215        node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node );
216       
217        if( ( jd->flags & JFLAG_WANT_TYPING ) && bud &&
218            ( ( bud->flags & JBFLAG_DOES_XEP85 ) ||
219             !( bud->flags & JBFLAG_PROBED_XEP85 ) ) )
220        {
221                struct xt_node *act;
222               
223                /* If the user likes typing notification and if we don't know
224                   (and didn't probe before) if this resource supports XEP85,
225                   include a probe in this packet now. */
226                act = xt_new_node( "active", NULL, NULL );
227                xt_add_attr( act, "xmlns", "http://jabber.org/protocol/chatstates" );
228                xt_add_child( node, act );
229               
230                /* Just make sure we do this only once. */
231                bud->flags |= JBFLAG_PROBED_XEP85;
232        }
233       
234        st = jabber_write_packet( gc, node );
235        xt_free_node( node );
236       
237        return st;
238}
239
240static GList *jabber_away_states( struct gaim_connection *gc )
241{
242        static GList *l = NULL;
243        int i;
244       
245        if( l == NULL )
246                for( i = 0; jabber_away_state_list[i].full_name; i ++ )
247                        l = g_list_append( l, (void*) jabber_away_state_list[i].full_name );
248       
249        return l;
250}
251
252static void jabber_get_info( struct gaim_connection *gc, char *who )
253{
254        struct jabber_data *jd = gc->proto_data;
255        struct jabber_buddy *bud;
256       
257        if( strchr( who, '/' ) )
258                bud = jabber_buddy_by_jid( gc, who );
259        else
260                bud = g_hash_table_lookup( jd->buddies, who );
261       
262        while( bud )
263        {
264                serv_got_crap( gc, "Buddy %s/%s (%d) information:\nAway state: %s\nAway message: %s",
265                                   bud->handle, bud->resource, bud->priority,
266                                   bud->away_state ? bud->away_state->full_name : "(none)",
267                                   bud->away_message ? : "(none)" );
268                bud = bud->next;
269        }
270       
271        jabber_get_vcard( gc, bud ? bud->handle : who );
272}
273
274static void jabber_set_away( struct gaim_connection *gc, char *state_txt, char *message )
275{
276        struct jabber_data *jd = gc->proto_data;
277        struct jabber_away_state *state;
278       
279        /* Save all this info. We need it, for example, when changing the priority setting. */
280        state = (void *) jabber_away_state_by_name( state_txt );
281        jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */
282        g_free( jd->away_message );
283        jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL;
284       
285        presence_send_update( gc );
286}
287
288static void jabber_add_buddy( struct gaim_connection *gc, char *who )
289{
290        if( jabber_add_to_roster( gc, who, NULL ) )
291                presence_send_request( gc, who, "subscribe" );
292}
293
294static void jabber_remove_buddy( struct gaim_connection *gc, char *who, char *group )
295{
296        /* We should always do this part. Clean up our administration a little bit. */
297        jabber_buddy_remove_bare( gc, who );
298       
299        if( jabber_remove_from_roster( gc, who ) )
300                presence_send_request( gc, who, "unsubscribe" );
301}
302
303static void jabber_keepalive( struct gaim_connection *gc )
304{
305        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
306        jabber_write( gc, "\n", 1 );
307       
308        /* This runs the garbage collection every minute, which means every packet
309           is in the cache for about a minute (which should be enough AFAIK). */
310        jabber_cache_clean( gc );
311}
312
313static int jabber_send_typing( struct gaim_connection *gc, char *who, int typing )
314{
315        struct jabber_data *jd = gc->proto_data;
316        struct jabber_buddy *bud;
317       
318        /* Enable typing notification related code from now. */
319        jd->flags |= JFLAG_WANT_TYPING;
320       
321        if( ( bud = jabber_buddy_by_jid( gc, who ) ) == NULL )
322        {
323                /* Sending typing notifications to unknown buddies is
324                   unsupported for now. Shouldn't be a problem, I think. */
325                return 0;
326        }
327       
328        if( bud->flags & JBFLAG_DOES_XEP85 )
329        {
330                /* We're only allowed to send this stuff if we know the other
331                   side supports it. */
332               
333                struct xt_node *node;
334                char *type;
335                int st;
336               
337                if( typing == 0 )
338                        type = "active";
339                else if( typing == 2 )
340                        type = "paused";
341                else /* if( typing == 1 ) */
342                        type = "composing";
343               
344                node = xt_new_node( type, NULL, NULL );
345                xt_add_attr( node, "xmlns", "http://jabber.org/protocol/chatstates" );
346                node = jabber_make_packet( "message", "chat", bud->full_jid, node );
347               
348                st = jabber_write_packet( gc, node );
349                xt_free_node( node );
350               
351                return st;
352        }
353       
354        return 1;
355}
356
357void jabber_init()
358{
359        struct prpl *ret = g_new0( struct prpl, 1 );
360       
361        ret->name = "jabber";
362        ret->login = jabber_login;
363        ret->acc_init = jabber_acc_init;
364        ret->close = jabber_close;
365        ret->send_im = jabber_send_im;
366        ret->away_states = jabber_away_states;
367//      ret->get_status_string = jabber_get_status_string;
368        ret->set_away = jabber_set_away;
369//      ret->set_info = jabber_set_info;
370        ret->get_info = jabber_get_info;
371        ret->add_buddy = jabber_add_buddy;
372        ret->remove_buddy = jabber_remove_buddy;
373//      ret->chat_send = jabber_chat_send;
374//      ret->chat_invite = jabber_chat_invite;
375//      ret->chat_leave = jabber_chat_leave;
376//      ret->chat_open = jabber_chat_open;
377        ret->keepalive = jabber_keepalive;
378        ret->send_typing = jabber_send_typing;
379        ret->handle_cmp = g_strcasecmp;
380
381        register_protocol( ret );
382}
Note: See TracBrowser for help on using the repository browser.