Skip to content

Commit

Permalink
Call filter_local_packet for host type
Browse files Browse the repository at this point in the history
To work correctly for subdomains,
  host type has to be temporarily replaced with the subdomain.

Also: remove unused routing for subhosts.
  Motivation: modules set the routes for subhosts directly,
              so the code is unreachable and potentially
              results in running hooks for different hosts, if ever used.
  • Loading branch information
chrzaszcz committed May 6, 2021
1 parent 3d3ff8d commit d3a36c6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/ejabberd_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ start_link() ->
%% and implement two functions:
%% filter/3 - should return either 'drop' atom or its args
%% route/3, which should either:
%% - deliver the message locally by calling mongoose_local_delivery:do_local_route/3
%% - deliver the message locally by calling mongoose_local_delivery:do_route/5
%% and return 'done'
%% - deliver the message it its own way and return 'done'
%% - return its args
Expand Down
12 changes: 6 additions & 6 deletions src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
auth_failed/3,
ejabberd_ctl_process/2,
failed_to_store_message/1,
filter_local_packet/2,
filter_local_packet/1,
filter_packet/1,
inbox_unread_count/3,
local_send_to_resource_hook/4,
Expand Down Expand Up @@ -271,12 +271,12 @@ failed_to_store_message(Acc) ->
To :: jid:jid(),
Acc :: mongoose_acc:t(),
Packet :: exml:element()}.
-spec filter_local_packet(Server, Acc) -> Result when
Server :: jid:server(),
Acc :: filter_packet_acc(),
-spec filter_local_packet(FilterAcc) -> Result when
FilterAcc :: filter_packet_acc(),
Result :: drop | filter_packet_acc().
filter_local_packet(Server, Acc) ->
ejabberd_hooks:run_for_host_type(filter_local_packet, Server, Acc, []).
filter_local_packet(FilterAcc = {_From, _To, Acc, _Packet}) ->
HostType = mongoose_acc:host_type(Acc),
ejabberd_hooks:run_for_host_type(filter_local_packet, HostType, FilterAcc, []).

%%% @doc The `filter_packet' hook is called to filter out
%%% stanzas routed with `mongoose_router_global'.
Expand Down
22 changes: 17 additions & 5 deletions src/mongoose_local_delivery.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@
-include("jlib.hrl").

%% API
-export([do_route/6]).
-export([do_route/5]).


do_route(OrigFrom, OrigTo, OrigAcc, OrigPacket, LDstDomain, Handler) ->
do_route(OrigFrom, OrigTo, OrigAcc, OrigPacket, Handler) ->
% strip acc from all sender-related values, from now on we are interested in the recipient
Acc0 = mongoose_acc:strip(#{lserver => OrigTo#jid.lserver,
LDstDomain = OrigTo#jid.lserver,
HostType = case mongoose_domain_api:get_host_type(LDstDomain) of
{ok, HT} ->
HT;
{error, not_found} ->
%% TODO this is needed to run hooks registered for subdomains
%% Replace with mapping from subdomain to host type when it is supported
LDstDomain
end,
Acc0 = mongoose_acc:strip(#{lserver => LDstDomain,
host_type => HostType,
from_jid => OrigFrom,
to_jid => OrigTo,
element => OrigPacket},
OrigAcc),
%% Filter locally
case mongoose_hooks:filter_local_packet(LDstDomain, {OrigFrom, OrigTo, Acc0, OrigPacket}) of
case mongoose_hooks:filter_local_packet({OrigFrom, OrigTo, Acc0, OrigPacket}) of
{From, To, Acc, Packet} ->
Acc1 = mongoose_acc:update_stanza(#{from_jid => From, to_jid => To, element => Packet}, Acc),
Acc1 = mongoose_acc:update_stanza(#{from_jid => From,
to_jid => To,
element => Packet}, Acc),
mongoose_packet_handler:process(Handler, Acc1, From, To, Packet);
drop ->
mongoose_hooks:xmpp_stanza_dropped(mongoose_acc:host_type(Acc0), OrigFrom,
Expand Down
3 changes: 1 addition & 2 deletions src/mongoose_router_external.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ route(From, To, Acc, Packet) ->
[] ->
{From, To, Acc, Packet};
[#external_component{handler = Handler}|_] -> %% may be multiple on various nodes
mongoose_local_delivery:do_route(From, To, Acc, Packet,
LDstDomain, Handler),
mongoose_local_delivery:do_route(From, To, Acc, Packet, Handler),
done
end.
3 changes: 1 addition & 2 deletions src/mongoose_router_external_localnode.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ route(From, To, Acc, Packet) ->
[] ->
{From, To, Acc, Packet};
[#external_component{handler = Handler}] ->
mongoose_local_delivery:do_route(From, To, Acc, Packet,
LDstDomain, Handler),
mongoose_local_delivery:do_route(From, To, Acc, Packet, Handler),
done
end.
14 changes: 2 additions & 12 deletions src/mongoose_router_localdomain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@ filter(From, To, Acc, Packet) ->

route(From, To, Acc, Packet) ->
LDstDomain = To#jid.lserver,
case route_to_host(From, To, Acc, Packet, LDstDomain) of
done -> done;
Result ->
case mongoose_subhosts:get_host(LDstDomain) of
{ok, Host} -> route_to_host(From, To, Acc, Packet, Host);
undefined -> Result
end
end.

route_to_host(From, To, Acc, Packet, Host) ->
case mnesia:dirty_read(route, Host) of
case mnesia:dirty_read(route, LDstDomain) of
[] -> {From, To, Acc, Packet};
[#route{handler = Handler}] ->
mongoose_local_delivery:do_route(From, To, Acc, Packet, Host, Handler),
mongoose_local_delivery:do_route(From, To, Acc, Packet, Handler),
done
end.

0 comments on commit d3a36c6

Please sign in to comment.