From a8f6c1db1dd21f3bee104867e9f81237b8696086 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Sun, 12 Jan 2020 13:42:00 +0100 Subject: [PATCH] mod_carboncopy: Support new copying rules Support the copying rules added to XEP-0280 with version 0.13.0 under the "urn:xmpp:carbons:rules:0" namespace. Closes #3011. --- rebar.config | 2 +- src/mod_carboncopy.erl | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/rebar.config b/rebar.config index af3ac2b35..71cdc0072 100644 --- a/rebar.config +++ b/rebar.config @@ -25,7 +25,7 @@ {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.18"}}}, {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.38"}}}, {idna, ".*", {git, "https://github.com/benoitc/erlang-idna", {tag, "6.0.0"}}}, - {xmpp, ".*", {git, "https://github.com/processone/xmpp", {tag, "1.4.3"}}}, + {xmpp, ".*", {git, "https://github.com/processone/xmpp", "f98a50eb4135"}}, {fast_yaml, ".*", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.22"}}}, {yconf, ".*", {git, "https://github.com/processone/yconf", {tag, "1.0.2"}}}, {jiffy, ".*", {git, "https://github.com/davisp/jiffy", {tag, "0.14.8"}}}, diff --git a/src/mod_carboncopy.erl b/src/mod_carboncopy.erl index be30791dc..2377d04c1 100644 --- a/src/mod_carboncopy.erl +++ b/src/mod_carboncopy.erl @@ -81,12 +81,10 @@ reload(_Host, _NewOpts, _OldOpts) -> -spec disco_features({error, stanza_error()} | {result, [binary()]} | empty, jid(), jid(), binary(), binary()) -> {error, stanza_error()} | {result, [binary()]}. -disco_features({error, Err}, _From, _To, _Node, _Lang) -> - {error, Err}; -disco_features(empty, _From, _To, <<"">>, _Lang) -> - {result, [?NS_CARBONS_2]}; +disco_features(empty, From, To, <<"">>, Lang) -> + disco_features({result, []}, From, To, <<"">>, Lang); disco_features({result, Feats}, _From, _To, <<"">>, _Lang) -> - {result, [?NS_CARBONS_2|Feats]}; + {result, [?NS_CARBONS_2,?NS_CARBONS_RULES_0|Feats]}; disco_features(Acc, _From, _To, _Node, _Lang) -> Acc. @@ -162,7 +160,8 @@ c2s_session_opened(State) -> -spec check_and_forward(jid(), jid(), stanza(), direction()) -> stanza() | {stop, stanza()}. check_and_forward(JID, To, Packet, Direction)-> - case is_chat_message(Packet) andalso + case (is_chat_message(Packet) orelse + is_received_muc_invite(Packet, Direction)) andalso not is_received_muc_pm(To, Packet, Direction) andalso not xmpp:has_subtag(Packet, #carbons_private{}) andalso not xmpp:has_subtag(Packet, #hint{type = 'no-copy'}) of @@ -275,9 +274,18 @@ is_chat_message(#message{type = chat}) -> true; is_chat_message(#message{type = normal, body = [_|_]}) -> true; +is_chat_message(#message{type = Type} = Msg) when Type == chat; + Type == normal -> + has_chatstate(Msg) orelse xmpp:has_subtag(Msg, #receipt_response{}); is_chat_message(_) -> false. +-spec is_received_muc_invite(message(), direction()) -> boolean(). +is_received_muc_invite(_Packet, sent) -> + false; +is_received_muc_invite(Packet, received) -> + xmpp:has_subtag(Packet, #x_conference{}). + -spec is_received_muc_pm(jid(), message(), direction()) -> boolean(). is_received_muc_pm(#jid{lresource = <<>>}, _Packet, _Direction) -> false; @@ -286,6 +294,10 @@ is_received_muc_pm(_To, _Packet, sent) -> is_received_muc_pm(_To, Packet, received) -> xmpp:has_subtag(Packet, #muc_user{}). +-spec has_chatstate(message()) -> boolean(). +has_chatstate(#message{sub_els = Els}) -> + lists:any(fun(El) -> xmpp:get_ns(El) == ?NS_CHATSTATES end, Els). + -spec list(binary(), binary()) -> [{Resource :: binary(), Namespace :: binary()}]. list(User, Server) -> lists:filtermap(