Package: irssi-plugin-xmpp / 0.54-3

singpolyma-0203-Implement-XEP-0280-Message-Carbons.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
From 563b255bafa981c357a6cde940b177342b8032b5 Mon Sep 17 00:00:00 2001
From: Stephen Paul Weber <singpolyma@singpolyma.net>
Date: Wed, 18 Nov 2015 13:25:59 -0500
Subject: [PATCH 3/3] Implement XEP-0280: Message Carbons

---
 src/core/Makefile              |   1 +
 src/core/xep/carbons.c         | 122 +++++++++++++++++++++++++++++++++++++++++
 src/core/xep/carbons.h         |   9 +++
 src/core/xep/xep.c             |   3 +
 src/fe-common/Makefile         |   1 +
 src/fe-common/xep/fe-carbons.c |  63 +++++++++++++++++++++
 src/fe-common/xep/fe-carbons.h |   9 +++
 src/fe-common/xep/fe-xep.c     |   3 +
 8 files changed, 211 insertions(+)
 create mode 100644 src/core/xep/carbons.c
 create mode 100644 src/core/xep/carbons.h
 create mode 100644 src/fe-common/xep/fe-carbons.c
 create mode 100644 src/fe-common/xep/fe-carbons.h

diff --git a/src/core/Makefile b/src/core/Makefile
index ca885fe..ac74b3b 100644
--- a/src/core/Makefile
+++ b/src/core/Makefile
@@ -12,6 +12,7 @@ SRCS=	xmpp-commands.c \
 	stanzas.c \
 	tools.c \
 	popenRWE.c \
+	xep/carbons.c \
 	xep/chatstates.c \
 	xep/composing.c \
 	xep/datetime.c \
