LServer = To#jid.lserver,
PrioRes = get_user_present_resources(LUser, LServer),
case catch lists:max(PrioRes) of
- {Priority, _R}
- when is_integer(Priority), Priority >= 0 ->
- lists:foreach(fun ({P, R}) when P == Priority;
+ {MaxPrio, MaxRes}
+ when is_integer(MaxPrio), MaxPrio >= 0 ->
+ lists:foreach(fun ({P, R}) when P == MaxPrio;
(P >= 0) and (Type == headline) ->
LResource = jid:resourceprep(R),
Mod = get_sm_backend(LServer),
Session = lists:max(Ss),
Pid = element(2, Session#session.sid),
?DEBUG("sending to process ~p~n", [Pid]),
- Pid ! {route, From, To, Packet}
+ LMaxRes = jid:resourceprep(MaxRes),
+ Packet1 = maybe_mark_as_copy(Packet,
+ LResource,
+ LMaxRes,
+ P, MaxPrio),
+ Pid ! {route, From, To, Packet1}
end;
%% Ignore other priority:
({_Prio, _Res}) -> ok
end
end.
+-spec maybe_mark_as_copy(message(), binary(), binary(), integer(), integer())
+ -> message().
+maybe_mark_as_copy(Packet, R, R, P, P) ->
+ Packet;
+maybe_mark_as_copy(Packet, _, _, P, P) ->
+ Meta = Packet#message.meta,
+ Packet#message{meta = Meta#{sm_copy => true}};
+maybe_mark_as_copy(Packet, _, _, _, _) ->
+ Packet.
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec clean_session_list([#session{}]) -> [#session{}].
clean_session_list(Ss) ->
{U, S, R} = jid:tolower(JID),
PrioRes = ejabberd_sm:get_user_present_resources(U, S),
{_, AvailRs} = lists:unzip(PrioRes),
- {MaxPrio, MaxRes} = case catch lists:max(PrioRes) of
+ {MaxPrio, _MaxRes} = case catch lists:max(PrioRes) of
{Prio, Res} -> {Prio, Res};
_ -> {0, undefined}
end,
end,
%% list of JIDs that should receive a carbon copy of this message (excluding the
%% receiver(s) of the original message
- TargetJIDs = case {IsBareTo, R} of
- {true, MaxRes} ->
- OrigTo = fun(Res) -> lists:member({MaxPrio, Res}, PrioRes) end,
- [ {jid:make({U, S, CCRes}), CC_Version}
- || {CCRes, CC_Version} <- list(U, S),
- lists:member(CCRes, AvailRs), not OrigTo(CCRes) ];
- {true, _} ->
+ TargetJIDs = case {IsBareTo, Packet} of
+ {true, #message{meta = #{sm_copy := true}}} ->
%% The message was sent to our bare JID, and we currently have
%% multiple resources with the same highest priority, so the session
%% manager routes the message to each of them. We create carbon
- %% copies only from one of those resources (the one where R equals
- %% MaxRes) in order to avoid duplicates.
+ %% copies only from one of those resources in order to avoid
+ %% duplicates.
[];
+ {true, _} ->
+ OrigTo = fun(Res) -> lists:member({MaxPrio, Res}, PrioRes) end,
+ [ {jid:make({U, S, CCRes}), CC_Version}
+ || {CCRes, CC_Version} <- list(U, S),
+ lists:member(CCRes, AvailRs), not OrigTo(CCRes) ];
{false, _} ->
[ {jid:make({U, S, CCRes}), CC_Version}
|| {CCRes, CC_Version} <- list(U, S),
Acc.
-spec user_receive_packet(stanza(), ejabberd_c2s:state(), jid(), jid(), jid()) -> stanza().
-user_receive_packet(Pkt, C2SState, JID, Peer, To) ->
+user_receive_packet(Pkt, C2SState, JID, Peer, _To) ->
LUser = JID#jid.luser,
LServer = JID#jid.lserver,
- IsBareCopy = is_bare_copy(JID, To),
case should_archive(Pkt, LServer) of
- true when not IsBareCopy ->
+ true ->
NewPkt = strip_my_archived_tag(Pkt, LServer),
case store_msg(C2SState, NewPkt, LUser, LServer, Peer, recv) of
{ok, ID} ->
should_archive(#message{type = error}, _LServer) ->
false;
+should_archive(#message{meta = #{sm_copy := true}}, _LServer) ->
+ false;
should_archive(#message{body = Body, subject = Subject,
type = Type} = Pkt, LServer) ->
case is_resent(Pkt, LServer) of
maybe_update_from_to(Pkt, _JidRequestor, _JidArchive, _Peer, chat, _Nick) ->
Pkt.
-is_bare_copy(#jid{luser = U, lserver = S, lresource = R}, To) ->
- PrioRes = ejabberd_sm:get_user_present_resources(U, S),
- MaxRes = case catch lists:max(PrioRes) of
- {_Prio, Res} when is_binary(Res) ->
- Res;
- _ ->
- undefined
- end,
- IsBareTo = case To of
- #jid{lresource = <<"">>} ->
- true;
- #jid{lresource = LRes} ->
- %% Unavailable resources are handled like bare JIDs.
- lists:keyfind(LRes, 2, PrioRes) =:= false
- end,
- case {IsBareTo, R} of
- {true, MaxRes} ->
- ?DEBUG("Recipient of message to bare JID has top priority: ~s@~s/~s",
- [U, S, R]),
- false;
- {true, _R} ->
- %% The message was sent to our bare JID, and we currently have
- %% multiple resources with the same highest priority, so the session
- %% manager routes the message to each of them. We store the message
- %% only from the resource where R equals MaxRes.
- ?DEBUG("Additional recipient of message to bare JID: ~s@~s/~s",
- [U, S, R]),
- true;
- {false, _R} ->
- false
- end.
-
-spec send([{binary(), integer(), xmlel()}],
non_neg_integer(), boolean(), iq()) -> iq() | ignore.
send(Msgs, Count, IsComplete,