source: protocols/jabber/jabber.c @ 8d74291

Last change on this file since 8d74291 was deff040, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-09-21T19:48:17Z

Implemented set_away() (VERY simple version, have to add an away state
table like in the MSN module), added sending of keepalive "packets" and
removed old main() code (for testing only) from jabber.c.

  • Property mode set to 100644
File size: 5.6 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_resprio, acc );
43       
44        s = set_add( &acc->set, "resource", "BitlBee", set_eval_resprio, acc );
45       
46        s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
47        s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY;
48       
49        s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc );
50        s->flags |= ACC_SET_OFFLINE_ONLY;
51       
52        s = set_add( &acc->set, "tls", "try", set_eval_tls, acc );
53        s->flags |= ACC_SET_OFFLINE_ONLY;
54}
55
56static void jabber_login( account_t *acc )
57{
58        struct gaim_connection *gc = new_gaim_conn( acc );
59        struct jabber_data *jd = g_new0( struct jabber_data, 1 );
60       
61        jd->gc = gc;
62        gc->proto_data = jd;
63       
64        jd->username = g_strdup( acc->user );
65        jd->server = strchr( jd->username, '@' );
66       
67        if( jd->server == NULL )
68        {
69                hide_login_progress( gc, "Incomplete account name (format it like <username@jabberserver.name>)" );
70                signoff( gc );
71                return;
72        }
73       
74        /* So don't think of free()ing jd->server.. :-) */
75        *jd->server = 0;
76        jd->server ++;
77       
78        if( set_getbool( &acc->set, "ssl" ) )
79        {
80                signoff( gc );
81                /* TODO! */
82        }
83        else
84        {
85                jd->fd = proxy_connect( jd->server, set_getint( &acc->set, "port" ), jabber_connected_plain, gc );
86        }
87}
88
89static void jabber_close( struct gaim_connection *gc )
90{
91        struct jabber_data *jd = gc->proto_data;
92       
93        jabber_end_stream( gc );
94       
95        if( jd->r_inpa >= 0 )
96                b_event_remove( jd->r_inpa );
97        if( jd->w_inpa >= 0 )
98                b_event_remove( jd->w_inpa );
99       
100        if( jd->ssl )
101                ssl_disconnect( jd->ssl );
102        if( jd->fd >= 0 )
103                closesocket( jd->fd );
104       
105        xt_free( jd->xt );
106       
107        g_free( jd->username );
108        g_free( jd );
109}
110
111static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away )
112{
113        struct xt_node *node;
114        int st;
115       
116        node = xt_new_node( "body", message, NULL );
117        node = jabber_make_packet( "message", "chat", who, node );
118        st = jabber_write_packet( gc, node );
119        xt_free_node( node );
120       
121        return st;
122}
123
124/* TODO: For away state handling, implement some list like the one for MSN. */
125static GList *jabber_away_states( struct gaim_connection *gc )
126{
127        GList *l = NULL;
128       
129        l = g_list_append( l, (void*) "Online" );
130        l = g_list_append( l, (void*) "Away" );
131        l = g_list_append( l, (void*) "Extended Away" );
132        l = g_list_append( l, (void*) "Do Not Disturb" );
133       
134        return( l );
135}
136
137static void jabber_set_away( struct gaim_connection *gc, char *state, char *message )
138{
139        /* For now let's just always set state to "away" and send the message, if available. */
140        presence_send( gc, NULL, g_strcasecmp( state, "Online" ) == 0 ? NULL : "away", message );
141}
142
143static void jabber_keepalive( struct gaim_connection *gc )
144{
145        /* Just any whitespace character is enough as a keepalive for XMPP sessions. */
146        jabber_write( gc, "\n", 1 );
147}
148
149void jabber_init()
150{
151        struct prpl *ret = g_new0( struct prpl, 1 );
152       
153        ret->name = "jabber";
154        ret->login = jabber_login;
155        ret->acc_init = jabber_acc_init;
156        ret->close = jabber_close;
157        ret->send_im = jabber_send_im;
158        ret->away_states = jabber_away_states;
159//      ret->get_status_string = jabber_get_status_string;
160        ret->set_away = jabber_set_away;
161//      ret->set_info = jabber_set_info;
162//      ret->get_info = jabber_get_info;
163//      ret->add_buddy = jabber_add_buddy;
164//      ret->remove_buddy = jabber_remove_buddy;
165//      ret->chat_send = jabber_chat_send;
166//      ret->chat_invite = jabber_chat_invite;
167//      ret->chat_leave = jabber_chat_leave;
168//      ret->chat_open = jabber_chat_open;
169        ret->keepalive = jabber_keepalive;
170//      ret->add_permit = jabber_add_permit;
171//      ret->rem_permit = jabber_rem_permit;
172//      ret->add_deny = jabber_add_deny;
173//      ret->rem_deny = jabber_rem_deny;
174//      ret->send_typing = jabber_send_typing;
175        ret->handle_cmp = g_strcasecmp;
176
177        register_protocol( ret );
178}
Note: See TracBrowser for help on using the repository browser.