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 | |
---|
35 | static 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 | |
---|
56 | static 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 | jd->node_cache = xt_new_node( "cache", NULL, NULL ); |
---|
79 | |
---|
80 | if( set_getbool( &acc->set, "ssl" ) ) |
---|
81 | { |
---|
82 | jd->ssl = ssl_connect( jd->server, set_getint( &acc->set, "port" ), jabber_connected_ssl, gc ); |
---|
83 | jd->fd = ssl_getfd( jd->ssl ); |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
87 | jd->fd = proxy_connect( jd->server, set_getint( &acc->set, "port" ), jabber_connected_plain, gc ); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | static void jabber_close( struct gaim_connection *gc ) |
---|
92 | { |
---|
93 | struct jabber_data *jd = gc->proto_data; |
---|
94 | |
---|
95 | jabber_end_stream( gc ); |
---|
96 | |
---|
97 | if( jd->r_inpa >= 0 ) |
---|
98 | b_event_remove( jd->r_inpa ); |
---|
99 | if( jd->w_inpa >= 0 ) |
---|
100 | b_event_remove( jd->w_inpa ); |
---|
101 | |
---|
102 | if( jd->ssl ) |
---|
103 | ssl_disconnect( jd->ssl ); |
---|
104 | if( jd->fd >= 0 ) |
---|
105 | closesocket( jd->fd ); |
---|
106 | |
---|
107 | if( jd->tx_len ) |
---|
108 | g_free( jd->txq ); |
---|
109 | |
---|
110 | xt_free_node( jd->node_cache ); |
---|
111 | xt_free( jd->xt ); |
---|
112 | |
---|
113 | g_free( jd->away_message ); |
---|
114 | g_free( jd->username ); |
---|
115 | g_free( jd ); |
---|
116 | } |
---|
117 | |
---|
118 | static int jabber_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away ) |
---|
119 | { |
---|
120 | struct xt_node *node; |
---|
121 | int st; |
---|
122 | |
---|
123 | node = xt_new_node( "body", message, NULL ); |
---|
124 | node = jabber_make_packet( "message", "chat", who, node ); |
---|
125 | st = jabber_write_packet( gc, node ); |
---|
126 | xt_free_node( node ); |
---|
127 | |
---|
128 | return st; |
---|
129 | } |
---|
130 | |
---|
131 | static GList *jabber_away_states( struct gaim_connection *gc ) |
---|
132 | { |
---|
133 | static GList *l = NULL; |
---|
134 | int i; |
---|
135 | |
---|
136 | if( l == NULL ) |
---|
137 | for( i = 0; jabber_away_state_list[i].full_name; i ++ ) |
---|
138 | l = g_list_append( l, (void*) jabber_away_state_list[i].full_name ); |
---|
139 | |
---|
140 | return l; |
---|
141 | } |
---|
142 | |
---|
143 | static void jabber_set_away( struct gaim_connection *gc, char *state_txt, char *message ) |
---|
144 | { |
---|
145 | struct jabber_data *jd = gc->proto_data; |
---|
146 | struct jabber_away_state *state; |
---|
147 | |
---|
148 | /* Save all this info. We need it, for example, when changing the priority setting. */ |
---|
149 | state = (void *) jabber_away_state_by_name( state_txt ); |
---|
150 | jd->away_state = state ? state : (void *) jabber_away_state_list; /* Fall back to "Away" if necessary. */ |
---|
151 | g_free( jd->away_message ); |
---|
152 | jd->away_message = ( message && *message ) ? g_strdup( message ) : NULL; |
---|
153 | |
---|
154 | presence_send_update( gc ); |
---|
155 | } |
---|
156 | |
---|
157 | static void jabber_keepalive( struct gaim_connection *gc ) |
---|
158 | { |
---|
159 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
160 | jabber_write( gc, "\n", 1 ); |
---|
161 | } |
---|
162 | |
---|
163 | void jabber_init() |
---|
164 | { |
---|
165 | struct prpl *ret = g_new0( struct prpl, 1 ); |
---|
166 | |
---|
167 | ret->name = "jabber"; |
---|
168 | ret->login = jabber_login; |
---|
169 | ret->acc_init = jabber_acc_init; |
---|
170 | ret->close = jabber_close; |
---|
171 | ret->send_im = jabber_send_im; |
---|
172 | ret->away_states = jabber_away_states; |
---|
173 | // ret->get_status_string = jabber_get_status_string; |
---|
174 | ret->set_away = jabber_set_away; |
---|
175 | // ret->set_info = jabber_set_info; |
---|
176 | // ret->get_info = jabber_get_info; |
---|
177 | // ret->add_buddy = jabber_add_buddy; |
---|
178 | // ret->remove_buddy = jabber_remove_buddy; |
---|
179 | // ret->chat_send = jabber_chat_send; |
---|
180 | // ret->chat_invite = jabber_chat_invite; |
---|
181 | // ret->chat_leave = jabber_chat_leave; |
---|
182 | // ret->chat_open = jabber_chat_open; |
---|
183 | ret->keepalive = jabber_keepalive; |
---|
184 | // ret->add_permit = jabber_add_permit; |
---|
185 | // ret->rem_permit = jabber_rem_permit; |
---|
186 | // ret->add_deny = jabber_add_deny; |
---|
187 | // ret->rem_deny = jabber_rem_deny; |
---|
188 | // ret->send_typing = jabber_send_typing; |
---|
189 | ret->handle_cmp = g_strcasecmp; |
---|
190 | |
---|
191 | register_protocol( ret ); |
---|
192 | } |
---|