source: protocols/jabber/jabber.c @ dce3903

Last change on this file since dce3903 was 2ff2076, checked in by ulim <a.sporto+bee@…>, at 2007-12-03T14:28:45Z

Intermediate commit. Sending seems to work. TODOs:

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