1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : mod_carboncopy.erl |
3 |
|
%%% Author : Eric Cestari <ecestari@process-one.net> |
4 |
|
%%% Purpose : Message Carbons XEP-0280 0.8 |
5 |
|
%%% Created : 5 May 2008 by Mickael Remond <mremond@process-one.net> |
6 |
|
%%% Usage : Add `mod_carboncopy` to the `modules` section of mongooseim.toml |
7 |
|
%%% |
8 |
|
%%% |
9 |
|
%%% ejabberd, Copyright (C) 2002-2014 ProcessOne |
10 |
|
%%% |
11 |
|
%%% This program is free software; you can redistribute it and/or |
12 |
|
%%% modify it under the terms of the GNU General Public License as |
13 |
|
%%% published by the Free Software Foundation; either version 2 of the |
14 |
|
%%% License, or (at your option) any later version. |
15 |
|
%%% |
16 |
|
%%% This program is distributed in the hope that it will be useful, |
17 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 |
|
%%% General Public License for more details. |
20 |
|
%%% |
21 |
|
%%% You should have received a copy of the GNU General Public License along |
22 |
|
%%% with this program; if not, write to the Free Software Foundation, Inc., |
23 |
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
24 |
|
%%% |
25 |
|
%%%---------------------------------------------------------------------- |
26 |
|
-module (mod_carboncopy). |
27 |
|
-author ('ecestari@process-one.net'). |
28 |
|
-xep([{xep, 280}, {version, "0.6"}]). |
29 |
|
-xep([{xep, 280}, {version, "0.13.3"}]). |
30 |
|
-behaviour(gen_mod). |
31 |
|
-behaviour(mongoose_module_metrics). |
32 |
|
|
33 |
|
%% API |
34 |
|
-export([start/2, |
35 |
|
stop/1, |
36 |
|
supported_features/0, |
37 |
|
config_spec/0, |
38 |
|
is_carbon_copy/1]). |
39 |
|
|
40 |
|
%% Hooks |
41 |
|
-export([disco_local_features/1, |
42 |
|
user_send_packet/4, |
43 |
|
user_receive_packet/5, |
44 |
|
iq_handler2/5, |
45 |
|
iq_handler1/5, |
46 |
|
remove_connection/5 |
47 |
|
]). |
48 |
|
|
49 |
|
%% Tests |
50 |
|
-export([should_forward/3]). |
51 |
|
|
52 |
|
-ignore_xref([disco_local_features/1, is_carbon_copy/1, remove_connection/5, |
53 |
|
should_forward/3, user_receive_packet/5, user_send_packet/4]). |
54 |
|
|
55 |
|
-define(CC_KEY, 'cc'). |
56 |
|
|
57 |
|
-include("mongoose.hrl"). |
58 |
|
-include("jlib.hrl"). |
59 |
|
-include("session.hrl"). |
60 |
|
-include("mongoose_config_spec.hrl"). |
61 |
|
|
62 |
|
-type direction() :: sent | received. |
63 |
|
|
64 |
146 |
supported_features() -> [dynamic_domains]. |
65 |
|
|
66 |
|
is_carbon_copy(Packet) -> |
67 |
:-( |
case xml:get_subtag(Packet, <<"sent">>) of |
68 |
|
#xmlel{name = <<"sent">>, attrs = AAttrs} -> |
69 |
:-( |
case xml:get_attr_s(<<"xmlns">>, AAttrs) of |
70 |
:-( |
?NS_CC_2 -> true; |
71 |
:-( |
?NS_CC_1 -> true; |
72 |
:-( |
_ -> false |
73 |
|
end; |
74 |
:-( |
_ -> false |
75 |
|
end. |
76 |
|
|
77 |
|
start(HostType, Opts) -> |
78 |
|
%% execute disable/enable actions in the c2s process itself |
79 |
316 |
IQDisc = gen_mod:get_opt(iqdisc, Opts, no_queue), |
80 |
316 |
ejabberd_hooks:add(hooks(HostType)), |
81 |
316 |
gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_CC_2, ejabberd_sm, |
82 |
|
fun ?MODULE:iq_handler2/5, #{}, IQDisc), |
83 |
316 |
gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_CC_1, ejabberd_sm, |
84 |
|
fun ?MODULE:iq_handler1/5, #{}, IQDisc). |
85 |
|
|
86 |
|
stop(HostType) -> |
87 |
316 |
ejabberd_hooks:delete(hooks(HostType)), |
88 |
316 |
gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_CC_1, ejabberd_sm), |
89 |
316 |
gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_CC_2, ejabberd_sm), |
90 |
316 |
ok. |
91 |
|
|
92 |
|
hooks(HostType) -> |
93 |
632 |
[{disco_local_features, HostType, ?MODULE, disco_local_features, 99}, |
94 |
|
{unset_presence_hook, HostType, ?MODULE, remove_connection, 10}, |
95 |
|
{user_send_packet, HostType, ?MODULE, user_send_packet, 89}, |
96 |
|
{user_receive_packet, HostType, ?MODULE, user_receive_packet, 89}]. |
97 |
|
|
98 |
|
-spec config_spec() -> mongoose_config_spec:config_section(). |
99 |
|
config_spec() -> |
100 |
164 |
#section{items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc()}}. |
101 |
|
|
102 |
|
-spec disco_local_features(mongoose_disco:feature_acc()) -> mongoose_disco:feature_acc(). |
103 |
|
disco_local_features(Acc = #{node := <<>>}) -> |
104 |
139 |
mongoose_disco:add_features([?NS_CC_1, ?NS_CC_2, ?NS_CC_RULES], Acc); |
105 |
|
disco_local_features(Acc) -> |
106 |
5 |
Acc. |
107 |
|
|
108 |
|
iq_handler2(Acc, From, _To, IQ, _Extra) -> |
109 |
45 |
iq_handler(Acc, From, IQ, ?NS_CC_2). |
110 |
|
iq_handler1(Acc, From, _To, IQ, _Extra) -> |
111 |
:-( |
iq_handler(Acc, From, IQ, ?NS_CC_1). |
112 |
|
|
113 |
|
iq_handler(Acc, From, #iq{type = set, |
114 |
|
sub_el = #xmlel{name = Operation, |
115 |
|
children = []}} = IQ, CC) -> |
116 |
45 |
?LOG_DEBUG(#{what => cc_iq_received, acc => Acc}), |
117 |
45 |
Result = case Operation of |
118 |
|
<<"enable">> -> |
119 |
44 |
enable(From, CC); |
120 |
|
<<"disable">> -> |
121 |
1 |
disable(From) |
122 |
|
end, |
123 |
45 |
case Result of |
124 |
|
ok -> |
125 |
45 |
?LOG_DEBUG(#{what => cc_iq_result, acc => Acc}), |
126 |
45 |
{Acc, IQ#iq{type = result, sub_el = []}}; |
127 |
|
{error, Reason} -> |
128 |
:-( |
?LOG_WARNING(#{what => cc_iq_failed, acc => Acc, reason => Reason}), |
129 |
:-( |
{Acc, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:not_allowed()]}} |
130 |
|
end; |
131 |
|
|
132 |
|
iq_handler(Acc, _From, IQ, _CC) -> |
133 |
:-( |
{Acc, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:bad_request()]}}. |
134 |
|
|
135 |
|
user_send_packet(Acc, From, To, Packet) -> |
136 |
7242 |
check_and_forward(Acc, From, To, Packet, sent), |
137 |
7242 |
Acc. |
138 |
|
|
139 |
|
user_receive_packet(Acc, JID, _From, To, Packet) -> |
140 |
10288 |
check_and_forward(Acc, JID, To, Packet, received), |
141 |
10288 |
Acc. |
142 |
|
|
143 |
|
remove_connection(Acc, LUser, LServer, LResource, _Status) -> |
144 |
2780 |
JID = jid:make_noprep(LUser, LServer, LResource), |
145 |
2780 |
disable(JID), |
146 |
2780 |
Acc. |
147 |
|
|
148 |
|
% Check if the traffic is local. |
149 |
|
% Modified from original version: |
150 |
|
% - registered to the user_send_packet hook, to be called only once even for multicast |
151 |
|
% - do not support "private" message mode, and do not modify the original packet in any way |
152 |
|
% - we also replicate "read" notifications |
153 |
|
-spec check_and_forward(mongoose_acc:t(), jid:jid(), jid:jid(), exml:element(), direction()) -> ok | stop. |
154 |
|
check_and_forward(Acc, JID, To, #xmlel{name = <<"message">>} = Packet, Direction) -> |
155 |
5232 |
case should_forward(Packet, To, Direction) of |
156 |
1873 |
false -> stop; |
157 |
3359 |
true -> send_copies(Acc, JID, To, Packet, Direction) |
158 |
|
end; |
159 |
12298 |
check_and_forward(_Acc, _JID, _To, _Packet, _) -> ok. |
160 |
|
|
161 |
|
%%%=================================================================== |
162 |
|
%%% Classification |
163 |
|
%%%=================================================================== |
164 |
|
|
165 |
|
-spec should_forward(exml:element(), jid:jid(), direction()) -> boolean(). |
166 |
|
should_forward(Packet, To, Direction) -> |
167 |
5232 |
(not is_carbon_private(Packet)) andalso |
168 |
5230 |
(not has_nocopy_hint(Packet)) andalso |
169 |
5230 |
(not is_received(Packet)) andalso |
170 |
5225 |
(not is_sent(Packet)) andalso |
171 |
5215 |
(is_chat(Packet) orelse is_valid_muc(Packet, To, Direction)). |
172 |
|
|
173 |
|
-spec is_chat(exml:element()) -> boolean(). |
174 |
|
is_chat(Packet) -> |
175 |
5215 |
case exml_query:attr(Packet, <<"type">>, <<"normal">>) of |
176 |
760 |
<<"normal">> -> contains_body(Packet) orelse |
177 |
754 |
contains_receipts(Packet) orelse |
178 |
754 |
contains_csn(Packet); |
179 |
3330 |
<<"chat">> -> true; |
180 |
1125 |
_ -> false |
181 |
|
end. |
182 |
|
|
183 |
|
-spec is_valid_muc(exml:element(), jid:jid(), direction()) -> boolean(). |
184 |
|
is_valid_muc(_, _, sent) -> |
185 |
160 |
false; |
186 |
|
is_valid_muc(Packet, To, _) -> |
187 |
1719 |
is_mediated_invitation(Packet) orelse |
188 |
1708 |
is_direct_muc_invitation(Packet) orelse |
189 |
1703 |
is_received_private_muc(Packet, To). |
190 |
|
|
191 |
|
-spec is_mediated_invitation(exml:element()) -> boolean(). |
192 |
|
is_mediated_invitation(Packet) -> |
193 |
1719 |
undefined =/= exml_query:path(Packet, |
194 |
|
[{element_with_ns, <<"x">>, ?NS_MUC_USER}, |
195 |
|
{element, <<"invite">>}, |
196 |
|
{attr, <<"from">>}]). |
197 |
|
|
198 |
|
-spec is_direct_muc_invitation(exml:element()) -> boolean(). |
199 |
|
is_direct_muc_invitation(Packet) -> |
200 |
1708 |
undefined =/= exml_query:subelement_with_name_and_ns(Packet, <<"x">>, ?NS_CONFERENCE). |
201 |
|
|
202 |
|
-spec is_received_private_muc(exml:element(), jid:jid()) -> boolean(). |
203 |
|
is_received_private_muc(_, #jid{lresource = <<>>}) -> |
204 |
397 |
false; |
205 |
|
is_received_private_muc(Packet, _) -> |
206 |
1306 |
undefined =/= exml_query:subelement_with_name_and_ns(Packet, <<"x">>, ?NS_MUC_USER). |
207 |
|
|
208 |
|
-spec has_nocopy_hint(exml:element()) -> boolean(). |
209 |
|
has_nocopy_hint(Packet) -> |
210 |
5230 |
undefined =/= exml_query:subelement_with_name_and_ns(Packet, <<"no-copy">>, ?NS_HINTS). |
211 |
|
|
212 |
|
-spec contains_body(exml:element()) -> boolean(). |
213 |
|
contains_body(Packet) -> |
214 |
760 |
undefined =/= exml_query:subelement(Packet, <<"body">>). |
215 |
|
|
216 |
|
-spec contains_receipts(exml:element()) -> boolean(). |
217 |
|
contains_receipts(Packet) -> |
218 |
754 |
undefined =/= exml_query:subelement_with_name_and_ns(Packet, <<"received">>, ?NS_RECEIPTS). |
219 |
|
|
220 |
|
-spec contains_csn(exml:element()) -> boolean(). |
221 |
|
contains_csn(Packet) -> |
222 |
754 |
undefined =/= exml_query:subelement_with_ns(Packet, ?NS_CHATSTATES). |
223 |
|
|
224 |
|
-spec is_carbon_private(exml:element()) -> boolean(). |
225 |
|
is_carbon_private(Packet) -> |
226 |
5232 |
[] =/= subelements_with_nss(Packet, <<"private">>, carbon_namespaces()). |
227 |
|
|
228 |
|
-spec is_received(exml:element()) -> boolean(). |
229 |
|
is_received(Packet) -> |
230 |
5230 |
[] =/= subelements_with_nss(Packet, <<"received">>, carbon_namespaces()). |
231 |
|
|
232 |
|
-spec is_sent(exml:element()) -> boolean(). |
233 |
|
is_sent(Packet) -> |
234 |
5225 |
[] =/= subelements_with_nss(Packet, <<"sent">>, carbon_namespaces()). |
235 |
|
|
236 |
|
-spec subelements_with_nss(exml:element(), binary(), [binary()]) -> [exml:element()]. |
237 |
|
subelements_with_nss(#xmlel{children = Children}, Name, NSS) -> |
238 |
15687 |
lists:filter(fun(#xmlel{name = N} = Child) when N =:= Name -> |
239 |
33 |
NS = exml_query:attr(Child, <<"xmlns">>), |
240 |
33 |
lists:member(NS, NSS); |
241 |
|
(_) -> |
242 |
19538 |
false |
243 |
|
end, Children). |
244 |
|
|
245 |
15687 |
carbon_namespaces() -> [?NS_CC_1, ?NS_CC_2]. |
246 |
|
|
247 |
|
%%%=================================================================== |
248 |
|
%%% Internal |
249 |
|
%%%=================================================================== |
250 |
|
|
251 |
|
|
252 |
|
%% |
253 |
|
%% Internal |
254 |
|
%% |
255 |
|
is_bare_to(Direction, To, _PrioRes) -> |
256 |
3359 |
case {Direction, To} of |
257 |
280 |
{received, #jid{lresource = <<>>}} -> true; |
258 |
3079 |
_ -> false |
259 |
|
end. |
260 |
|
|
261 |
|
max_prio(PrioRes) -> |
262 |
48 |
case catch lists:max(PrioRes) of |
263 |
48 |
{Prio, _Res} -> Prio; |
264 |
:-( |
_ -> 0 |
265 |
|
end. |
266 |
|
|
267 |
|
is_max_prio(Res, PrioRes) -> |
268 |
48 |
lists:member({max_prio(PrioRes), Res}, PrioRes). |
269 |
|
|
270 |
|
jids_minus_max_priority_resource(JID, CCResList, PrioRes) -> |
271 |
280 |
[ {jid:replace_resource(JID, CCRes), CCVersion} |
272 |
280 |
|| {CCVersion, CCRes} <- CCResList, not is_max_prio(CCRes, PrioRes) ]. |
273 |
|
|
274 |
|
jids_minus_specific_resource(JID, R, CCResList, _PrioRes) -> |
275 |
3079 |
[ {jid:replace_resource(JID, CCRes), CCVersion} |
276 |
3079 |
|| {CCVersion, CCRes} <- CCResList, CCRes =/= R ]. |
277 |
|
|
278 |
|
%% Direction = received | sent <received xmlns='urn:xmpp:carbons:1'/> |
279 |
|
send_copies(Acc, JID, To, Packet, Direction) -> |
280 |
3359 |
#jid{lresource = R} = JID, |
281 |
3359 |
{PrioRes, CCResList} = get_cc_enabled_resources(JID), |
282 |
3359 |
Targets = case is_bare_to(Direction, To, PrioRes) of |
283 |
280 |
true -> jids_minus_max_priority_resource |
284 |
|
(JID, CCResList, PrioRes); |
285 |
3079 |
_ -> jids_minus_specific_resource(JID, R, CCResList, PrioRes) |
286 |
|
end, |
287 |
3359 |
?LOG_DEBUG(#{what => cc_send_copies, |
288 |
3359 |
targets => Targets, resources => PrioRes, ccenabled => CCResList}), |
289 |
3359 |
lists:foreach(fun({Dest, Version}) -> |
290 |
15 |
?LOG_DEBUG(#{what => cc_forwarding, |
291 |
|
user => JID#jid.luser, server => JID#jid.lserver, |
292 |
15 |
resource => JID#jid.lresource, exml_packet => Packet}), |
293 |
15 |
Sender = jid:to_bare(JID), |
294 |
15 |
New = build_forward_packet(Acc, JID, Packet, Sender, Dest, Direction, Version), |
295 |
15 |
ejabberd_router:route(Sender, Dest, Acc, New) |
296 |
|
end, Targets). |
297 |
|
|
298 |
|
build_forward_packet(Acc, JID, Packet, Sender, Dest, Direction, Version) -> |
299 |
|
% The wrapping message SHOULD maintain the same 'type' attribute value; |
300 |
15 |
Type = exml_query:attr(Packet, <<"type">>, <<"normal">>), |
301 |
15 |
#xmlel{name = <<"message">>, |
302 |
|
attrs = [{<<"xmlns">>, <<"jabber:client">>}, |
303 |
|
{<<"type">>, Type}, |
304 |
|
{<<"from">>, jid:to_binary(Sender)}, |
305 |
|
{<<"to">>, jid:to_binary(Dest)}], |
306 |
|
children = carbon_copy_children(Acc, Version, JID, Packet, Direction)}. |
307 |
|
|
308 |
|
carbon_copy_children(Acc, ?NS_CC_1, JID, Packet, Direction) -> |
309 |
:-( |
[ #xmlel{name = atom_to_binary(Direction, utf8), |
310 |
|
attrs = [{<<"xmlns">>, ?NS_CC_1}]}, |
311 |
|
#xmlel{name = <<"forwarded">>, |
312 |
|
attrs = [{<<"xmlns">>, ?NS_FORWARD}], |
313 |
|
children = [complete_packet(Acc, JID, Packet, Direction)]} ]; |
314 |
|
carbon_copy_children(Acc, ?NS_CC_2, JID, Packet, Direction) -> |
315 |
15 |
[ #xmlel{name = atom_to_binary(Direction, utf8), |
316 |
|
attrs = [{<<"xmlns">>, ?NS_CC_2}], |
317 |
|
children = [ #xmlel{name = <<"forwarded">>, |
318 |
|
attrs = [{<<"xmlns">>, ?NS_FORWARD}], |
319 |
|
children = [complete_packet(Acc, JID, Packet, Direction)]} ]} ]. |
320 |
|
|
321 |
|
enable(JID, CC) -> |
322 |
44 |
?LOG_INFO(#{what => cc_enable, |
323 |
44 |
user => JID#jid.luser, server => JID#jid.lserver}), |
324 |
44 |
case ejabberd_sm:store_info(JID, ?CC_KEY, cc_ver_to_int(CC)) of |
325 |
44 |
{ok, ?CC_KEY} -> ok; |
326 |
:-( |
{error, _} = Err -> Err |
327 |
|
end. |
328 |
|
|
329 |
|
disable(JID) -> |
330 |
2781 |
?LOG_INFO(#{what => cc_disable, |
331 |
2781 |
user => JID#jid.luser, server => JID#jid.lserver}), |
332 |
2781 |
case ejabberd_sm:remove_info(JID, ?CC_KEY) of |
333 |
206 |
ok -> ok; |
334 |
2575 |
{error, offline} -> ok |
335 |
|
end. |
336 |
|
|
337 |
|
complete_packet(Acc, From, #xmlel{name = <<"message">>, attrs = OrigAttrs} = Packet, sent) -> |
338 |
|
%% if this is a packet sent by user on this host, then Packet doesn't |
339 |
|
%% include the 'from' attribute. We must add it. |
340 |
10 |
Attrs = lists:keystore(<<"xmlns">>, 1, OrigAttrs, {<<"xmlns">>, <<"jabber:client">>}), |
341 |
10 |
Packet2 = set_stanza_id(Acc, From, Packet), |
342 |
10 |
case proplists:get_value(<<"from">>, Attrs) of |
343 |
|
undefined -> |
344 |
10 |
Packet2#xmlel{attrs = [{<<"from">>, jid:to_binary(From)} | Attrs]}; |
345 |
|
_ -> |
346 |
:-( |
Packet2#xmlel{attrs = Attrs} |
347 |
|
end; |
348 |
|
|
349 |
|
complete_packet(_Acc, _From, #xmlel{name = <<"message">>, attrs = OrigAttrs} = Packet, received) -> |
350 |
5 |
Attrs = lists:keystore(<<"xmlns">>, 1, OrigAttrs, {<<"xmlns">>, <<"jabber:client">>}), |
351 |
5 |
Packet#xmlel{attrs = Attrs}. |
352 |
|
|
353 |
|
get_cc_enabled_resources(JID) -> |
354 |
3359 |
AllSessions = ejabberd_sm:get_raw_sessions(JID), |
355 |
3359 |
CCs = filter_cc_enabled_resources(AllSessions), |
356 |
3359 |
Prios = filter_priority_resources(AllSessions), |
357 |
3359 |
{Prios, CCs}. |
358 |
|
|
359 |
|
filter_cc_enabled_resources(AllSessions) -> |
360 |
3359 |
lists:filtermap(fun fun_filter_cc_enabled_resource/1, AllSessions). |
361 |
|
|
362 |
|
fun_filter_cc_enabled_resource(Session = #session{usr = {_, _, R}}) -> |
363 |
3739 |
case mongoose_session:get_info(Session, ?CC_KEY, undefined) of |
364 |
|
{?CC_KEY, V} when is_integer(V) -> |
365 |
78 |
{true, {cc_ver_from_int(V), R}}; |
366 |
|
_ -> |
367 |
3661 |
false |
368 |
|
end. |
369 |
|
|
370 |
|
filter_priority_resources(AllSessions) -> |
371 |
3359 |
lists:filtermap(fun fun_filter_priority_resources/1, AllSessions). |
372 |
|
|
373 |
|
fun_filter_priority_resources(#session{usr = {_, _, R}, priority = P}) |
374 |
|
when is_integer(P) -> |
375 |
3671 |
{true, {P, R}}; |
376 |
|
fun_filter_priority_resources(_) -> |
377 |
68 |
false. |
378 |
|
|
379 |
:-( |
cc_ver_to_int(?NS_CC_1) -> 1; |
380 |
44 |
cc_ver_to_int(?NS_CC_2) -> 2. |
381 |
|
|
382 |
:-( |
cc_ver_from_int(1) -> ?NS_CC_1; |
383 |
78 |
cc_ver_from_int(2) -> ?NS_CC_2. |
384 |
|
|
385 |
|
%% Servers SHOULD include the element as a child |
386 |
|
%% of the forwarded message when using Message Carbons (XEP-0280) |
387 |
|
%% https://xmpp.org/extensions/xep-0313.html#archives_id |
388 |
|
set_stanza_id(Acc, From, Packet) -> |
389 |
10 |
MamId = mongoose_acc:get(mam, mam_id, undefined, Acc), |
390 |
10 |
set_stanza_id(MamId, From, Acc, Packet). |
391 |
|
|
392 |
|
set_stanza_id(undefined, _From, _Acc, Packet) -> |
393 |
9 |
Packet; |
394 |
|
set_stanza_id(MamId, From, _Acc, Packet) -> |
395 |
1 |
By = jid:to_binary(jid:to_bare(From)), |
396 |
1 |
mod_mam_utils:replace_arcid_elem(<<"stanza-id">>, By, MamId, Packet). |