Debian Codegraph @

src/core/xep/delay.c

raw symbols SCIP
1 /*
2 * Copyright (C) 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-0203: Delayed Delivery
20 */
21
22 #include "module.h"
23 #include "signals.h"
24
25 #include "xmpp-servers.h"
26 #include "tools.h"
27 #include "datetime.h"
28 #include "disco.h"
29 #include "muc.h"
30
31 #define XMLNS_DELAY "urn:xmpp:delay"
32 #define XMLNS_OLD_DELAY "jabber:x:delay"
33
34 static void
35 sig_recv_message(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
36 const char *id, const char *from, const char *to)
37 {
38 LmMessageNode *node;
39 MUC_REC *channel;
40 const char *stamp;
41 char *nick, *str;
42 time_t t;
43
44 node = lm_find_node(lmsg->node, "delay", "xmlns", XMLNS_DELAY);
45 if (node == NULL) {
46 /* XEP-0091: Delayed Delivery (deprecated) */
47 node = lm_find_node(lmsg->node, "x", "xmlns", XMLNS_OLD_DELAY);
48 if (node == NULL)
49 return;
50 }
51 stamp = lm_message_node_get_attribute(node, "stamp");
52 if ((t = xep82_datetime(stamp)) == (time_t)-1)
53 return;
54 node = lm_message_node_get_child(lmsg->node, "body");
55 if (node == NULL || node->value == NULL || *node->value == '\0')
56 return;
57 if (type == LM_MESSAGE_SUB_TYPE_GROUPCHAT
58 && (channel = get_muc(server, from)) != NULL
59 && (nick = muc_extract_nick(from)) != NULL) {
60 str = xmpp_recode_in(node->value);
61 if (g_ascii_strncasecmp(str, "/me ", 4) == 0)
62 signal_emit("message xmpp delay action", 6,
63 server, str+4, nick, channel->name, &t,
64 GINT_TO_POINTER(SEND_TARGET_CHANNEL));
65 else
66 signal_emit("message xmpp delay", 6, server,
67 str, nick, channel->name, &t,
68 GINT_TO_POINTER(SEND_TARGET_CHANNEL));
69 g_free(str);
70 g_free(nick);
71 } else if ((type == LM_MESSAGE_SUB_TYPE_NOT_SET
72 || type == LM_MESSAGE_SUB_TYPE_HEADLINE
73 || type == LM_MESSAGE_SUB_TYPE_NORMAL
74 || type == LM_MESSAGE_SUB_TYPE_CHAT)) {
75 str = xmpp_recode_in(node->value);
76 if (g_ascii_strncasecmp(str, "/me ", 4) == 0)
77 signal_emit("message xmpp delay action", 6,
78 server, str+4, from, from, &t,
79 GINT_TO_POINTER(SEND_TARGET_NICK));
80 else
81 signal_emit("message xmpp delay", 6, server,
82 str, from, from, &t,
83 GINT_TO_POINTER(SEND_TARGET_NICK));
84 g_free(str);
85 } else
86 return;
87 signal_stop();
88 }
89
90 void
91 delay_init(void)
92 {
93 disco_add_feature(XMLNS_DELAY);
94 signal_add_first("xmpp recv message", sig_recv_message);
95 }
96
97 void
98 delay_deinit(void)
99 {
100 signal_remove("xmpp recv message", sig_recv_message);
101 }