Debian Codegraph @

src/core/xep/muc-events.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2007,2008,2009 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 /*
19 * XEP-0045: Multi-User Chat
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "module.h"
26 #include "commands.h"
27 #include "misc.h"
28 #include "settings.h"
29 #include "signals.h"
30
31 #include "rosters-tools.h"
32 #include "tools.h"
33 #include "disco.h"
34 #include "muc.h"
35 #include "muc-nicklist.h"
36 #include "muc-affiliation.h"
37 #include "muc-role.h"
38
39 #define MAX_LONG_STRLEN ((sizeof(long) * CHAR_BIT + 2) / 3 + 1)
40
41 #define XMLNS_DATA_X "jabber:x:data"
42
43 void send_join(MUC_REC *);
44
45 static void
46 topic(MUC_REC *channel, const char *topic, const char *nickname)
47 {
48 if (channel->topic != NULL && topic != NULL
49 && strcmp(channel->topic, topic) == 0)
50 return;
51 g_free(channel->topic);
52 channel->topic = (topic != NULL && *topic != '\0') ?
53 g_strdup(topic) : NULL;
54 g_free(channel->topic_by);
55 channel->topic_by = g_strdup(nickname);
56 signal_emit("channel topic changed", 1, channel);
57 if (channel->joined && nickname != NULL && *nickname != '\0')
58 signal_emit("message topic", 5, channel->server, channel->name,
59 (channel->topic != NULL) ? channel->topic : "",
60 channel->topic_by, "");
61 else {
62 char *data = g_strconcat(" ", channel->name, " :",
63 (channel->topic != NULL) ? channel->topic : "", (void *)NULL);
64 signal_emit("event 332", 2, channel->server, data);
65 g_free(data);
66 }
67 }
68
69 static void
70 nick_changed(MUC_REC *channel, const char *oldnick, const char *newnick)
71 {
72 XMPP_NICK_REC *nick;
73
74 if ((nick = xmpp_nicklist_find(channel, oldnick)) == NULL)
75 return;
76 xmpp_nicklist_rename(channel, nick, oldnick, newnick);
77 if (channel->ownnick == NICK(nick))
78 signal_emit("message xmpp muc own_nick", 3,
79 channel, nick, oldnick);
80 else
81 signal_emit("message xmpp muc nick", 3,
82 channel, nick, oldnick);
83 }
84
85 static void
86 own_join(MUC_REC *channel, const char *nickname,
87 const char *full_jid, const char *affiliation, const char *role,
88 gboolean forced)
89 {
90 XMPP_NICK_REC *nick;
91
92 if (channel->joined)
93 return;
94 if ((nick = xmpp_nicklist_find(channel, nickname)) != NULL)
95 return;
96 nick = xmpp_nicklist_insert(channel, nickname, full_jid);
97 nicklist_set_own(CHANNEL(channel), NICK(nick));
98 channel->chanop = channel->ownnick->op;
99 xmpp_nicklist_set_modes(nick,
100 xmpp_nicklist_get_affiliation(affiliation),
101 xmpp_nicklist_get_role(role));
102 channel->names_got = TRUE;
103 channel->joined = TRUE;
104 signal_emit("message join", 4, channel->server, channel->name,
105 nick->nick, nick->host);
106 signal_emit("message xmpp muc mode", 4, channel,
107 nick->nick, nick->affiliation, nick->role);
108 signal_emit("channel joined", 1, channel);
109 signal_emit("channel sync", 1, channel);
110 channel_send_autocommands(CHANNEL(channel));
111 if (forced)
112 nick_changed(channel, channel->nick, nick->nick);
113 if (*channel->mode == '\0')
114 disco_request(channel->server, channel->name);
115 }
116
117 static void
118 nick_join(MUC_REC *channel, const char *nickname, const char *full_jid,
119 const char *affiliation, const char *role)
120 {
121 XMPP_NICK_REC *nick;
122
123 nick = xmpp_nicklist_insert(channel, nickname, full_jid);
124 xmpp_nicklist_set_modes(nick,
125 xmpp_nicklist_get_affiliation(affiliation),
126 xmpp_nicklist_get_role(role));
127 if (channel->names_got) {
128 signal_emit("message join", 4, channel->server, channel->name,
129 nick->nick, nick->host);
130 signal_emit("message xmpp muc mode", 4, channel,
131 nick->nick, nick->affiliation, nick->role);
132 }
133 }
134
135 static void
136 nick_mode(MUC_REC *channel, XMPP_NICK_REC *nick, const char *affiliation_str,
137 const char *role_str)
138 {
139 int affiliation, role;
140
141 affiliation = xmpp_nicklist_get_affiliation(affiliation_str);
142 role = xmpp_nicklist_get_role(role_str);
143 if (xmpp_nicklist_modes_changed(nick, affiliation, role)) {
144 xmpp_nicklist_set_modes(nick, affiliation, role);
145 signal_emit("message xmpp muc mode", 4, channel,
146 nick->nick, affiliation, role);
147 }
148 }
149
150 static void
151 own_event(MUC_REC *channel, const char *nickname, const char *full_jid,
152 const char *affiliation, const char *role, gboolean forced)
153 {
154 XMPP_NICK_REC *nick;
155
156 if ((nick = xmpp_nicklist_find(channel, nickname)) == NULL)
157 own_join(channel, nickname, full_jid, affiliation, role,
158 forced);
159 else
160 nick_mode(channel, nick, affiliation, role);
161 }
162
163 static void
164 nick_event(MUC_REC *channel, const char *nickname, const char *full_jid,
165 const char *affiliation, const char *role)
166 {
167 XMPP_NICK_REC *nick;
168
169 if ((nick = xmpp_nicklist_find(channel, nickname)) == NULL)
170 nick_join(channel, nickname, full_jid, affiliation, role);
171 else
172 nick_mode(channel, nick, affiliation, role);
173 }
174
175 static void
176 nick_part(MUC_REC *channel, const char *nickname, const char *reason)
177 {
178 XMPP_NICK_REC *nick;
179
180 if ((nick = xmpp_nicklist_find(channel, nickname)) == NULL)
181 return;
182 signal_emit("message part", 5, channel->server, channel->name,
183 nick->nick, nick->host, reason);
184 if (channel->ownnick == NICK(nick)) {
185 channel->left = TRUE;
186 channel_destroy(CHANNEL(channel));
187 } else
188 nicklist_remove(CHANNEL(channel), NICK(nick));
189 }
190
191 static void
192 nick_presence(MUC_REC *channel, const char *nickname, const char *show_str,
193 const char *status)
194 {
195 XMPP_NICK_REC *nick;
196 int show;
197
198 if ((nick = xmpp_nicklist_find(channel, nickname)) == NULL)
199 return;
200 show = xmpp_get_show(show_str);
201 if (xmpp_presence_changed(show, nick->show, status, nick->status,
202 0, 0)) {
203 xmpp_nicklist_set_presence(nick, show, status);
204 if (channel->joined && channel->ownnick != NICK(nick)) {
205 /* TODO eventually show event */
206 }
207 }
208 }
209
210 static void
211 nick_kicked(MUC_REC *channel, const char *nickname, const char *actor,
212 const char *reason)
213 {
214 XMPP_NICK_REC *nick;
215
216 if ((nick = xmpp_nicklist_find(channel, nickname)) == NULL)
217 return;
218 signal_emit("message kick", 6, channel->server, channel->name,
219 nick->nick, (actor != NULL) ? actor : channel->name, nick->host,
220 reason);
221 if (channel->ownnick == NICK(nick)) {
222 channel->kicked = TRUE;
223 channel_destroy(CHANNEL(channel));
224 } else
225 nicklist_remove(CHANNEL(channel), NICK(nick));
226 }
227
228 static void
229 error_message(MUC_REC *channel, const char *code)
230 {
231 int error;
232
233 error = code != NULL ? atoi(code) : MUC_ERROR_UNKNOWN;
234 switch (error) {
235 case MUC_ERROR_PASSWORD_INVALID_OR_MISSING:
236 signal_emit("xmpp muc error", 2, channel, "not allowed");
237 break;
238 }
239 }
240
241 static void
242 error_join(MUC_REC *channel, const char *code, const char *nick)
243 {
244 char *altnick;
245 int error;
246
247 if (nick != NULL && strcmp(nick, channel->nick) != 0)
248 return;
249 error = code != NULL ? atoi(code) : MUC_ERROR_UNKNOWN;
250 signal_emit("xmpp muc joinerror", 2, channel, GINT_TO_POINTER(error));
251 switch(error) {
252 case MUC_ERROR_USE_RESERVED_ROOM_NICK:
253 case MUC_ERROR_NICK_IN_USE:
254 /* rejoin with alternate nick */
255 altnick = (char *)settings_get_str("alternate_nick");
256 if (altnick != NULL && *altnick != '\0'
257 && strcmp(channel->nick, altnick) != 0) {
258 g_free(channel->nick);
259 channel->nick = g_strdup(altnick);
260 } else {
261 altnick = g_strdup_printf("%s_", channel->nick);
262 g_free(channel->nick);
263 channel->nick = altnick;
264 }
265 send_join(channel);
266 return; /* don't destroy the channel */
267 }
268 channel_destroy(CHANNEL(channel));
269 }
270
271 static void
272 error_presence(MUC_REC *channel, const char *code, const char *nick)
273 {
274 int error;
275
276 error = code != NULL ? atoi(code) : MUC_ERROR_UNKNOWN;
277 switch (error) {
278 case MUC_ERROR_NICK_IN_USE:
279 signal_emit("message xmpp muc nick in use", 2, channel, nick);
280 break;
281 }
282 }
283
284 static void
285 error_destroy(MUC_REC *channel, const char *code, const char *reason)
286 {
287 int error;
288
289 error = code != NULL ? atoi(code) : MUC_ERROR_UNKNOWN;
290 switch (error) {
291 case 403:
292 signal_emit("xmpp muc destroyerror", 2, channel, reason);
293 break;
294 }
295 }
296
297 static void
298 available(MUC_REC *channel, const char *from, LmMessage *lmsg)
299 {
300 LmMessageNode *node;
301 const char *item_affiliation, *item_role, *nick;
302 char *item_jid, *item_nick, *status;
303 gboolean own, forced, created;
304
305 item_affiliation = item_role = status = NULL;
306 item_jid = item_nick = NULL;
307 /* <x xmlns='http://jabber.org/protocol/muc#user'> */
308 if ((node = lm_find_node(lmsg->node, "x", XMLNS,
309 XMLNS_MUC_USER)) == NULL)
310 return;
311 /* <status code='110'/> */
312 own = lm_find_node(node, "status", "code", "110") != NULL;
313 /* <status code='210'/> */
314 forced = lm_find_node(node, "status", "code", "210") != NULL;
315 /* <status code='201'/> */
316 created = lm_find_node(node, "status", "code", "201") != NULL;
317 if (created) {
318 char str[MAX_LONG_STRLEN], *data, *recoded;
319 LmMessage *lmsg;
320 LmMessageNode *query, *x;
321
322 /* Require instant room */
323 lmsg = lm_message_new_with_sub_type(channel->name, LM_MESSAGE_TYPE_IQ,
324 LM_MESSAGE_SUB_TYPE_GET);
325 recoded = xmpp_recode_out(channel->server->jid);
326 lm_message_node_set_attribute(lmsg->node, "from", recoded);
327 g_free(recoded);
328 query = lm_message_node_add_child(lmsg->node, "query", NULL);
329 lm_message_node_set_attribute(query, XMLNS, XMLNS_MUC_OWNER);
330 x = lm_message_node_add_child(query, "x", NULL);
331 lm_message_node_set_attribute(x, XMLNS, XMLNS_DATA_X);
332 lm_message_node_set_attribute(x, "type", "submit");
333 signal_emit("xmpp send iq", 2, channel->server, lmsg);
334 lm_message_unref(lmsg);
335
336 /* muc created */
337 g_snprintf(str, sizeof(str), "%ld", (long)time(NULL));
338 data = g_strconcat("_ ", channel->name, " ", str, (void *)NULL);
339 signal_emit("event 329", 2, channel->server, data);
340 g_free(data);
341 }
342 if ((node = lm_message_node_get_child(node, "item")) == NULL)
343 return;
344 /* <item affiliation='item_affiliation'
345 * role='item_role'
346 * jid='item_jid'
347 * nick='item_nick'/> */
348 item_affiliation = lm_message_node_get_attribute(node, "affiliation");
349 item_role = lm_message_node_get_attribute(node, "role");
350 item_jid = xmpp_recode_in( lm_message_node_get_attribute(node, "jid"));
351 item_nick = xmpp_recode_in( lm_message_node_get_attribute(node, "nick"));
352 nick = item_nick != NULL ? item_nick : from;
353 if (nick == NULL)
354 goto err;
355 if (own || strcmp(nick, channel->nick) == 0)
356 own_event(channel, nick, item_jid, item_affiliation, item_role,
357 forced);
358 else
359 nick_event(channel, nick, item_jid, item_affiliation, item_role);
360 /* <status>text</status> */
361 node = lm_message_node_get_child(lmsg->node, "status");
362 if (node != NULL)
363 status = xmpp_recode_in(node->value);
364 /* <show>show</show> */
365 node = lm_message_node_get_child(lmsg->node, "show");
366 nick_presence(channel, nick, node != NULL ? node->value : NULL, status);
367 g_free(status);
368 err: g_free(item_jid);
369 g_free(item_nick);
370 }
371
372 static void
373 unavailable(MUC_REC *channel, const char *nick, LmMessage *lmsg)
374 {
375 LmMessageNode *node, *child;
376 const char *status_code;
377 char *reason, *actor, *item_nick, *status;
378
379 status_code = NULL;
380 reason = actor = item_nick = status = NULL;
381 /* <x xmlns='http://jabber.org/protocol/muc#user'> */
382 node = lm_find_node(lmsg->node, "x", XMLNS, XMLNS_MUC_USER);
383 if (node != NULL) {
384 /* <status code='status_code'/> */
385 child = lm_message_node_get_child(node, "status");
386 if (child != NULL)
387 status_code =
388 lm_message_node_get_attribute(child, "code");
389 /* <item nick='item_nick'> */
390 node = lm_message_node_get_child(node, "item");
391 if (node != NULL) {
392 item_nick = xmpp_recode_in(
393 lm_message_node_get_attribute(node, "nick"));
394 /* <reason>reason</reason> */
395 child = lm_message_node_get_child(node, "reason");
396 if (child != NULL)
397 reason = xmpp_recode_in(child->value);
398 /* <actor jid='actor'/> */
399 child = lm_message_node_get_child(node, "actor");
400 if (child != NULL)
401 actor = xmpp_recode_in(
402 lm_message_node_get_attribute(child, "jid"));
403 }
404 }
405 if (status_code != NULL) {
406 switch (atoi(status_code)) {
407 case 303: /* <status code='303'/> */
408 nick_changed(channel, nick, item_nick);
409 break;
410 case 307: /* kick: <status code='307'/> */
411 nick_kicked(channel, nick, actor, reason);
412 break;
413 case 301: /* ban: <status code='301'/> */
414 nick_kicked(channel, nick, actor, reason);
415 break;
416 }
417 } else {
418 /* <status>text</status> */
419 node = lm_message_node_get_child(lmsg->node, "status");
420 if (node != NULL)
421 status = xmpp_recode_in(node->value);
422 nick_part(channel, nick, status);
423 g_free(status);
424 }
425 g_free(item_nick);
426 g_free(reason);
427 g_free(actor);
428 }
429
430 static void
431 admin(MUC_REC *channel, LmMessage *lmsg)
432 {
433 LmMessageNode *node, *query;
434 const char *item_affiliation, *item_role, *item_jid, *item_nick;
435 int affiliation, role;
436
437 if ((query = lm_find_node(lmsg->node, "query", XMLNS,
438 XMLNS_MUC_ADMIN)) == NULL)
439 return;
440
441 for (node = query->children; node != NULL; node = node->next) {
442 /* <item affiliation='item_affiliation'
443 * role='item_role'
444 * jid='item_jid'
445 * nick='item_nick'/> */
446 item_jid = xmpp_recode_in(lm_message_node_get_attribute(node,
447 "jid"));
448 item_affiliation = lm_message_node_get_attribute(node,
449 "affiliation");
450 item_nick = xmpp_recode_in(lm_message_node_get_attribute(node,
451 "nick"));
452 item_role = lm_message_node_get_attribute(node, "role");
453 affiliation = xmpp_nicklist_get_affiliation(item_affiliation);
454 if (item_role == NULL) {
455 /* affiliation query */
456 signal_emit("message xmpp muc affiliation", 4, channel,
457 item_jid, item_nick, affiliation);
458 } else {
459 role = xmpp_nicklist_get_role(item_role);
460 signal_emit("message xmpp muc mode", 4, channel,
461 item_nick, affiliation, role);
462 }
463 }
464 }
465
466 static void
467 invite(XMPP_SERVER_REC *server, const char *channame, LmMessageNode *node,
468 LmMessageNode *invite_node)
469 {
470 LmMessageNode *pass;
471 CHANNEL_SETUP_REC *setup;
472 const char *from;
473 char *inviter, *password, *joindata;
474
475 if ((from = lm_message_node_get_attribute(invite_node, "from")) == NULL)
476 return;
477 inviter = xmpp_recode_in(from);
478 pass = lm_message_node_get_child(node, "password");
479 password = pass != NULL ? xmpp_recode_in(pass->value) : NULL;
480 signal_emit("xmpp invite", 4, server, inviter, channame, password);
481 /* check if we're supposed to autojoin this muc */
482 setup = channel_setup_find(channame, server->connrec->chatnet);
483 if (setup != NULL && setup->autojoin
484 && settings_get_bool("join_auto_chans_on_invite")) {
485 joindata = password == NULL ?
486 g_strconcat("\"", channame, "\"", (void *)NULL)
487 : g_strconcat("\"", channame, "\" ",
488 password, (void *)NULL);
489 muc_join(server, joindata, TRUE);
490 g_free(joindata);
491 }
492 g_free(inviter);
493 g_free(password);
494 g_free(server->last_invite);
495 server->last_invite = g_strdup(channame);
496 }
497
498 static void
499 sig_recv_message(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
500 const char *id, const char *from, const char *to)
501 {
502 MUC_REC *channel;
503 LmMessageNode *node, *child;
504 char *nick, *str;
505 gboolean action, own;
506
507 if ((channel = get_muc(server, from)) == NULL) {
508 /* Not a joined channel, search the MUC namespace */
509 /* <x xmlns='http://jabber.org/protocol/muc#user'> */
510 node = lm_find_node(lmsg->node, "x", XMLNS, XMLNS_MUC_USER);
511 if (node == NULL)
512 return;
513 switch (type) {
514 case LM_MESSAGE_SUB_TYPE_NOT_SET:
515 case LM_MESSAGE_SUB_TYPE_NORMAL:
516 child = lm_message_node_get_child(node, "invite");
517 if (child != NULL)
518 invite(server, from, node, child);
519 break;
520 }
521 return;
522 }
523 nick = muc_extract_nick(from);
524 switch (type) {
525 case LM_MESSAGE_SUB_TYPE_ERROR:
526 node = lm_message_node_get_child(lmsg->node, "error");
527 if (node != NULL) {
528 /* TODO: extract error type and name -> XMLNS_STANZAS */
529 error_message(channel,
530 lm_message_node_get_attribute(node, "code"));
531 }
532 break;
533 case LM_MESSAGE_SUB_TYPE_GROUPCHAT:
534 node = lm_message_node_get_child(lmsg->node, "subject");
535 if (node != NULL) {
536 str = xmpp_recode_in(node->value);
537 topic(channel, str, nick);
538 g_free(str);
539 }
540 node = lm_message_node_get_child(lmsg->node, "body");
541 if (node != NULL && node->value != NULL) {
542 str = xmpp_recode_in(node->value);
543 if (nick != NULL) {
544 own = strcmp(nick, channel->nick) == 0;
545 action = g_ascii_strncasecmp(str, "/me ", 4) == 0;
546 if (action && own)
547 signal_emit("message xmpp own_action", 4,
548 server, str+4, channel->name,
549 GINT_TO_POINTER(SEND_TARGET_CHANNEL));
550 else if (action)
551 signal_emit("message xmpp action", 5,
552 server, str+4, nick, channel->name,
553 GINT_TO_POINTER(SEND_TARGET_CHANNEL));
554 else if (own)
555 signal_emit("message xmpp own_public", 3,
556 server, str, channel->name);
557 else
558 signal_emit("message public", 5,
559 server, str, nick, "", channel->name);
560 } else {
561 signal_emit("message xmpp room", 3,
562 server, str, channel->name);
563 }
564 g_free(str);
565 }
566 break;
567 }
568 g_free(nick);
569 }
570
571 static void
572 sig_recv_presence(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
573 const char *id, const char *from, const char *to)
574 {
575 MUC_REC *channel;
576 LmMessageNode *node;
577 const char *code;
578 char *nick;
579
580 if ((channel = get_muc(server, from)) == NULL)
581 return;
582 nick = muc_extract_nick(from);
583 switch (type) {
584 case LM_MESSAGE_SUB_TYPE_ERROR:
585 node = lm_message_node_get_child(lmsg->node, "error");
586 if (node == NULL)
587 goto out;
588 /* TODO: extract error type and name -> XMLNS_STANZAS */
589 code = lm_message_node_get_attribute(node, "code");
590 if (!channel->joined)
591 error_join(channel, code, nick);
592 else
593 error_presence(channel, code, nick);
594 break;
595 case LM_MESSAGE_SUB_TYPE_AVAILABLE:
596 available(channel, nick, lmsg);
597 break;
598 case LM_MESSAGE_SUB_TYPE_UNAVAILABLE:
599 unavailable(channel, nick, lmsg);
600 break;
601 }
602
603 out:
604 g_free(nick);
605 }
606
607 static void
608 sig_recv_iq(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
609 const char *id, const char *from, const char *to)
610 {
611 MUC_REC *channel;
612 LmMessageNode *node, *error, *text, *query;
613 const char *code;
614 char *reason;
615
616 if ((channel = get_muc(server, from)) == NULL)
617 return;
618
619 switch (type) {
620 case LM_MESSAGE_SUB_TYPE_ERROR:
621 error = lm_message_node_get_child(lmsg->node, "error");
622 if (error == NULL)
623 return;
624 code = lm_message_node_get_attribute(error, "code");
625
626 query = lm_find_node(lmsg->node, "query", XMLNS, XMLNS_MUC_OWNER);
627 if (query == NULL)
628 return;
629 for (node = query->children; node != NULL; node = node->next) {
630 if (strcmp(node->name, "destroy") == 0) {
631 text = lm_message_node_get_child(error, "text");
632 reason = xmpp_recode_in(text->value);
633 error_destroy(channel, code, reason);
634 g_free(reason);
635 }
636 }
637 break;
638 case LM_MESSAGE_SUB_TYPE_RESULT:
639 admin(channel, lmsg);
640 break;
641 }
642 }
643
644 void
645 muc_events_init(void)
646 {
647 signal_add("xmpp recv message", sig_recv_message);
648 signal_add("xmpp recv presence", sig_recv_presence);
649 signal_add("xmpp recv iq", sig_recv_iq);
650 }
651
652 void
653 muc_events_deinit(void)
654 {
655 signal_remove("xmpp recv message", sig_recv_message);
656 signal_remove("xmpp recv presence", sig_recv_presence);
657 signal_remove("xmpp recv iq", sig_recv_iq);
658 }