1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Main file * |
---|
5 | * * |
---|
6 | * Copyright 2006-2013 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 | #include "oauth.h" |
---|
35 | #include "md5.h" |
---|
36 | |
---|
37 | GSList *jabber_connections; |
---|
38 | |
---|
39 | /* First enty is the default */ |
---|
40 | static const int jabber_port_list[] = { |
---|
41 | 5222, |
---|
42 | 5223, |
---|
43 | 5220, |
---|
44 | 5221, |
---|
45 | 5224, |
---|
46 | 5225, |
---|
47 | 5226, |
---|
48 | 5227, |
---|
49 | 5228, |
---|
50 | 5229, |
---|
51 | 80, |
---|
52 | 443, |
---|
53 | 0 |
---|
54 | }; |
---|
55 | |
---|
56 | static void jabber_init(account_t *acc) |
---|
57 | { |
---|
58 | set_t *s; |
---|
59 | char str[16]; |
---|
60 | |
---|
61 | s = set_add(&acc->set, "activity_timeout", "600", set_eval_int, acc); |
---|
62 | |
---|
63 | s = set_add(&acc->set, "display_name", NULL, NULL, acc); |
---|
64 | |
---|
65 | g_snprintf(str, sizeof(str), "%d", jabber_port_list[0]); |
---|
66 | s = set_add(&acc->set, "port", str, set_eval_int, acc); |
---|
67 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
68 | |
---|
69 | s = set_add(&acc->set, "priority", "0", set_eval_priority, acc); |
---|
70 | |
---|
71 | s = set_add(&acc->set, "proxy", "<local>;<auto>", NULL, acc); |
---|
72 | |
---|
73 | s = set_add(&acc->set, "resource", "BitlBee", NULL, acc); |
---|
74 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
75 | |
---|
76 | s = set_add(&acc->set, "resource_select", "activity", NULL, acc); |
---|
77 | |
---|
78 | s = set_add(&acc->set, "sasl", "true", set_eval_bool, acc); |
---|
79 | s->flags |= ACC_SET_OFFLINE_ONLY | SET_HIDDEN_DEFAULT; |
---|
80 | |
---|
81 | s = set_add(&acc->set, "server", NULL, set_eval_account, acc); |
---|
82 | s->flags |= SET_NOSAVE | ACC_SET_OFFLINE_ONLY | SET_NULL_OK; |
---|
83 | |
---|
84 | if (strcmp(acc->prpl->name, "hipchat") == 0) { |
---|
85 | set_setstr(&acc->set, "server", "chat.hipchat.com"); |
---|
86 | } else { |
---|
87 | set_add(&acc->set, "oauth", "false", set_eval_oauth, acc); |
---|
88 | |
---|
89 | /* this reuses set_eval_oauth, which clears the password */ |
---|
90 | set_add(&acc->set, "anonymous", "false", set_eval_oauth, acc); |
---|
91 | } |
---|
92 | |
---|
93 | s = set_add(&acc->set, "ssl", "false", set_eval_bool, acc); |
---|
94 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
95 | |
---|
96 | s = set_add(&acc->set, "tls", "true", set_eval_tls, acc); |
---|
97 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
98 | |
---|
99 | s = set_add(&acc->set, "tls_verify", "true", set_eval_bool, acc); |
---|
100 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
101 | |
---|
102 | s = set_add(&acc->set, "user_agent", "BitlBee", NULL, acc); |
---|
103 | |
---|
104 | s = set_add(&acc->set, "xmlconsole", "false", set_eval_bool, acc); |
---|
105 | |
---|
106 | s = set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc); |
---|
107 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
108 | |
---|
109 | /* changing this is rarely needed so keeping it secret */ |
---|
110 | s = set_add(&acc->set, "mail_notifications_limit", "5", set_eval_int, acc); |
---|
111 | s->flags |= SET_HIDDEN_DEFAULT; |
---|
112 | |
---|
113 | s = set_add(&acc->set, "mail_notifications_handle", NULL, NULL, acc); |
---|
114 | s->flags |= ACC_SET_OFFLINE_ONLY | SET_NULL_OK; |
---|
115 | |
---|
116 | acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE | |
---|
117 | ACC_FLAG_HANDLE_DOMAINS; |
---|
118 | } |
---|
119 | |
---|
120 | static void jabber_generate_id_hash(struct jabber_data *jd); |
---|
121 | |
---|
122 | static void jabber_login(account_t *acc) |
---|
123 | { |
---|
124 | struct im_connection *ic = imcb_new(acc); |
---|
125 | struct jabber_data *jd = g_new0(struct jabber_data, 1); |
---|
126 | char *s; |
---|
127 | |
---|
128 | /* For now this is needed in the _connected() handlers if using |
---|
129 | GLib event handling, to make sure we're not handling events |
---|
130 | on dead connections. */ |
---|
131 | jabber_connections = g_slist_prepend(jabber_connections, ic); |
---|
132 | |
---|
133 | jd->ic = ic; |
---|
134 | ic->proto_data = jd; |
---|
135 | |
---|
136 | jabber_set_me(ic, acc->user); |
---|
137 | |
---|
138 | jd->fd = jd->r_inpa = jd->w_inpa = -1; |
---|
139 | |
---|
140 | if (strcmp(acc->prpl->name, "hipchat") == 0) { |
---|
141 | jd->flags |= JFLAG_HIPCHAT; |
---|
142 | } |
---|
143 | |
---|
144 | if (jd->server == NULL) { |
---|
145 | imcb_error(ic, "Incomplete account name (format it like <username@jabberserver.name>)"); |
---|
146 | imc_logout(ic, FALSE); |
---|
147 | return; |
---|
148 | } |
---|
149 | |
---|
150 | if ((s = strchr(jd->server, '/'))) { |
---|
151 | *s = 0; |
---|
152 | set_setstr(&acc->set, "resource", s + 1); |
---|
153 | |
---|
154 | /* Also remove the /resource from the original variable so we |
---|
155 | won't have to do this again every time. */ |
---|
156 | s = strchr(acc->user, '/'); |
---|
157 | *s = 0; |
---|
158 | } |
---|
159 | |
---|
160 | jd->node_cache = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, jabber_cache_entry_free); |
---|
161 | jd->buddies = g_hash_table_new(g_str_hash, g_str_equal); |
---|
162 | |
---|
163 | if (set_getbool(&acc->set, "oauth")) { |
---|
164 | GSList *p_in = NULL; |
---|
165 | const char *tok; |
---|
166 | |
---|
167 | jd->fd = jd->r_inpa = jd->w_inpa = -1; |
---|
168 | |
---|
169 | if (strstr(jd->server, ".facebook.com")) { |
---|
170 | jd->oauth2_service = &oauth2_service_facebook; |
---|
171 | } else { |
---|
172 | jd->oauth2_service = &oauth2_service_google; |
---|
173 | } |
---|
174 | |
---|
175 | oauth_params_parse(&p_in, ic->acc->pass); |
---|
176 | |
---|
177 | /* First see if we have a refresh token, in which case any |
---|
178 | access token we *might* have has probably expired already |
---|
179 | anyway. */ |
---|
180 | if ((tok = oauth_params_get(&p_in, "refresh_token"))) { |
---|
181 | sasl_oauth2_refresh(ic, tok); |
---|
182 | } |
---|
183 | /* If we don't have a refresh token, let's hope the access |
---|
184 | token is still usable. */ |
---|
185 | else if ((tok = oauth_params_get(&p_in, "access_token"))) { |
---|
186 | jd->oauth2_access_token = g_strdup(tok); |
---|
187 | jabber_connect(ic); |
---|
188 | } |
---|
189 | /* If we don't have any, start the OAuth process now. Don't |
---|
190 | even open an XMPP connection yet. */ |
---|
191 | else { |
---|
192 | sasl_oauth2_init(ic); |
---|
193 | ic->flags |= OPT_SLOW_LOGIN; |
---|
194 | } |
---|
195 | |
---|
196 | oauth_params_free(&p_in); |
---|
197 | } else { |
---|
198 | jabber_connect(ic); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | /* Separate this from jabber_login() so we can do OAuth first if necessary. |
---|
203 | Putting this in io.c would probably be more correct. */ |
---|
204 | void jabber_connect(struct im_connection *ic) |
---|
205 | { |
---|
206 | account_t *acc = ic->acc; |
---|
207 | struct jabber_data *jd = ic->proto_data; |
---|
208 | int i; |
---|
209 | char *connect_to; |
---|
210 | struct ns_srv_reply **srvl = NULL, *srv = NULL; |
---|
211 | |
---|
212 | /* Figure out the hostname to connect to. */ |
---|
213 | if (acc->server && *acc->server) { |
---|
214 | connect_to = acc->server; |
---|
215 | } else if ((srvl = srv_lookup("xmpp-client", "tcp", jd->server)) || |
---|
216 | (srvl = srv_lookup("jabber-client", "tcp", jd->server))) { |
---|
217 | /* Find the lowest-priority one. These usually come |
---|
218 | back in random/shuffled order. Not looking at |
---|
219 | weights etc for now. */ |
---|
220 | srv = *srvl; |
---|
221 | for (i = 1; srvl[i]; i++) { |
---|
222 | if (srvl[i]->prio < srv->prio) { |
---|
223 | srv = srvl[i]; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | connect_to = srv->name; |
---|
228 | } else { |
---|
229 | connect_to = jd->server; |
---|
230 | } |
---|
231 | |
---|
232 | imcb_log(ic, "Connecting"); |
---|
233 | |
---|
234 | for (i = 0; jabber_port_list[i] > 0; i++) { |
---|
235 | if (set_getint(&acc->set, "port") == jabber_port_list[i]) { |
---|
236 | break; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | if (jabber_port_list[i] == 0) { |
---|
241 | imcb_log(ic, "Illegal port number"); |
---|
242 | imc_logout(ic, FALSE); |
---|
243 | return; |
---|
244 | } |
---|
245 | |
---|
246 | /* For non-SSL connections we can try to use the port # from the SRV |
---|
247 | reply, but let's not do that when using SSL, SSL usually runs on |
---|
248 | non-standard ports... */ |
---|
249 | if (set_getbool(&acc->set, "ssl")) { |
---|
250 | jd->ssl = ssl_connect(connect_to, set_getint(&acc->set, "port"), set_getbool(&acc->set, |
---|
251 | "tls_verify"), jabber_connected_ssl, |
---|
252 | ic); |
---|
253 | jd->fd = jd->ssl ? ssl_getfd(jd->ssl) : -1; |
---|
254 | } else { |
---|
255 | jd->fd = proxy_connect(connect_to, srv ? srv->port : set_getint(&acc->set, |
---|
256 | "port"), jabber_connected_plain, ic); |
---|
257 | } |
---|
258 | srv_free(srvl); |
---|
259 | |
---|
260 | if (jd->fd == -1) { |
---|
261 | imcb_error(ic, "Could not connect to server"); |
---|
262 | imc_logout(ic, TRUE); |
---|
263 | |
---|
264 | return; |
---|
265 | } |
---|
266 | |
---|
267 | if (set_getbool(&acc->set, "xmlconsole")) { |
---|
268 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
269 | /* Shouldn't really do this at this stage already, maybe. But |
---|
270 | I think this shouldn't break anything. */ |
---|
271 | imcb_add_buddy(ic, JABBER_XMLCONSOLE_HANDLE, NULL); |
---|
272 | } |
---|
273 | |
---|
274 | if (set_getbool(&acc->set, "mail_notifications")) { |
---|
275 | /* It's gmail specific, but it checks for server support before enabling it */ |
---|
276 | jd->flags |= JFLAG_GMAILNOTIFY; |
---|
277 | if (set_getstr(&acc->set, "mail_notifications_handle")) { |
---|
278 | imcb_add_buddy(ic, set_getstr(&acc->set, "mail_notifications_handle"), NULL); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | jabber_generate_id_hash(jd); |
---|
283 | } |
---|
284 | |
---|
285 | /* This generates an unfinished md5_state_t variable. Every time we generate |
---|
286 | an ID, we finish the state by adding a sequence number and take the hash. */ |
---|
287 | static void jabber_generate_id_hash(struct jabber_data *jd) |
---|
288 | { |
---|
289 | md5_byte_t binbuf[4]; |
---|
290 | char *s; |
---|
291 | |
---|
292 | md5_init(&jd->cached_id_prefix); |
---|
293 | md5_append(&jd->cached_id_prefix, (unsigned char *) jd->username, strlen(jd->username)); |
---|
294 | md5_append(&jd->cached_id_prefix, (unsigned char *) jd->server, strlen(jd->server)); |
---|
295 | s = set_getstr(&jd->ic->acc->set, "resource"); |
---|
296 | md5_append(&jd->cached_id_prefix, (unsigned char *) s, strlen(s)); |
---|
297 | random_bytes(binbuf, 4); |
---|
298 | md5_append(&jd->cached_id_prefix, binbuf, 4); |
---|
299 | } |
---|
300 | |
---|
301 | static void jabber_logout(struct im_connection *ic) |
---|
302 | { |
---|
303 | struct jabber_data *jd = ic->proto_data; |
---|
304 | |
---|
305 | while (jd->filetransfers) { |
---|
306 | imcb_file_canceled(ic, (( struct jabber_transfer *) jd->filetransfers->data)->ft, "Logging out"); |
---|
307 | } |
---|
308 | |
---|
309 | while (jd->streamhosts) { |
---|
310 | jabber_streamhost_t *sh = jd->streamhosts->data; |
---|
311 | jd->streamhosts = g_slist_remove(jd->streamhosts, sh); |
---|
312 | g_free(sh->jid); |
---|
313 | g_free(sh->host); |
---|
314 | g_free(sh); |
---|
315 | } |
---|
316 | |
---|
317 | if (jd->fd >= 0) { |
---|
318 | jabber_end_stream(ic); |
---|
319 | } |
---|
320 | |
---|
321 | while (ic->groupchats) { |
---|
322 | jabber_chat_free(ic->groupchats->data); |
---|
323 | } |
---|
324 | |
---|
325 | if (jd->r_inpa >= 0) { |
---|
326 | b_event_remove(jd->r_inpa); |
---|
327 | } |
---|
328 | if (jd->w_inpa >= 0) { |
---|
329 | b_event_remove(jd->w_inpa); |
---|
330 | } |
---|
331 | |
---|
332 | if (jd->ssl) { |
---|
333 | ssl_disconnect(jd->ssl); |
---|
334 | } |
---|
335 | if (jd->fd >= 0) { |
---|
336 | closesocket(jd->fd); |
---|
337 | } |
---|
338 | |
---|
339 | if (jd->tx_len) { |
---|
340 | g_free(jd->txq); |
---|
341 | } |
---|
342 | |
---|
343 | if (jd->node_cache) { |
---|
344 | g_hash_table_destroy(jd->node_cache); |
---|
345 | } |
---|
346 | |
---|
347 | if (jd->buddies) { |
---|
348 | jabber_buddy_remove_all(ic); |
---|
349 | } |
---|
350 | |
---|
351 | xt_free(jd->xt); |
---|
352 | |
---|
353 | md5_free(&jd->cached_id_prefix); |
---|
354 | |
---|
355 | g_free(jd->oauth2_access_token); |
---|
356 | g_free(jd->away_message); |
---|
357 | g_free(jd->internal_jid); |
---|
358 | g_free(jd->gmail_tid); |
---|
359 | g_free(jd->username); |
---|
360 | g_free(jd->me); |
---|
361 | g_free(jd); |
---|
362 | |
---|
363 | jabber_connections = g_slist_remove(jabber_connections, ic); |
---|
364 | } |
---|
365 | |
---|
366 | static int jabber_buddy_msg(struct im_connection *ic, char *who, char *message, int flags) |
---|
367 | { |
---|
368 | struct jabber_data *jd = ic->proto_data; |
---|
369 | struct jabber_buddy *bud; |
---|
370 | struct xt_node *node; |
---|
371 | char *s; |
---|
372 | int st; |
---|
373 | |
---|
374 | if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) { |
---|
375 | return jabber_write(ic, message, strlen(message)); |
---|
376 | } |
---|
377 | |
---|
378 | if (g_strcasecmp(who, JABBER_OAUTH_HANDLE) == 0 && |
---|
379 | !(jd->flags & OPT_LOGGED_IN) && jd->fd == -1) { |
---|
380 | if (sasl_oauth2_get_refresh_token(ic, message)) { |
---|
381 | return 1; |
---|
382 | } else { |
---|
383 | imcb_error(ic, "OAuth failure"); |
---|
384 | imc_logout(ic, TRUE); |
---|
385 | return 0; |
---|
386 | } |
---|
387 | } |
---|
388 | |
---|
389 | if ((s = strchr(who, '=')) && jabber_chat_by_jid(ic, s + 1)) { |
---|
390 | bud = jabber_buddy_by_ext_jid(ic, who, 0); |
---|
391 | } else { |
---|
392 | bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_BARE_OK); |
---|
393 | } |
---|
394 | |
---|
395 | node = xt_new_node("body", message, NULL); |
---|
396 | node = jabber_make_packet("message", "chat", bud ? bud->full_jid : who, node); |
---|
397 | |
---|
398 | if (bud && (jd->flags & JFLAG_WANT_TYPING) && |
---|
399 | ((bud->flags & JBFLAG_DOES_XEP85) || |
---|
400 | !(bud->flags & JBFLAG_PROBED_XEP85))) { |
---|
401 | struct xt_node *act; |
---|
402 | |
---|
403 | /* If the user likes typing notification and if we don't know |
---|
404 | (and didn't probe before) if this resource supports XEP85, |
---|
405 | include a probe in this packet now. Also, if we know this |
---|
406 | buddy does support XEP85, we have to send this <active/> |
---|
407 | tag to tell that the user stopped typing (well, that's what |
---|
408 | we guess when s/he pressed Enter...). */ |
---|
409 | act = xt_new_node("active", NULL, NULL); |
---|
410 | xt_add_attr(act, "xmlns", XMLNS_CHATSTATES); |
---|
411 | xt_add_child(node, act); |
---|
412 | |
---|
413 | /* Just make sure we do this only once. */ |
---|
414 | bud->flags |= JBFLAG_PROBED_XEP85; |
---|
415 | } |
---|
416 | |
---|
417 | st = jabber_write_packet(ic, node); |
---|
418 | xt_free_node(node); |
---|
419 | |
---|
420 | return st; |
---|
421 | } |
---|
422 | |
---|
423 | static GList *jabber_away_states(struct im_connection *ic) |
---|
424 | { |
---|
425 | static GList *l = NULL; |
---|
426 | int i; |
---|
427 | |
---|
428 | if (l == NULL) { |
---|
429 | for (i = 0; jabber_away_state_list[i].full_name; i++) { |
---|
430 | l = g_list_append(l, (void *) jabber_away_state_list[i].full_name); |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | return l; |
---|
435 | } |
---|
436 | |
---|
437 | static void jabber_get_info(struct im_connection *ic, char *who) |
---|
438 | { |
---|
439 | struct jabber_buddy *bud; |
---|
440 | |
---|
441 | bud = jabber_buddy_by_jid(ic, who, GET_BUDDY_FIRST); |
---|
442 | |
---|
443 | while (bud) { |
---|
444 | imcb_log(ic, "Buddy %s (%d) information:", bud->full_jid, bud->priority); |
---|
445 | if (bud->away_state) { |
---|
446 | imcb_log(ic, "Away state: %s", bud->away_state->full_name); |
---|
447 | } |
---|
448 | imcb_log(ic, "Status message: %s", bud->away_message ? bud->away_message : "(none)"); |
---|
449 | |
---|
450 | bud = bud->next; |
---|
451 | } |
---|
452 | |
---|
453 | jabber_get_vcard(ic, who); |
---|
454 | } |
---|
455 | |
---|
456 | static void jabber_set_away(struct im_connection *ic, char *state_txt, char *message) |
---|
457 | { |
---|
458 | struct jabber_data *jd = ic->proto_data; |
---|
459 | |
---|
460 | /* state_txt == NULL -> Not away. |
---|
461 | Unknown state -> fall back to the first defined away state. */ |
---|
462 | if (state_txt == NULL) { |
---|
463 | jd->away_state = NULL; |
---|
464 | } else if ((jd->away_state = jabber_away_state_by_name(state_txt)) == NULL) { |
---|
465 | jd->away_state = jabber_away_state_list; |
---|
466 | } |
---|
467 | |
---|
468 | g_free(jd->away_message); |
---|
469 | jd->away_message = (message && *message) ? g_strdup(message) : NULL; |
---|
470 | |
---|
471 | presence_send_update(ic); |
---|
472 | } |
---|
473 | |
---|
474 | static void jabber_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
475 | { |
---|
476 | struct jabber_data *jd = ic->proto_data; |
---|
477 | |
---|
478 | if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) { |
---|
479 | jd->flags |= JFLAG_XMLCONSOLE; |
---|
480 | imcb_add_buddy(ic, JABBER_XMLCONSOLE_HANDLE, NULL); |
---|
481 | return; |
---|
482 | } |
---|
483 | |
---|
484 | if (jabber_add_to_roster(ic, who, NULL, group)) { |
---|
485 | presence_send_request(ic, who, "subscribe"); |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | static void jabber_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
490 | { |
---|
491 | struct jabber_data *jd = ic->proto_data; |
---|
492 | |
---|
493 | if (g_strcasecmp(who, JABBER_XMLCONSOLE_HANDLE) == 0) { |
---|
494 | jd->flags &= ~JFLAG_XMLCONSOLE; |
---|
495 | /* Not necessary for now. And for now the code isn't too |
---|
496 | happy if the buddy is completely gone right after calling |
---|
497 | this function already. |
---|
498 | imcb_remove_buddy( ic, JABBER_XMLCONSOLE_HANDLE, NULL ); |
---|
499 | */ |
---|
500 | return; |
---|
501 | } |
---|
502 | |
---|
503 | /* We should always do this part. Clean up our administration a little bit. */ |
---|
504 | jabber_buddy_remove_bare(ic, who); |
---|
505 | |
---|
506 | if (jabber_remove_from_roster(ic, who)) { |
---|
507 | presence_send_request(ic, who, "unsubscribe"); |
---|
508 | } |
---|
509 | } |
---|
510 | |
---|
511 | static struct groupchat *jabber_chat_join_(struct im_connection *ic, const char *room, const char *nick, |
---|
512 | const char *password, set_t **sets) |
---|
513 | { |
---|
514 | struct jabber_data *jd = ic->proto_data; |
---|
515 | char *final_nick; |
---|
516 | |
---|
517 | /* Ignore the passed nick parameter if we have our own default */ |
---|
518 | if (!(final_nick = set_getstr(sets, "nick")) && |
---|
519 | !(final_nick = set_getstr(&ic->acc->set, "display_name"))) { |
---|
520 | /* Well, whatever, actually use the provided default, then */ |
---|
521 | final_nick = (char *) nick; |
---|
522 | } |
---|
523 | |
---|
524 | if (strchr(room, '@') == NULL) { |
---|
525 | imcb_error(ic, "%s is not a valid Jabber room name. Maybe you mean %s@conference.%s?", |
---|
526 | room, room, jd->server); |
---|
527 | } else if (jabber_chat_by_jid(ic, room)) { |
---|
528 | imcb_error(ic, "Already present in chat `%s'", room); |
---|
529 | } else { |
---|
530 | return jabber_chat_join(ic, room, final_nick, set_getstr(sets, "password")); |
---|
531 | } |
---|
532 | |
---|
533 | return NULL; |
---|
534 | } |
---|
535 | |
---|
536 | static struct groupchat *jabber_chat_with_(struct im_connection *ic, char *who) |
---|
537 | { |
---|
538 | return jabber_chat_with(ic, who); |
---|
539 | } |
---|
540 | |
---|
541 | static void jabber_chat_msg_(struct groupchat *c, char *message, int flags) |
---|
542 | { |
---|
543 | if (c && message) { |
---|
544 | jabber_chat_msg(c, message, flags); |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | static void jabber_chat_topic_(struct groupchat *c, char *topic) |
---|
549 | { |
---|
550 | if (c && topic) { |
---|
551 | jabber_chat_topic(c, topic); |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | static void jabber_chat_leave_(struct groupchat *c) |
---|
556 | { |
---|
557 | if (c) { |
---|
558 | jabber_chat_leave(c, NULL); |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | static void jabber_chat_invite_(struct groupchat *c, char *who, char *msg) |
---|
563 | { |
---|
564 | struct jabber_data *jd = c->ic->proto_data; |
---|
565 | struct jabber_chat *jc = c->data; |
---|
566 | gchar *msg_alt = NULL; |
---|
567 | |
---|
568 | if (msg == NULL) { |
---|
569 | msg_alt = g_strdup_printf("%s invited you to %s", jd->me, jc->name); |
---|
570 | } |
---|
571 | |
---|
572 | if (c && who) { |
---|
573 | jabber_chat_invite(c, who, msg ? msg : msg_alt); |
---|
574 | } |
---|
575 | |
---|
576 | g_free(msg_alt); |
---|
577 | } |
---|
578 | |
---|
579 | static void jabber_keepalive(struct im_connection *ic) |
---|
580 | { |
---|
581 | /* Just any whitespace character is enough as a keepalive for XMPP sessions. */ |
---|
582 | if (!jabber_write(ic, "\n", 1)) { |
---|
583 | return; |
---|
584 | } |
---|
585 | |
---|
586 | /* This runs the garbage collection every minute, which means every packet |
---|
587 | is in the cache for about a minute (which should be enough AFAIK). */ |
---|
588 | jabber_cache_clean(ic); |
---|
589 | } |
---|
590 | |
---|
591 | static int jabber_send_typing(struct im_connection *ic, char *who, int typing) |
---|
592 | { |
---|
593 | struct jabber_data *jd = ic->proto_data; |
---|
594 | struct jabber_buddy *bud; |
---|
595 | |
---|
596 | /* Enable typing notification related code from now. */ |
---|
597 | jd->flags |= JFLAG_WANT_TYPING; |
---|
598 | |
---|
599 | if ((bud = jabber_buddy_by_jid(ic, who, 0)) == NULL) { |
---|
600 | /* Sending typing notifications to unknown buddies is |
---|
601 | unsupported for now. Shouldn't be a problem, I think. */ |
---|
602 | return 0; |
---|
603 | } |
---|
604 | |
---|
605 | if (bud->flags & JBFLAG_DOES_XEP85) { |
---|
606 | /* We're only allowed to send this stuff if we know the other |
---|
607 | side supports it. */ |
---|
608 | |
---|
609 | struct xt_node *node; |
---|
610 | char *type; |
---|
611 | int st; |
---|
612 | |
---|
613 | if (typing & OPT_TYPING) { |
---|
614 | type = "composing"; |
---|
615 | } else if (typing & OPT_THINKING) { |
---|
616 | type = "paused"; |
---|
617 | } else { |
---|
618 | type = "active"; |
---|
619 | } |
---|
620 | |
---|
621 | node = xt_new_node(type, NULL, NULL); |
---|
622 | xt_add_attr(node, "xmlns", XMLNS_CHATSTATES); |
---|
623 | node = jabber_make_packet("message", "chat", bud->full_jid, node); |
---|
624 | |
---|
625 | st = jabber_write_packet(ic, node); |
---|
626 | xt_free_node(node); |
---|
627 | |
---|
628 | return st; |
---|
629 | } |
---|
630 | |
---|
631 | return 1; |
---|
632 | } |
---|
633 | |
---|
634 | void jabber_chat_add_settings(account_t *acc, set_t **head) |
---|
635 | { |
---|
636 | /* Meh. Stupid room passwords. Not trying to obfuscate/hide |
---|
637 | them from the user for now. */ |
---|
638 | set_add(head, "password", NULL, NULL, NULL); |
---|
639 | } |
---|
640 | |
---|
641 | void jabber_chat_free_settings(account_t *acc, set_t **head) |
---|
642 | { |
---|
643 | set_del(head, "password"); |
---|
644 | } |
---|
645 | |
---|
646 | GList *jabber_buddy_action_list(bee_user_t *bu) |
---|
647 | { |
---|
648 | static GList *ret = NULL; |
---|
649 | |
---|
650 | if (ret == NULL) { |
---|
651 | static const struct buddy_action ba[2] = { |
---|
652 | { "VERSION", "Get client (version) information" }, |
---|
653 | }; |
---|
654 | |
---|
655 | ret = g_list_prepend(ret, (void *) ba + 0); |
---|
656 | } |
---|
657 | |
---|
658 | return ret; |
---|
659 | } |
---|
660 | |
---|
661 | void *jabber_buddy_action(struct bee_user *bu, const char *action, char * const args[], void *data) |
---|
662 | { |
---|
663 | if (g_strcasecmp(action, "VERSION") == 0) { |
---|
664 | struct jabber_buddy *bud; |
---|
665 | |
---|
666 | if ((bud = jabber_buddy_by_ext_jid(bu->ic, bu->handle, 0)) == NULL) { |
---|
667 | bud = jabber_buddy_by_jid(bu->ic, bu->handle, GET_BUDDY_FIRST); |
---|
668 | } |
---|
669 | for (; bud; bud = bud->next) { |
---|
670 | jabber_iq_version_send(bu->ic, bud, data); |
---|
671 | } |
---|
672 | } |
---|
673 | |
---|
674 | return NULL; |
---|
675 | } |
---|
676 | |
---|
677 | gboolean jabber_handle_is_self(struct im_connection *ic, const char *who) |
---|
678 | { |
---|
679 | struct jabber_data *jd = ic->proto_data; |
---|
680 | |
---|
681 | return ((g_strcasecmp(who, ic->acc->user) == 0) || |
---|
682 | (jd->internal_jid && |
---|
683 | g_strcasecmp(who, jd->internal_jid) == 0)); |
---|
684 | } |
---|
685 | |
---|
686 | void jabber_initmodule() |
---|
687 | { |
---|
688 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
689 | struct prpl *hipchat = NULL; |
---|
690 | |
---|
691 | ret->name = "jabber"; |
---|
692 | ret->mms = 0; /* no limit */ |
---|
693 | ret->login = jabber_login; |
---|
694 | ret->init = jabber_init; |
---|
695 | ret->logout = jabber_logout; |
---|
696 | ret->buddy_msg = jabber_buddy_msg; |
---|
697 | ret->away_states = jabber_away_states; |
---|
698 | ret->set_away = jabber_set_away; |
---|
699 | // ret->set_info = jabber_set_info; |
---|
700 | ret->get_info = jabber_get_info; |
---|
701 | ret->add_buddy = jabber_add_buddy; |
---|
702 | ret->remove_buddy = jabber_remove_buddy; |
---|
703 | ret->chat_msg = jabber_chat_msg_; |
---|
704 | ret->chat_topic = jabber_chat_topic_; |
---|
705 | ret->chat_invite = jabber_chat_invite_; |
---|
706 | ret->chat_leave = jabber_chat_leave_; |
---|
707 | ret->chat_join = jabber_chat_join_; |
---|
708 | ret->chat_with = jabber_chat_with_; |
---|
709 | ret->chat_add_settings = jabber_chat_add_settings; |
---|
710 | ret->chat_free_settings = jabber_chat_free_settings; |
---|
711 | ret->keepalive = jabber_keepalive; |
---|
712 | ret->send_typing = jabber_send_typing; |
---|
713 | ret->handle_cmp = g_strcasecmp; |
---|
714 | ret->handle_is_self = jabber_handle_is_self; |
---|
715 | ret->transfer_request = jabber_si_transfer_request; |
---|
716 | ret->buddy_action_list = jabber_buddy_action_list; |
---|
717 | ret->buddy_action = jabber_buddy_action; |
---|
718 | |
---|
719 | register_protocol(ret); |
---|
720 | |
---|
721 | /* Another one for hipchat, which has completely different logins */ |
---|
722 | hipchat = g_memdup(ret, sizeof(struct prpl)); |
---|
723 | hipchat->name = "hipchat"; |
---|
724 | register_protocol(hipchat); |
---|
725 | } |
---|