source: protocols/jabber/jabber.c @ 995913b

Last change on this file since 995913b was 995913b, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-02T17:46:57Z

Added some error handling for the (not very complete yet) privacy list code.

  • Property mode set to 100644
File size: 8.8 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, "privacy_list", NULL, set_eval_privacy_list, 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, "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       
64        jd->gc = gc;
65        gc->proto_data = jd;
66       
67        jd->username = g_strdup( acc->user );
68        jd->server = strchr( jd->username, '@' );
69       
70        if( jd->server == NULL )
71        {
72                hide_login_progress( gc, "Incomplete account name (format it like <username@jabberserver.name>)" );
73                signoff( gc );
74                return;
75        }
76       
77        /* So don't think of free()ing jd->server.. :-) */
78        *jd->server = 0;
79        jd->server ++;
80       
81        jd->node_cache = xt_new_node( "cache", NULL, NULL );
82       
83        if( set_getbool( &acc->set, "ssl" ) )
84        {
85                jd->ssl = ssl_connect( acc->server ? acc->server : jd->server, set_getint( &acc->set, "port" ), jabber_connected_ssl, gc );
86                jd->fd = ssl_getfd( jd->ssl );
87        }
88        else
89        {
90                jd->fd = proxy_connect( acc->server ? acc->server : jd->server, set_getint( &acc->set, "port" ), jabber_connected_plain, gc );
91        }
92}
93
94static void jabber_close( struct gaim_connection *gc )
95{
96        struct jabber_data *jd = gc->proto_data;
97       
98        jabber_end_stream( gc );
99       
100        if( jd->r_inpa >= 0 )
101                b_event_remove( jd->r_inpa );
102        if( jd->w_inpa >= 0 )
103                b_event_remove( jd->w_inpa );
104       
105        if( jd->ssl )
106                ssl_disconnect( jd->ssl );
107        if( jd->fd >= 0 )
108                closesocket( jd->fd );
109       
110        if( jd->tx_len )
111                g_free( jd->txq );
112       
113        xt_free_node( jd->privacy_list );
114        g_free( jd->privacy_active );
115       
116        xt_free_node( jd->node_cache );
117        xt_free( jd->xt );
118       
119        g_free( jd->away_message );
120        g_free( jd->username );
121        g_free( jd );
122}
123
124static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away )
125{
126        struct xt_node *node, *event;
127        int st;
128       
129        /*
130        event = xt_new_node( "active", NULL, NULL );
131        xt_add_attr( event, "xlmns", "http://jabber.org/protocol/chatstates" );
132       
133        event = xt_new_node( "x", NULL, xt_new_node( "composing", NULL, NULL ) );
134        xt_add_attr( event, "xmlns", "jabber:x:event" );
135        */
136       
137        node = xt_new_node( "body", message, NULL );
138        node = jabber_make_packet( "message", "chat", who, node );
139        st = jabber_write_packet( gc, node );
140        xt_free_node( node );
141       
142        return st;
143}
144
145static GList *jabber_away_states( struct gaim_connection *gc )
146{
147        static GList *l = NULL;
148        int i;
149       
150        if( l == NULL )
151                for( i = 0; jabber_away_state_list[i].full_name; i ++ )
152                        l = g_list_append( l, (void*) jabber_away_state_list[i].full_name );
153       
154        return l;
155}
156
157static void jabber_set_away( struct gaim_connection *gc, char *state_txt, char *message )
158{
159        struct jabber_data *jd = gc->proto_data;
160        struct jabber_away_state *state;
161       
162        /* Save all this info. We need it, for example, when changing the priority setting. */
163        state = (void *) jabber_away_state_by_name( state_txt );
164        jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */
165        g_free( jd->away_message );
166        jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL;
167       
168        presence_send_update( gc );
169}
170
171static void jabber_add_buddy( struct gaim_connection *gc, char *who )
172{
173        if( jabber_add_to_roster( gc, who, NULL ) )
174                presence_send_request( gc, who, "subscribe" );
175}
176
177static void jabber_remove_buddy( struct gaim_connection *gc, char *who, char *group )
178{
179        if( jabber_remove_from_roster( gc, who ) )
180                presence_send_request( gc, who, "unsubscribe" );
181}
182
183static void jabber_keepalive( struct gaim_connection *gc )
184{
185        struct jabber_data *jd = gc->proto_data;
186        struct xt_node *c, *tmp;
187       
188        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
189        jabber_write( gc, "\n", 1 );
190       
191        /* Let's abuse this keepalive for garbage collection of the node cache too.
192           It runs every minute, so let's mark every node with a special flag the
193           first time we see it, and clean it up the second time (clean up all
194           packets with the flag set).
195           
196           node->flags is normally only used by xmltree itself for parsing/handling,
197           so it should be safe to use the variable for gc. */
198       
199        /* This horrible loop is explained in xmltree.c. Makes me wonder if maybe I
200           didn't choose the perfect data structure... */
201        for( c = jd->node_cache->children; c; c =  c->next )
202                if( !( c->flags & XT_SEEN ) )
203                        break;
204       
205        /* Now c points at the first unflagged node (or at NULL). Clean up
206           everything until that point. */
207        while( jd->node_cache->children != c )
208        {
209                /*
210                printf( "Cleaning up:\n" );
211                xt_print( jd->node_cache->children );
212                */
213               
214                tmp = jd->node_cache->children->next;
215                xt_free_node( jd->node_cache->children );
216                jd->node_cache->children = tmp;
217        }
218       
219        /* Now flag the ones that were still unflagged. */
220        for( c = jd->node_cache->children; c; c = c->next )
221        {
222                /*
223                printf( "Flagged:\n" );
224                xt_print( c );
225                */
226               
227                c->flags |= XT_SEEN;
228        }
229}
230
231static void jabber_add_permit( struct gaim_connection *gc, char *who )
232{
233        struct jabber_data *jd = gc->proto_data;
234       
235        if( jd->flags & JFLAG_PRIVACY_BROKEN )
236        {
237                serv_got_crap( gc, "Privacy lists not supported by this server" );
238                return;
239        }
240}
241
242static void jabber_rem_permit( struct gaim_connection *gc, char *who )
243{
244        struct jabber_data *jd = gc->proto_data;
245       
246        if( jd->flags & JFLAG_PRIVACY_BROKEN )
247        {
248                serv_got_crap( gc, "Privacy lists not supported by this server" );
249                return;
250        }
251}
252
253/* XMPP doesn't have both a block- and and allow-list, so these two functions
254   will be no-ops: */
255static void jabber_add_deny( struct gaim_connection *gc, char *who )
256{
257}
258
259static void jabber_rem_deny( struct gaim_connection *gc, char *who )
260{
261}
262
263void jabber_init()
264{
265        struct prpl *ret = g_new0( struct prpl, 1 );
266       
267        ret->name = "jabber";
268        ret->login = jabber_login;
269        ret->acc_init = jabber_acc_init;
270        ret->close = jabber_close;
271        ret->send_im = jabber_send_im;
272        ret->away_states = jabber_away_states;
273//      ret->get_status_string = jabber_get_status_string;
274        ret->set_away = jabber_set_away;
275//      ret->set_info = jabber_set_info;
276//      ret->get_info = jabber_get_info;
277        ret->add_buddy = jabber_add_buddy;
278        ret->remove_buddy = jabber_remove_buddy;
279//      ret->chat_send = jabber_chat_send;
280//      ret->chat_invite = jabber_chat_invite;
281//      ret->chat_leave = jabber_chat_leave;
282//      ret->chat_open = jabber_chat_open;
283        ret->keepalive = jabber_keepalive;
284        ret->add_permit = jabber_add_permit;
285        ret->rem_permit = jabber_rem_permit;
286        ret->add_deny = jabber_add_deny;
287        ret->rem_deny = jabber_rem_deny;
288//      ret->send_typing = jabber_send_typing;
289        ret->handle_cmp = g_strcasecmp;
290
291        register_protocol( ret );
292}
Note: See TracBrowser for help on using the repository browser.