diff --git a/src/core/xep/carbons.c b/src/core/xep/carbons.c
new file mode 100644
index 0000000..d652776
--- /dev/null
+++ b/src/core/xep/carbons.c
@@ -0,0 +1,122 @@
+/*
+ * XEP-0280: Message Carbons
+ */
+
+#include "module.h"
+#include "misc.h"
+#include "settings.h"
+#include "signals.h"
+
+#include "rosters-tools.h"
+#include "xmpp-servers.h"
+#include "disco.h"
+#include "tools.h"
+#include "muc.h"
+
+#define XMLNS_CARBONS "urn:xmpp:carbons:2"
+
+static void
+sig_server_features(XMPP_SERVER_REC *server)
+{
+	if (disco_have_feature(server->server_features, XMLNS_CARBONS)) {
+		LmMessage *lmsg;
+		LmMessageNode *node;
+
+		lmsg = lm_message_new_with_sub_type(NULL,
+				 LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
+		node = lm_message_node_add_child(lmsg->node, "enable", NULL);
+		lm_message_node_set_attribute(node, XMLNS, XMLNS_CARBONS);
+		signal_emit("xmpp send iq", 2, server, lmsg);
+		lm_message_unref(lmsg);
+	}
+}
+
+static void
+sig_recv_message(XMPP_SERVER_REC *server, LmMessage *lmsg, const int type,
+    const char *id, const char *from, const char *to)
+{
+	LmMessageNode *node = NULL;
+	LmMessageNode *forwarded = NULL;
+	LmMessageNode *message = NULL;
+	char *ffrom, *fto;
+
+	node = lm_find_node(lmsg->node, "received", "xmlns", XMLNS_CARBONS);
+	if(node) {
+		forwarded = lm_find_node(node, "forwarded", "xmlns", "urn:xmpp:forward:0");
+		if(forwarded) message = lm_find_node(forwarded, "message", "xmlns", "jabber:client");
+		if(message) {
+			ffrom = xmpp_recode_in(lm_message_node_get_attribute(message, "from"));
+			if (from == NULL)
+				from = g_strdup("");
+			fto = xmpp_recode_in(lm_message_node_get_attribute(message, "to"));
+			if (to == NULL)
+				to = g_strdup("");
+
+			node = lmsg->node;
+			lmsg->node = message;
+			signal_emit("xmpp recv message", 6, server, lmsg, type, id, ffrom, fto);
+			lmsg->node = node;
+			goto done;
+		}
+	}
+
+	node = lm_find_node(lmsg->node, "sent", "xmlns", XMLNS_CARBONS);
+	if(!node) return; // Not for us
+
+	forwarded = lm_find_node(node, "forwarded", "xmlns", "urn:xmpp:forward:0");
+	if(forwarded) message = lm_find_node(forwarded, "message", "xmlns", "jabber:client");
+	if(message) {
+		MUC_REC *channel;
+		char *nick, *str;
+		node = lm_message_node_get_child(message, "body");
+		if(node == NULL || node->value == NULL || *node->value == '\0') return;
+		str = xmpp_recode_in(node->value);
+
+		ffrom = xmpp_recode_in(lm_message_node_get_attribute(message, "from"));
+		if (from == NULL)
+			from = g_strdup("");
+		fto = xmpp_recode_in(lm_message_node_get_attribute(message, "to"));
+		if (to == NULL)
+			to = g_strdup("");
+
+		nick = rosters_resolve_name(server, fto);
+		if(nick) fto = nick;
+
+		if (type == LM_MESSAGE_SUB_TYPE_GROUPCHAT
+		    && (channel = get_muc(server, fto)) != NULL
+		    && (nick = muc_extract_nick(ffrom)) != NULL) {
+			signal_emit("message xmpp carbons sent", 6, server,
+			            str, nick, channel->name,
+			            GINT_TO_POINTER(SEND_TARGET_CHANNEL));
+			g_free(nick);
+		} else if ((type == LM_MESSAGE_SUB_TYPE_NOT_SET
+		           || type == LM_MESSAGE_SUB_TYPE_HEADLINE
+		           || type == LM_MESSAGE_SUB_TYPE_NORMAL
+		           || type == LM_MESSAGE_SUB_TYPE_CHAT)) {
+			signal_emit("message xmpp carbons sent", 6, server,
+			            str, ffrom, fto,
+			            GINT_TO_POINTER(SEND_TARGET_NICK));
+		}
+
+		g_free(str);
+	}
+
+done:
+	g_free(fto);
+	g_free(ffrom);
+	signal_stop();
+}
+
+void
+carbons_init(void)
+{
+	signal_add("xmpp server features", sig_server_features);
+	signal_add_first("xmpp recv message", sig_recv_message);
+}
+
+void
+carbons_deinit(void)
+{
+	signal_remove("xmpp server features", sig_server_features);
+	signal_remove("xmpp recv message", sig_recv_message);
+}
diff --git a/src/core/xep/carbons.h b/src/core/xep/carbons.h
new file mode 100644
index 0000000..3380bc1
--- /dev/null
+++ b/src/core/xep/carbons.h
@@ -0,0 +1,9 @@
+#ifndef __CARBONS_H
+#define __CARBONS_H
+
+__BEGIN_DECLS
+void carbons_init(void);
+void carbons_deinit(void);
+__END_DECLS
+
+#endif
diff --git a/src/core/xep/xep.c b/src/core/xep/xep.c
index 8c7bcdf..a47cddd 100644
--- a/src/core/xep/xep.c
+++ b/src/core/xep/xep.c
@@ -17,6 +17,7 @@
 
 #include "module.h"
 
+#include "carbons.h"
 #include "chatstates.h"
 #include "composing.h"
 #include "delay.h"
@@ -32,6 +33,7 @@ void
 xep_init(void)
 {
 	disco_init(); /* init sevice discovery first */
+	carbons_init();
 	chatstates_init();
 	composing_init();
 	delay_init();
@@ -47,6 +49,7 @@ void
 xep_deinit(void)
 {
 	disco_deinit();
+	carbons_deinit();
 	chatstates_deinit();
 	composing_deinit();
 	delay_deinit();
diff --git a/src/fe-common/Makefile b/src/fe-common/Makefile
index 487d125..c84ccdd 100644
--- a/src/fe-common/Makefile
+++ b/src/fe-common/Makefile
@@ -9,6 +9,7 @@ SRCS=	fe-xmpp-messages.c \
 	module-formats.c \
 	xmpp-completion.c \
 	xmpp-formats.c \
+	xep/fe-carbons.c \
 	xep/fe-composing.c \
 	xep/fe-delay.c \
 	xep/fe-muc.c \
diff --git a/src/fe-common/xep/fe-carbons.c b/src/fe-common/xep/fe-carbons.c
new file mode 100644
index 0000000..fe95496
--- /dev/null
+++ b/src/fe-common/xep/fe-carbons.c
@@ -0,0 +1,63 @@
+#include "module.h"
+#include "levels.h"
+#include "module-formats.h"
+#include "printtext.h"
+#include "settings.h"
+#include "signals.h"
+#include "window-items.h"
+#include "fe-messages.h"
+#include "fe-queries.h"
+#include "fe-common/core/module-formats.h"
+#include "fe-common/core/fe-messages.h"
+#include "fe-common/irc/module-formats.h"
+
+#include "xmpp-servers.h"
+#include "rosters-tools.h"
+#include "xep/muc.h"
+
+static void
+sig_message_carbons_sent(SERVER_REC *server, const char *msg, const char *nick,
+                         const char *target, gpointer gpointer_type)
+{
+	void *item;
+	char *freemsg = NULL;
+	int level, type;
+
+	g_return_if_fail(server != NULL);
+	g_return_if_fail(msg != NULL);
+	g_return_if_fail(nick != NULL);
+	g_return_if_fail(target != NULL);
+
+	type = GPOINTER_TO_INT(gpointer_type);
+	level = MSGLEVEL_NO_ACT | MSGLEVEL_NOHILIGHT
+	    | (type == SEND_TARGET_CHANNEL ? MSGLEVEL_PUBLIC : MSGLEVEL_MSGS);
+	item = type == SEND_TARGET_CHANNEL ?
+	    (void *)get_muc((XMPP_SERVER_REC *)server, target) :
+	    query_find(server, target);
+
+	if (settings_get_bool("emphasis"))
+		msg = freemsg = expand_emphasis(item, msg);
+
+	if (type == SEND_TARGET_CHANNEL) {
+		char *nickmode = channel_get_nickmode(item, nick);
+		printformat_module(CORE_MODULE_NAME, server, target,
+		    level, TXT_OWN_MSG_CHANNEL, nick, target, msg, nickmode);
+	} else if(item) { // If no query, why do we want to see carbons?
+		printformat_module(CORE_MODULE_NAME, server, target,
+		    level, TXT_OWN_MSG_PRIVATE_QUERY, target, msg, nick);
+	}
+
+	g_free_not_null(freemsg);
+}
+
+void
+fe_carbons_init(void)
+{
+	signal_add("message xmpp carbons sent", sig_message_carbons_sent);
+}
+
+void
+fe_carbons_deinit(void)
+{
+	signal_remove("message xmpp carbons sent", sig_message_carbons_sent);
+}
diff --git a/src/fe-common/xep/fe-carbons.h b/src/fe-common/xep/fe-carbons.h
new file mode 100644
index 0000000..ee8ba85
--- /dev/null
+++ b/src/fe-common/xep/fe-carbons.h
@@ -0,0 +1,9 @@
+#ifndef __FE_CARBONS_H
+#define __FE_CARBONS_H
+
+__BEGIN_DECLS
+void fe_carbons_init(void);
+void fe_carbons_deinit(void);
+__END_DECLS
+
+#endif
diff --git a/src/fe-common/xep/fe-xep.c b/src/fe-common/xep/fe-xep.c
index 605922b..46069f3 100644
--- a/src/fe-common/xep/fe-xep.c
+++ b/src/fe-common/xep/fe-xep.c
@@ -17,6 +17,7 @@
 
 #include "module.h"
 
+#include "fe-carbons.h"
 #include "fe-composing.h"
 #include "fe-delay.h"
 #include "fe-muc.h"
@@ -28,6 +29,7 @@
 void
 fe_xep_init(void)
 {
+	fe_carbons_init();
 	fe_composing_init();
 	fe_delay_init();
 	fe_muc_init();
@@ -40,6 +42,7 @@ fe_xep_init(void)
 void
 fe_xep_deinit(void)
 {
+	fe_carbons_deinit();
 	fe_composing_deinit();
 	fe_delay_deinit();
 	fe_muc_deinit();
-- 
2.6.4