From 3325e69ae6d625968ce8ec09d9100b5eeca9fb28 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Tue, 22 Nov 2016 22:21:34 +0100 Subject: [PATCH] Let mod_carboncopy mark copied messages Carbon copies are now marked with a 'carbon_copy' flag. This makes it easier to identify them. --- src/ejabberd_c2s.erl | 44 ++++++++++++++---------------------------- src/mod_carboncopy.erl | 10 ++++++---- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 7ef708d31..6d84d8d93 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -2645,44 +2645,30 @@ handle_unacked_stanzas(#state{mgmt_state = MgmtState} = StateData) Txt = <<"User session terminated">>, ejabberd_router:route_error( To, From, El, xmpp:err_service_unavailable(Txt, Lang)); + (From, _To, #message{meta = #{carbon_copy := true}}, _Time) -> + %% XEP-0280 says: "When a receiving server attempts to deliver a + %% forked message, and that message bounces with an error for + %% any reason, the receiving server MUST NOT forward that error + %% back to the original sender." Resending such a stanza could + %% easily lead to unexpected results as well. + ?DEBUG("Dropping forwarded message stanza from ~s", + [jid:to_string(From)]); (From, To, El, Time) -> - %% We'll drop the stanza if it was by some - %% encapsulating protocol as per XEP-0297. One such protocol is - %% XEP-0280, which says: "When a receiving server attempts to - %% deliver a forked message, and that message bounces with an - %% error for any reason, the receiving server MUST NOT forward - %% that error back to the original sender." Resending such a - %% stanza could easily lead to unexpected results as well. - case is_encapsulated_forward(El) of + case ejabberd_hooks:run_fold(message_is_archived, + StateData#state.server, false, + [StateData, From, + StateData#state.jid, El]) of true -> - ?DEBUG("Dropping forwarded message stanza from ~s", - [jid:to_string(From)]); + ?DEBUG("Dropping archived message stanza from ~p", + [jid:to_string(xmpp:get_from(El))]); false -> - case ejabberd_hooks:run_fold(message_is_archived, - StateData#state.server, - false, - [StateData, From, - StateData#state.jid, El]) of - true -> - ?DEBUG("Dropping archived message stanza from ~p", - [jid:to_string(xmpp:get_from(El))]); - false -> - ReRoute(From, To, El, Time) - end + ReRoute(From, To, El, Time) end end, handle_unacked_stanzas(StateData, F); handle_unacked_stanzas(_StateData) -> ok. --spec is_encapsulated_forward(stanza()) -> boolean(). -is_encapsulated_forward(#message{} = Msg) -> - xmpp:has_subtag(Msg, #forwarded{}) orelse - xmpp:has_subtag(Msg, #carbons_sent{}) orelse - xmpp:has_subtag(Msg, #carbons_received{}); -is_encapsulated_forward(_El) -> - false. - -spec inherit_session_state(state(), binary()) -> {ok, state()} | {error, binary()} | {error, binary(), non_neg_integer()}. diff --git a/src/mod_carboncopy.erl b/src/mod_carboncopy.erl index f1eb3e790..5839a65b2 100644 --- a/src/mod_carboncopy.erl +++ b/src/mod_carboncopy.erl @@ -52,9 +52,10 @@ -callback list(binary(), binary()) -> [{binary(), binary()}]. -spec is_carbon_copy(stanza()) -> boolean(). -is_carbon_copy(Packet) -> - xmpp:has_subtag(Packet, #carbons_sent{}) orelse - xmpp:has_subtag(Packet, #carbons_received{}). +is_carbon_copy(#message{meta = #{carbon_copy := true}}) -> + true; +is_carbon_copy(_) -> + false. start(Host, Opts) -> IQDisc = gen_mod:get_opt(iqdisc, Opts,fun gen_iq_handler:check_type/1, one_queue), @@ -203,7 +204,8 @@ build_forward_packet(JID, #message{type = T} = Msg, Sender, Dest, Direction) -> sent -> #carbons_sent{forwarded = Forwarded}; received -> #carbons_received{forwarded = Forwarded} end, - #message{from = Sender, to = Dest, type = T, sub_els = [Carbon]}. + #message{from = Sender, to = Dest, type = T, sub_els = [Carbon], + meta = #{carbon_copy => true}}. -spec enable(binary(), binary(), binary(), binary()) -> ok | {error, any()}. enable(Host, U, R, CC)->