source: protocols/jabber/jabber.c @ 82135c7

Last change on this file since 82135c7 was 9da0bbf, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-07-02T22:12:03Z

Added (and using) jabber_chat_free() for better memory management, fixed
channel name generation code in root_commands.c and fixed one memory leak
in jabber_buddy_remove_bare().

  • Property mode set to 100644
File size: 13.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_init( account_t *acc )
36{
37        set_t *s;
38       
39        s = set_add( &acc->set, "port", JABBER_PORT_DEFAULT, 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 im_connection *ic = imcb_new( 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->ic = ic;
67        ic->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                imcb_error( ic, "Incomplete account name (format it like <username@jabberserver.name>)" );
75                imc_logout( ic, FALSE );
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        imcb_log( ic, "Connecting" );
162       
163        if( set_getint( &acc->set, "port" ) < JABBER_PORT_MIN ||
164            set_getint( &acc->set, "port" ) > JABBER_PORT_MAX )
165        {
166                imcb_log( ic, "Incorrect port number, must be in the %d-%d range",
167                               JABBER_PORT_MIN, JABBER_PORT_MAX );
168                imc_logout( ic, FALSE );
169                return;
170        }
171       
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... */
175        if( set_getbool( &acc->set, "ssl" ) )
176        {
177                jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, ic );
178                jd->fd = jd->ssl ? ssl_getfd( jd->ssl ) : -1;
179        }
180        else
181        {
182                jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic );
183        }
184        g_free( srv );
185       
186        if( jd->fd == -1 )
187        {
188                imcb_error( ic, "Could not connect to server" );
189                imc_logout( ic, TRUE );
190        }
191}
192
193static void jabber_logout( struct im_connection *ic )
194{
195        struct jabber_data *jd = ic->proto_data;
196       
197        jabber_end_stream( ic );
198       
199        while( ic->groupchats )
200                jabber_chat_free( ic->groupchats );
201       
202        if( jd->r_inpa >= 0 )
203                b_event_remove( jd->r_inpa );
204        if( jd->w_inpa >= 0 )
205                b_event_remove( jd->w_inpa );
206       
207        if( jd->ssl )
208                ssl_disconnect( jd->ssl );
209        if( jd->fd >= 0 )
210                closesocket( jd->fd );
211       
212        if( jd->tx_len )
213                g_free( jd->txq );
214       
215        g_hash_table_destroy( jd->node_cache );
216       
217        xt_free( jd->xt );
218       
219        g_free( jd->away_message );
220        g_free( jd->username );
221        g_free( jd );
222}
223
224static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message, int flags )
225{
226        struct jabber_data *jd = ic->proto_data;
227        struct jabber_buddy *bud;
228        struct xt_node *node;
229        char *s;
230        int st;
231       
232        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
233                return jabber_write( ic, message, strlen( message ) );
234       
235        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_name( ic, s + 1 ) )
236                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
237        else
238                bud = jabber_buddy_by_jid( ic, who, 0 );
239       
240        node = xt_new_node( "body", message, NULL );
241        node = jabber_make_packet( "message", "chat", bud ? bud->full_jid : who, node );
242       
243        if( bud && ( jd->flags & JFLAG_WANT_TYPING ) &&
244            ( ( bud->flags & JBFLAG_DOES_XEP85 ) ||
245             !( bud->flags & JBFLAG_PROBED_XEP85 ) ) )
246        {
247                struct xt_node *act;
248               
249                /* If the user likes typing notification and if we don't know
250                   (and didn't probe before) if this resource supports XEP85,
251                   include a probe in this packet now. Also, if we know this
252                   buddy does support XEP85, we have to send this <active/>
253                   tag to tell that the user stopped typing (well, that's what
254                   we guess when s/he pressed Enter...). */
255                act = xt_new_node( "active", NULL, NULL );
256                xt_add_attr( act, "xmlns", XMLNS_CHATSTATES );
257                xt_add_child( node, act );
258               
259                /* Just make sure we do this only once. */
260                bud->flags |= JBFLAG_PROBED_XEP85;
261        }
262       
263        st = jabber_write_packet( ic, node );
264        xt_free_node( node );
265       
266        return st;
267}
268
269static GList *jabber_away_states( struct im_connection *ic )
270{
271        static GList *l = NULL;
272        int i;
273       
274        if( l == NULL )
275                for( i = 0; jabber_away_state_list[i].full_name; i ++ )
276                        l = g_list_append( l, (void*) jabber_away_state_list[i].full_name );
277       
278        return l;
279}
280
281static void jabber_get_info( struct im_connection *ic, char *who )
282{
283        struct jabber_data *jd = ic->proto_data;
284        struct jabber_buddy *bud;
285       
286        if( strchr( who, '/' ) )
287                bud = jabber_buddy_by_jid( ic, who, 0 );
288        else
289        {
290                char *s = jabber_normalize( who );
291                bud = g_hash_table_lookup( jd->buddies, s );
292                g_free( s );
293        }
294       
295        while( bud )
296        {
297                imcb_log( ic, "Buddy %s (%d) information:\nAway state: %s\nAway message: %s",
298                                   bud->full_jid, bud->priority,
299                                   bud->away_state ? bud->away_state->full_name : "(none)",
300                                   bud->away_message ? : "(none)" );
301                bud = bud->next;
302        }
303       
304        jabber_get_vcard( ic, bud ? bud->full_jid : who );
305}
306
307static void jabber_set_away( struct im_connection *ic, char *state_txt, char *message )
308{
309        struct jabber_data *jd = ic->proto_data;
310        struct jabber_away_state *state;
311       
312        /* Save all this info. We need it, for example, when changing the priority setting. */
313        state = (void *) jabber_away_state_by_name( state_txt );
314        jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */
315        g_free( jd->away_message );
316        jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL;
317       
318        presence_send_update( ic );
319}
320
321static void jabber_add_buddy( struct im_connection *ic, char *who, char *group )
322{
323        struct jabber_data *jd = ic->proto_data;
324       
325        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
326        {
327                jd->flags |= JFLAG_XMLCONSOLE;
328                imcb_add_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
329                return;
330        }
331       
332        if( jabber_add_to_roster( ic, who, NULL ) )
333                presence_send_request( ic, who, "subscribe" );
334}
335
336static void jabber_remove_buddy( struct im_connection *ic, char *who, char *group )
337{
338        struct jabber_data *jd = ic->proto_data;
339       
340        if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )
341        {
342                jd->flags &= ~JFLAG_XMLCONSOLE;
343                /* Not necessary for now. And for now the code isn't too
344                   happy if the buddy is completely gone right after calling
345                   this function already.
346                imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL );
347                */
348                return;
349        }
350       
351        /* We should always do this part. Clean up our administration a little bit. */
352        jabber_buddy_remove_bare( ic, who );
353       
354        if( jabber_remove_from_roster( ic, who ) )
355                presence_send_request( ic, who, "unsubscribe" );
356}
357
358static struct groupchat *jabber_chat_join_( struct im_connection *ic, char *room, char *nick, char *password )
359{
360        if( strchr( room, '@' ) == NULL )
361                imcb_error( ic, "Invalid room name: %s", room );
362        else if( jabber_chat_by_name( ic, room ) )
363                imcb_error( ic, "Already present in chat `%s'", room );
364        else
365                return jabber_chat_join( ic, room, nick, password );
366       
367        return NULL;
368}
369
370static void jabber_chat_msg_( struct groupchat *c, char *message, int flags )
371{
372        if( c && message )
373                jabber_chat_msg( c, message, flags );
374}
375
376static void jabber_chat_leave_( struct groupchat *c )
377{
378        if( c )
379                jabber_chat_leave( c, NULL );
380}
381
382static void jabber_keepalive( struct im_connection *ic )
383{
384        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
385        jabber_write( ic, "\n", 1 );
386       
387        /* This runs the garbage collection every minute, which means every packet
388           is in the cache for about a minute (which should be enough AFAIK). */
389        jabber_cache_clean( ic );
390}
391
392static int jabber_send_typing( struct im_connection *ic, char *who, int typing )
393{
394        struct jabber_data *jd = ic->proto_data;
395        struct jabber_buddy *bud;
396       
397        /* Enable typing notification related code from now. */
398        jd->flags |= JFLAG_WANT_TYPING;
399       
400        if( ( bud = jabber_buddy_by_jid( ic, who, 0 ) ) == NULL )
401        {
402                /* Sending typing notifications to unknown buddies is
403                   unsupported for now. Shouldn't be a problem, I think. */
404                return 0;
405        }
406       
407        if( bud->flags & JBFLAG_DOES_XEP85 )
408        {
409                /* We're only allowed to send this stuff if we know the other
410                   side supports it. */
411               
412                struct xt_node *node;
413                char *type;
414                int st;
415               
416                if( typing & OPT_TYPING )
417                        type = "composing";
418                else if( typing & OPT_THINKING )
419                        type = "paused";
420                else
421                        type = "active";
422               
423                node = xt_new_node( type, NULL, NULL );
424                xt_add_attr( node, "xmlns", XMLNS_CHATSTATES );
425                node = jabber_make_packet( "message", "chat", bud->full_jid, node );
426               
427                st = jabber_write_packet( ic, node );
428                xt_free_node( node );
429               
430                return st;
431        }
432       
433        return 1;
434}
435
436void jabber_initmodule()
437{
438        struct prpl *ret = g_new0( struct prpl, 1 );
439       
440        ret->name = "jabber";
441        ret->login = jabber_login;
442        ret->init = jabber_init;
443        ret->logout = jabber_logout;
444        ret->buddy_msg = jabber_buddy_msg;
445        ret->away_states = jabber_away_states;
446        ret->set_away = jabber_set_away;
447//      ret->set_info = jabber_set_info;
448        ret->get_info = jabber_get_info;
449        ret->add_buddy = jabber_add_buddy;
450        ret->remove_buddy = jabber_remove_buddy;
451        ret->chat_msg = jabber_chat_msg_;
452//      ret->chat_invite = jabber_chat_invite;
453        ret->chat_leave = jabber_chat_leave_;
454        ret->chat_join = jabber_chat_join_;
455        ret->keepalive = jabber_keepalive;
456        ret->send_typing = jabber_send_typing;
457        ret->handle_cmp = g_strcasecmp;
458
459        register_protocol( ret );
460}
Note: See TracBrowser for help on using the repository browser.