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