Debian Codegraph @

src/core/xep/carbons.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * Copyright (C) 2017 Stephen Paul Weber
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 #include "module.h"
19 #include "misc.h"
20 #include "settings.h"
21 #include "signals.h"
22
23 #include "rosters-tools.h"
24 #include "xmpp-servers.h"
25 #include "disco.h"
26 #include "tools.h"
27 #include "muc.h"
28
29 #define XMLNS_CARBONS "urn:xmpp:carbons:2"
30
31 static void
32 sig_server_features(XMPP_SERVER_REC *server)
33 {
34 if (disco_have_feature(server->server_features, XMLNS_CARBONS)) {
35 LmMessage *lmsg;
36 LmMessageNode *node;
37
38 lmsg = lm_message_new_with_sub_type(
39 NULL,
40 LM_MESSAGE_TYPE_IQ,
41 LM_MESSAGE_SUB_TYPE_SET);
42 node = lm_message_node_add_child(lmsg->node, "enable", NULL);
43 lm_message_node_set_attribute(node, XMLNS, XMLNS_CARBONS);
44 signal_emit("xmpp send iq", 2, server, lmsg);
45 lm_message_unref(lmsg);
46 }
47 }
48
49 static void
50 sig_recv_message(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
51 const char *id, const char *from, const char *to)
52 {
53 LmMessageNode *node = NULL;
54 LmMessageNode *forwarded = NULL;
55 LmMessageNode *message = NULL;
56 char *ffrom, *fto;
57
58 node = lm_find_node(lmsg->node, "received", "xmlns", XMLNS_CARBONS);
59 if (node != NULL) {
60 forwarded = lm_find_node(node, "forwarded", "xmlns", "urn:xmpp:forward:0");
61 if (forwarded != NULL) {
62 message = lm_find_node(forwarded, "message", "xmlns", "jabber:client");
63 }
64 if (message != NULL) {
65 ffrom = xmpp_recode_in(lm_message_node_get_attribute(message, "from"));
66 if (ffrom == NULL) {
67 ffrom = g_strdup("");
68 }
69 fto = xmpp_recode_in(lm_message_node_get_attribute(message, "to"));
70 if (fto == NULL) {
71 fto = g_strdup("");
72 }
73
74 node = lmsg->node;
75 lmsg->node = message;
76 signal_emit("xmpp recv message", 6, server, lmsg, type, id, ffrom, fto);
77 lmsg->node = node;
78 goto done;
79 }
80 }
81
82 node = lm_find_node(lmsg->node, "sent", "xmlns", XMLNS_CARBONS);
83 if (node == NULL) {
84 return; // Not for us
85 }
86
87 forwarded = lm_find_node(node, "forwarded", "xmlns", "urn:xmpp:forward:0");
88 if (forwarded != NULL) {
89 message = lm_find_node(forwarded, "message", "xmlns", "jabber:client");
90 }
91 if (message != NULL) {
92 MUC_REC *channel;
93 char *nick, *str;
94 node = lm_message_node_get_child(message, "body");
95 if (node == NULL || node->value == NULL || *node->value == '\0') {
96 return;
97 }
98 str = xmpp_recode_in(node->value);
99
100 ffrom = xmpp_recode_in(lm_message_node_get_attribute(message, "from"));
101 if (ffrom == NULL) {
102 ffrom = g_strdup("");
103 }
104 fto = xmpp_recode_in(lm_message_node_get_attribute(message, "to"));
105 if (fto == NULL) {
106 fto = g_strdup("");
107 }
108
109 nick = rosters_resolve_name(server, fto);
110 if (nick != NULL) {
111 g_free(fto);
112 fto = nick;
113 }
114
115 if (type == LM_MESSAGE_SUB_TYPE_GROUPCHAT
116 && (channel = get_muc(server, fto)) != NULL
117 && (nick = muc_extract_nick(ffrom)) != NULL) {
118 signal_emit("message xmpp carbons sent", 6, server,
119 str, nick, channel->name,
120 GINT_TO_POINTER(SEND_TARGET_CHANNEL));
121 g_free(nick);
122 } else if ((type == LM_MESSAGE_SUB_TYPE_NOT_SET
123 || type == LM_MESSAGE_SUB_TYPE_HEADLINE
124 || type == LM_MESSAGE_SUB_TYPE_NORMAL
125 || type == LM_MESSAGE_SUB_TYPE_CHAT)) {
126 signal_emit("message xmpp carbons sent", 6, server,
127 str, ffrom, fto,
128 GINT_TO_POINTER(SEND_TARGET_NICK));
129 }
130
131 g_free(str);
132 }
133
134 done:
135 g_free(fto);
136 g_free(ffrom);
137 signal_stop();
138 }
139
140 void
141 carbons_init(void)
142 {
143 signal_add("xmpp server features", sig_server_features);
144 signal_add_first("xmpp recv message", sig_recv_message);
145 }
146
147 void
148 carbons_deinit(void)
149 {
150 signal_remove("xmpp server features", sig_server_features);
151 signal_remove("xmpp recv message", sig_recv_message);
152 }