source: protocols/jabber/jabber.c @ 36e9f62

Last change on this file since 36e9f62 was 36e9f62, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-07T17:46:28Z

Added SRV lookups to automatically find out the correct server for a
domain.

  • Property mode set to 100644
File size: 8.2 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, "server", NULL, set_eval_account, acc );
48        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
49       
50        s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc );
51        s->flags |= ACC_SET_OFFLINE_ONLY;
52       
53        s = set_add( &acc->set, "tls", "try", set_eval_tls, acc );
54        s->flags |= ACC_SET_OFFLINE_ONLY;
55}
56
57static void jabber_login( account_t *acc )
58{
59        struct gaim_connection *gc = new_gaim_conn( acc );
60        struct jabber_data *jd = g_new0( struct jabber_data, 1 );
61        struct ns_srv_reply *srv = NULL;
62        char *connect_to;
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        /* Figure out the hostname to connect to. */
84        if( acc->server )
85                connect_to = acc->server;
86        else if( ( srv = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
87                 ( srv = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
88                connect_to = srv->name;
89        else
90                connect_to = jd->server;
91       
92        /* For non-SSL connections we can try to use the port # from the SRV
93           reply, but let's not do that when using SSL, SSL usually runs on
94           non-standard ports... */
95        if( set_getbool( &acc->set, "ssl" ) )
96        {
97                jd->ssl = ssl_connect( connect_to, set_getint( &acc->set, "port" ), jabber_connected_ssl, gc );
98                jd->fd = ssl_getfd( jd->ssl );
99        }
100        else
101        {
102                jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, gc );
103        }
104       
105        g_free( srv );
106}
107
108static void jabber_close( struct gaim_connection *gc )
109{
110        struct jabber_data *jd = gc->proto_data;
111       
112        jabber_end_stream( gc );
113       
114        if( jd->r_inpa >= 0 )
115                b_event_remove( jd->r_inpa );
116        if( jd->w_inpa >= 0 )
117                b_event_remove( jd->w_inpa );
118       
119        if( jd->ssl )
120                ssl_disconnect( jd->ssl );
121        if( jd->fd >= 0 )
122                closesocket( jd->fd );
123       
124        if( jd->tx_len )
125                g_free( jd->txq );
126       
127        xt_free_node( jd->node_cache );
128        xt_free( jd->xt );
129       
130        g_free( jd->away_message );
131        g_free( jd->username );
132        g_free( jd );
133}
134
135static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away )
136{
137        struct xt_node *node;
138        int st;
139       
140        /*
141        event = xt_new_node( "active", NULL, NULL );
142        xt_add_attr( event, "xlmns", "http://jabber.org/protocol/chatstates" );
143       
144        event = xt_new_node( "x", NULL, xt_new_node( "composing", NULL, NULL ) );
145        xt_add_attr( event, "xmlns", "jabber:x:event" );
146        */
147       
148        node = xt_new_node( "body", message, NULL );
149        node = jabber_make_packet( "message", "chat", who, node );
150        st = jabber_write_packet( gc, node );
151        xt_free_node( node );
152       
153        return st;
154}
155
156static GList *jabber_away_states( struct gaim_connection *gc )
157{
158        static GList *l = NULL;
159        int i;
160       
161        if( l == NULL )
162                for( i = 0; jabber_away_state_list[i].full_name; i ++ )
163                        l = g_list_append( l, (void*) jabber_away_state_list[i].full_name );
164       
165        return l;
166}
167
168static void jabber_set_away( struct gaim_connection *gc, char *state_txt, char *message )
169{
170        struct jabber_data *jd = gc->proto_data;
171        struct jabber_away_state *state;
172       
173        /* Save all this info. We need it, for example, when changing the priority setting. */
174        state = (void *) jabber_away_state_by_name( state_txt );
175        jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */
176        g_free( jd->away_message );
177        jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL;
178       
179        presence_send_update( gc );
180}
181
182static void jabber_add_buddy( struct gaim_connection *gc, char *who )
183{
184        if( jabber_add_to_roster( gc, who, NULL ) )
185                presence_send_request( gc, who, "subscribe" );
186}
187
188static void jabber_remove_buddy( struct gaim_connection *gc, char *who, char *group )
189{
190        if( jabber_remove_from_roster( gc, who ) )
191                presence_send_request( gc, who, "unsubscribe" );
192}
193
194static void jabber_keepalive( struct gaim_connection *gc )
195{
196        struct jabber_data *jd = gc->proto_data;
197        struct xt_node *c, *tmp;
198       
199        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
200        jabber_write( gc, "\n", 1 );
201       
202        /* Let's abuse this keepalive for garbage collection of the node cache too.
203           It runs every minute, so let's mark every node with a special flag the
204           first time we see it, and clean it up the second time (clean up all
205           packets with the flag set).
206           
207           node->flags is normally only used by xmltree itself for parsing/handling,
208           so it should be safe to use the variable for gc. */
209       
210        /* This horrible loop is explained in xmltree.c. Makes me wonder if maybe I
211           didn't choose the perfect data structure... */
212        for( c = jd->node_cache->children; c; c =  c->next )
213                if( !( c->flags & XT_SEEN ) )
214                        break;
215       
216        /* Now c points at the first unflagged node (or at NULL). Clean up
217           everything until that point. */
218        while( jd->node_cache->children != c )
219        {
220                /*
221                printf( "Cleaning up:\n" );
222                xt_print( jd->node_cache->children );
223                */
224               
225                tmp = jd->node_cache->children->next;
226                xt_free_node( jd->node_cache->children );
227                jd->node_cache->children = tmp;
228        }
229       
230        /* Now flag the ones that were still unflagged. */
231        for( c = jd->node_cache->children; c; c = c->next )
232        {
233                /*
234                printf( "Flagged:\n" );
235                xt_print( c );
236                */
237               
238                c->flags |= XT_SEEN;
239        }
240}
241
242void jabber_init()
243{
244        struct prpl *ret = g_new0( struct prpl, 1 );
245       
246        ret->name = "jabber";
247        ret->login = jabber_login;
248        ret->acc_init = jabber_acc_init;
249        ret->close = jabber_close;
250        ret->send_im = jabber_send_im;
251        ret->away_states = jabber_away_states;
252//      ret->get_status_string = jabber_get_status_string;
253        ret->set_away = jabber_set_away;
254//      ret->set_info = jabber_set_info;
255//      ret->get_info = jabber_get_info;
256        ret->add_buddy = jabber_add_buddy;
257        ret->remove_buddy = jabber_remove_buddy;
258//      ret->chat_send = jabber_chat_send;
259//      ret->chat_invite = jabber_chat_invite;
260//      ret->chat_leave = jabber_chat_leave;
261//      ret->chat_open = jabber_chat_open;
262        ret->keepalive = jabber_keepalive;
263//      ret->send_typing = jabber_send_typing;
264        ret->handle_cmp = g_strcasecmp;
265
266        register_protocol( ret );
267}
Note: See TracBrowser for help on using the repository browser.