1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Conference rooms * |
---|
5 | * * |
---|
6 | * Copyright 2007-2012 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 "jabber.h" |
---|
25 | #include "sha1.h" |
---|
26 | |
---|
27 | static xt_status jabber_chat_join_failed(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
28 | static xt_status jabber_chat_self_message(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); |
---|
29 | |
---|
30 | struct groupchat *jabber_chat_join(struct im_connection *ic, const char *room, const char *nick, const char *password) |
---|
31 | { |
---|
32 | struct jabber_chat *jc; |
---|
33 | struct xt_node *node; |
---|
34 | struct groupchat *c; |
---|
35 | char *roomjid; |
---|
36 | |
---|
37 | roomjid = g_strdup_printf("%s/%s", room, nick); |
---|
38 | node = xt_new_node("x", NULL, NULL); |
---|
39 | xt_add_attr(node, "xmlns", XMLNS_MUC); |
---|
40 | if (password) { |
---|
41 | xt_add_child(node, xt_new_node("password", password, NULL)); |
---|
42 | } |
---|
43 | node = jabber_make_packet("presence", NULL, roomjid, node); |
---|
44 | jabber_cache_add(ic, node, jabber_chat_join_failed); |
---|
45 | |
---|
46 | if (!jabber_write_packet(ic, node)) { |
---|
47 | g_free(roomjid); |
---|
48 | return NULL; |
---|
49 | } |
---|
50 | |
---|
51 | jc = g_new0(struct jabber_chat, 1); |
---|
52 | jc->name = jabber_normalize(room); |
---|
53 | |
---|
54 | if ((jc->me = jabber_buddy_add(ic, roomjid)) == NULL) { |
---|
55 | g_free(roomjid); |
---|
56 | g_free(jc->name); |
---|
57 | g_free(jc); |
---|
58 | return NULL; |
---|
59 | } |
---|
60 | |
---|
61 | /* roomjid isn't normalized yet, and we need an original version |
---|
62 | of the nick to send a proper presence update. */ |
---|
63 | jc->my_full_jid = roomjid; |
---|
64 | |
---|
65 | c = imcb_chat_new(ic, room); |
---|
66 | c->data = jc; |
---|
67 | |
---|
68 | return c; |
---|
69 | } |
---|
70 | |
---|
71 | struct groupchat *jabber_chat_with(struct im_connection *ic, char *who) |
---|
72 | { |
---|
73 | struct jabber_data *jd = ic->proto_data; |
---|
74 | struct jabber_chat *jc; |
---|
75 | struct groupchat *c; |
---|
76 | sha1_state_t sum; |
---|
77 | double now = gettime(); |
---|
78 | char *uuid, *rjid, *cserv; |
---|
79 | |
---|
80 | sha1_init(&sum); |
---|
81 | sha1_append(&sum, (uint8_t *) ic->acc->user, strlen(ic->acc->user)); |
---|
82 | sha1_append(&sum, (uint8_t *) &now, sizeof(now)); |
---|
83 | sha1_append(&sum, (uint8_t *) who, strlen(who)); |
---|
84 | uuid = sha1_random_uuid(&sum); |
---|
85 | |
---|
86 | if (jd->flags & JFLAG_GTALK) { |
---|
87 | cserv = g_strdup("groupchat.google.com"); |
---|
88 | } else { |
---|
89 | /* Guess... */ |
---|
90 | cserv = g_strdup_printf("conference.%s", jd->server); |
---|
91 | } |
---|
92 | |
---|
93 | rjid = g_strdup_printf("private-chat-%s@%s", uuid, cserv); |
---|
94 | g_free(uuid); |
---|
95 | g_free(cserv); |
---|
96 | |
---|
97 | c = jabber_chat_join(ic, rjid, jd->username, NULL); |
---|
98 | g_free(rjid); |
---|
99 | if (c == NULL) { |
---|
100 | return NULL; |
---|
101 | } |
---|
102 | |
---|
103 | jc = c->data; |
---|
104 | jc->invite = g_strdup(who); |
---|
105 | |
---|
106 | return c; |
---|
107 | } |
---|
108 | |
---|
109 | static xt_status jabber_chat_join_failed(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
110 | { |
---|
111 | struct jabber_error *err; |
---|
112 | struct jabber_buddy *bud; |
---|
113 | char *room; |
---|
114 | |
---|
115 | room = xt_find_attr(orig, "to"); |
---|
116 | bud = jabber_buddy_by_jid(ic, room, 0); |
---|
117 | err = jabber_error_parse(xt_find_node(node->children, "error"), XMLNS_STANZA_ERROR); |
---|
118 | if (err) { |
---|
119 | imcb_error(ic, "Error joining groupchat %s: %s%s%s", room, err->code, |
---|
120 | err->text ? ": " : "", err->text ? err->text : ""); |
---|
121 | jabber_error_free(err); |
---|
122 | } |
---|
123 | if (bud) { |
---|
124 | struct groupchat *c = jabber_chat_by_jid(ic, bud->bare_jid); |
---|
125 | if (c) { |
---|
126 | jabber_chat_free(c); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | return XT_HANDLED; |
---|
131 | } |
---|
132 | |
---|
133 | static xt_status jabber_chat_self_message(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) |
---|
134 | { |
---|
135 | /* This is a self message sent by this bitlbee - just drop it */ |
---|
136 | return XT_ABORT; |
---|
137 | } |
---|
138 | |
---|
139 | struct groupchat *jabber_chat_by_jid(struct im_connection *ic, const char *name) |
---|
140 | { |
---|
141 | char *normalized = jabber_normalize(name); |
---|
142 | GSList *l; |
---|
143 | struct groupchat *ret; |
---|
144 | struct jabber_chat *jc; |
---|
145 | |
---|
146 | for (l = ic->groupchats; l; l = l->next) { |
---|
147 | ret = l->data; |
---|
148 | jc = ret->data; |
---|
149 | if (strcmp(normalized, jc->name) == 0) { |
---|
150 | break; |
---|
151 | } |
---|
152 | } |
---|
153 | g_free(normalized); |
---|
154 | |
---|
155 | return l ? ret : NULL; |
---|
156 | } |
---|
157 | |
---|
158 | void jabber_chat_free(struct groupchat *c) |
---|
159 | { |
---|
160 | struct jabber_chat *jc = c->data; |
---|
161 | |
---|
162 | jabber_buddy_remove_bare(c->ic, jc->name); |
---|
163 | |
---|
164 | g_free(jc->my_full_jid); |
---|
165 | g_free(jc->name); |
---|
166 | g_free(jc->invite); |
---|
167 | g_free(jc); |
---|
168 | |
---|
169 | imcb_chat_free(c); |
---|
170 | } |
---|
171 | |
---|
172 | int jabber_chat_msg(struct groupchat *c, char *message, int flags) |
---|
173 | { |
---|
174 | struct im_connection *ic = c->ic; |
---|
175 | struct jabber_chat *jc = c->data; |
---|
176 | struct xt_node *node; |
---|
177 | |
---|
178 | jc->flags |= JCFLAG_MESSAGE_SENT; |
---|
179 | |
---|
180 | node = xt_new_node("body", message, NULL); |
---|
181 | node = jabber_make_packet("message", "groupchat", jc->name, node); |
---|
182 | |
---|
183 | jabber_cache_add(ic, node, jabber_chat_self_message); |
---|
184 | |
---|
185 | return !jabber_write_packet(ic, node); |
---|
186 | } |
---|
187 | |
---|
188 | int jabber_chat_topic(struct groupchat *c, char *topic) |
---|
189 | { |
---|
190 | struct im_connection *ic = c->ic; |
---|
191 | struct jabber_chat *jc = c->data; |
---|
192 | struct xt_node *node; |
---|
193 | |
---|
194 | node = xt_new_node("subject", topic, NULL); |
---|
195 | node = jabber_make_packet("message", "groupchat", jc->name, node); |
---|
196 | |
---|
197 | if (!jabber_write_packet(ic, node)) { |
---|
198 | xt_free_node(node); |
---|
199 | return 0; |
---|
200 | } |
---|
201 | xt_free_node(node); |
---|
202 | |
---|
203 | return 1; |
---|
204 | } |
---|
205 | |
---|
206 | int jabber_chat_leave(struct groupchat *c, const char *reason) |
---|
207 | { |
---|
208 | struct im_connection *ic = c->ic; |
---|
209 | struct jabber_chat *jc = c->data; |
---|
210 | struct xt_node *node; |
---|
211 | |
---|
212 | node = xt_new_node("x", NULL, NULL); |
---|
213 | xt_add_attr(node, "xmlns", XMLNS_MUC); |
---|
214 | node = jabber_make_packet("presence", "unavailable", jc->my_full_jid, node); |
---|
215 | |
---|
216 | if (!jabber_write_packet(ic, node)) { |
---|
217 | xt_free_node(node); |
---|
218 | return 0; |
---|
219 | } |
---|
220 | xt_free_node(node); |
---|
221 | |
---|
222 | return 1; |
---|
223 | } |
---|
224 | |
---|
225 | void jabber_chat_invite(struct groupchat *c, char *who, char *message) |
---|
226 | { |
---|
227 | struct xt_node *node; |
---|
228 | struct im_connection *ic = c->ic; |
---|
229 | struct jabber_chat *jc = c->data; |
---|
230 | |
---|
231 | node = xt_new_node("reason", message, NULL); |
---|
232 | |
---|
233 | node = xt_new_node("invite", NULL, node); |
---|
234 | xt_add_attr(node, "to", who); |
---|
235 | |
---|
236 | node = xt_new_node("x", NULL, node); |
---|
237 | xt_add_attr(node, "xmlns", XMLNS_MUC_USER); |
---|
238 | |
---|
239 | node = jabber_make_packet("message", NULL, jc->name, node); |
---|
240 | |
---|
241 | jabber_write_packet(ic, node); |
---|
242 | |
---|
243 | xt_free_node(node); |
---|
244 | } |
---|
245 | |
---|
246 | /* Not really the same syntax as the normal pkt_ functions, but this isn't |
---|
247 | called by the xmltree parser directly and this way I can add some extra |
---|
248 | parameters so we won't have to repeat too many things done by the caller |
---|
249 | already. */ |
---|
250 | void jabber_chat_pkt_presence(struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node) |
---|
251 | { |
---|
252 | struct groupchat *chat; |
---|
253 | struct xt_node *c; |
---|
254 | char *type = xt_find_attr(node, "type"); |
---|
255 | struct jabber_data *jd = ic->proto_data; |
---|
256 | struct jabber_chat *jc; |
---|
257 | char *s; |
---|
258 | |
---|
259 | if ((chat = jabber_chat_by_jid(ic, bud->bare_jid)) == NULL) { |
---|
260 | /* How could this happen?? We could do kill( self, 11 ) |
---|
261 | now or just wait for the OS to do it. :-) */ |
---|
262 | return; |
---|
263 | } |
---|
264 | |
---|
265 | jc = chat->data; |
---|
266 | |
---|
267 | if (type == NULL && !(bud->flags & JBFLAG_IS_CHATROOM)) { |
---|
268 | bud->flags |= JBFLAG_IS_CHATROOM; |
---|
269 | /* If this one wasn't set yet, this buddy just joined the chat. |
---|
270 | Slightly hackish way of finding out eh? ;-) */ |
---|
271 | |
---|
272 | /* This is pretty messy... Here it sets ext_jid to the real |
---|
273 | JID of the participant. Works for non-anonymized channels. |
---|
274 | Might break if someone joins a chat twice, though. */ |
---|
275 | for (c = node->children; (c = xt_find_node(c, "x")); c = c->next) { |
---|
276 | if ((s = xt_find_attr(c, "xmlns")) && |
---|
277 | (strcmp(s, XMLNS_MUC_USER) == 0)) { |
---|
278 | struct xt_node *item; |
---|
279 | |
---|
280 | item = xt_find_node(c->children, "item"); |
---|
281 | if ((s = xt_find_attr(item, "jid"))) { |
---|
282 | /* Yay, found what we need. :-) */ |
---|
283 | bud->ext_jid = jabber_normalize(s); |
---|
284 | break; |
---|
285 | } |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | /* Make up some other handle, if necessary. */ |
---|
290 | if (bud->ext_jid == NULL) { |
---|
291 | if (bud == jc->me) { |
---|
292 | bud->ext_jid = g_strdup(jd->me); |
---|
293 | } else { |
---|
294 | int i; |
---|
295 | |
---|
296 | /* Don't want the nick to be at the end, so let's |
---|
297 | think of some slightly different notation to use |
---|
298 | for anonymous groupchat participants in BitlBee. */ |
---|
299 | bud->ext_jid = g_strdup_printf("%s=%s", bud->resource, bud->bare_jid); |
---|
300 | |
---|
301 | /* And strip any unwanted characters. */ |
---|
302 | for (i = 0; bud->resource[i]; i++) { |
---|
303 | if (bud->ext_jid[i] == '=' || bud->ext_jid[i] == '@') { |
---|
304 | bud->ext_jid[i] = '_'; |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | bud->flags |= JBFLAG_IS_ANONYMOUS; |
---|
309 | } |
---|
310 | |
---|
311 | if (bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS) { |
---|
312 | /* If JIDs are anonymized, add them to the local |
---|
313 | list for the duration of this chat. */ |
---|
314 | imcb_add_buddy(ic, bud->ext_jid, NULL); |
---|
315 | imcb_buddy_nick_hint(ic, bud->ext_jid, bud->resource); |
---|
316 | } |
---|
317 | |
---|
318 | if (bud == jc->me && jc->invite != NULL) { |
---|
319 | char *msg = g_strdup_printf("Please join me in room %s", jc->name); |
---|
320 | jabber_chat_invite(chat, jc->invite, msg); |
---|
321 | g_free(jc->invite); |
---|
322 | g_free(msg); |
---|
323 | jc->invite = NULL; |
---|
324 | } |
---|
325 | |
---|
326 | s = strchr(bud->ext_jid, '/'); |
---|
327 | if (s) { |
---|
328 | *s = 0; /* Should NEVER be NULL, but who knows... */ |
---|
329 | } |
---|
330 | imcb_chat_add_buddy(chat, bud->ext_jid); |
---|
331 | if (s) { |
---|
332 | *s = '/'; |
---|
333 | } |
---|
334 | } else if (type) { /* type can only be NULL or "unavailable" in this function */ |
---|
335 | if ((bud->flags & JBFLAG_IS_CHATROOM) && bud->ext_jid) { |
---|
336 | char *reason = NULL; |
---|
337 | char *status = NULL; |
---|
338 | char *status_text = NULL; |
---|
339 | |
---|
340 | if ((c = xt_find_node_by_attr(node->children, "x", "xmlns", XMLNS_MUC_USER))) { |
---|
341 | struct xt_node *c2 = c->children; |
---|
342 | |
---|
343 | while ((c2 = xt_find_node(c2, "status"))) { |
---|
344 | char *code = xt_find_attr(c2, "code"); |
---|
345 | if (g_strcmp0(code, "301") == 0) { |
---|
346 | status = "Banned"; |
---|
347 | break; |
---|
348 | } else if (g_strcmp0(code, "303") == 0) { |
---|
349 | /* This could be handled in a cleverer way, |
---|
350 | * but let's just show a literal part/join for now */ |
---|
351 | status = "Changing nicks"; |
---|
352 | break; |
---|
353 | } else if (g_strcmp0(code, "307") == 0) { |
---|
354 | status = "Kicked"; |
---|
355 | break; |
---|
356 | } |
---|
357 | c2 = c2->next; |
---|
358 | } |
---|
359 | |
---|
360 | /* Sometimes the status message is in presence/x/item/reason */ |
---|
361 | if ((c2 = xt_find_path(c, "item/reason")) && c2->text && c2->text_len) { |
---|
362 | status_text = c2->text; |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | /* Sometimes the status message is right inside <presence> */ |
---|
367 | if ((c = xt_find_node(node->children, "status")) && c->text && c->text_len) { |
---|
368 | status_text = c->text; |
---|
369 | } |
---|
370 | |
---|
371 | if (status_text && status) { |
---|
372 | reason = g_strdup_printf("%s: %s", status, status_text); |
---|
373 | } else { |
---|
374 | reason = g_strdup(status_text ? : status); |
---|
375 | } |
---|
376 | |
---|
377 | s = strchr(bud->ext_jid, '/'); |
---|
378 | if (s) { |
---|
379 | *s = 0; |
---|
380 | } |
---|
381 | imcb_chat_remove_buddy(chat, bud->ext_jid, reason); |
---|
382 | if (bud != jc->me && bud->flags & JBFLAG_IS_ANONYMOUS) { |
---|
383 | imcb_remove_buddy(ic, bud->ext_jid, reason); |
---|
384 | } |
---|
385 | if (s) { |
---|
386 | *s = '/'; |
---|
387 | } |
---|
388 | |
---|
389 | g_free(reason); |
---|
390 | } |
---|
391 | |
---|
392 | if (bud == jc->me) { |
---|
393 | jabber_chat_free(chat); |
---|
394 | } |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | void jabber_chat_pkt_message(struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node) |
---|
399 | { |
---|
400 | struct xt_node *subject = xt_find_node(node->children, "subject"); |
---|
401 | struct xt_node *body = xt_find_node(node->children, "body"); |
---|
402 | struct groupchat *chat = NULL; |
---|
403 | struct jabber_chat *jc = NULL; |
---|
404 | char *from = NULL; |
---|
405 | char *nick = NULL; |
---|
406 | char *final_from = NULL; |
---|
407 | char *bare_jid = NULL; |
---|
408 | guint32 flags = 0; |
---|
409 | |
---|
410 | from = (bud) ? bud->full_jid : xt_find_attr(node, "from"); |
---|
411 | |
---|
412 | if (from) { |
---|
413 | nick = strchr(from, '/'); |
---|
414 | if (nick) { |
---|
415 | *nick = 0; |
---|
416 | } |
---|
417 | chat = jabber_chat_by_jid(ic, from); |
---|
418 | if (nick) { |
---|
419 | *nick = '/'; |
---|
420 | nick++; |
---|
421 | } |
---|
422 | } |
---|
423 | |
---|
424 | jc = (chat) ? chat->data : NULL; |
---|
425 | |
---|
426 | if (!bud) { |
---|
427 | struct xt_node *c; |
---|
428 | char *s; |
---|
429 | |
---|
430 | /* Try some clever stuff to find out the real JID here */ |
---|
431 | c = xt_find_node_by_attr(node->children, "delay", "xmlns", XMLNS_DELAY); |
---|
432 | |
---|
433 | if (c && ((s = xt_find_attr(c, "from")) || |
---|
434 | (s = xt_find_attr(c, "from_jid")))) { |
---|
435 | /* This won't be useful if it's the MUC JID */ |
---|
436 | if (!(jc && jabber_compare_jid(s, jc->name))) { |
---|
437 | /* Hopefully this one makes more sense! */ |
---|
438 | bud = jabber_buddy_by_jid(ic, s, GET_BUDDY_FIRST | GET_BUDDY_CREAT); |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | } |
---|
443 | |
---|
444 | if (subject && chat) { |
---|
445 | char *subject_text = subject->text_len > 0 ? subject->text : ""; |
---|
446 | if (g_strcmp0(chat->topic, subject_text) != 0) { |
---|
447 | bare_jid = (bud) ? jabber_get_bare_jid(bud->ext_jid) : NULL; |
---|
448 | imcb_chat_topic(chat, bare_jid, subject_text, |
---|
449 | jabber_get_timestamp(node)); |
---|
450 | g_free(bare_jid); |
---|
451 | bare_jid = NULL; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | if (body == NULL || body->text_len == 0) { |
---|
456 | /* Meh. Empty messages aren't very interesting, no matter |
---|
457 | how much some servers love to send them. */ |
---|
458 | return; |
---|
459 | } |
---|
460 | |
---|
461 | if (chat == NULL) { |
---|
462 | if (nick == NULL) { |
---|
463 | imcb_log(ic, "System message from unknown groupchat %s: %s", from, body->text); |
---|
464 | } else { |
---|
465 | imcb_log(ic, "Groupchat message from unknown JID %s: %s", from, body->text); |
---|
466 | } |
---|
467 | |
---|
468 | return; |
---|
469 | } else if (chat != NULL && bud == NULL && nick == NULL) { |
---|
470 | imcb_chat_log(chat, "From conference server: %s", body->text); |
---|
471 | return; |
---|
472 | } else if (jc && jc->flags & JCFLAG_MESSAGE_SENT && bud == jc->me && |
---|
473 | (jabber_cache_handle_packet(ic, node) == XT_ABORT)) { |
---|
474 | /* Self message marked by this bitlbee, don't show it */ |
---|
475 | return; |
---|
476 | } |
---|
477 | |
---|
478 | if (bud) { |
---|
479 | bare_jid = jabber_get_bare_jid(bud->ext_jid ? bud->ext_jid : bud->full_jid); |
---|
480 | final_from = bare_jid; |
---|
481 | flags = (bud == jc->me) ? OPT_SELFMESSAGE : 0; |
---|
482 | } else { |
---|
483 | final_from = nick; |
---|
484 | } |
---|
485 | |
---|
486 | imcb_chat_msg(chat, final_from, body->text, flags, jabber_get_timestamp(node)); |
---|
487 | |
---|
488 | g_free(bare_jid); |
---|
489 | } |
---|