Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dynamic domains in mod_adhoc #3252

Merged
merged 5 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

{suites, "tests", acc_e2e_SUITE}.

{suites, "tests", adhoc_SUITE}.

{suites, "tests", bosh_SUITE}.

{suites, "tests", carboncopy_SUITE}.
Expand Down
18 changes: 10 additions & 8 deletions big_tests/tests/adhoc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

-define(NS_COMMANDS, <<"http://jabber.org/protocol/commands">>).
-import(domain_helper, [host_type/0]).

%%--------------------------------------------------------------------
%% Suite configuration
%%--------------------------------------------------------------------

-define(NS_COMMANDS, <<"http://jabber.org/protocol/commands">>).

all() ->
[{group, disco_visible},
{group, adhoc}].

groups() ->
G = [{adhoc, [parallel], common_disco_cases() ++ hidden_disco_cases() ++ [ping]},
{disco_visible, [parallel], common_disco_cases() ++ visible_disco_cases()}],
ct_helper:repeat_all_until_all_ok(G).
[{adhoc, [parallel], common_disco_cases() ++ hidden_disco_cases() ++ [ping]},
{disco_visible, [parallel], common_disco_cases() ++ visible_disco_cases()}].

common_disco_cases() ->
[disco_info,
Expand Down Expand Up @@ -75,15 +77,15 @@ end_per_testcase(CaseName, Config) ->
escalus:end_per_testcase(CaseName, Config).

init_modules(disco_visible, Config) ->
Config1 = escalus:init_per_suite(dynamic_modules:save_modules(domain(), Config)),
dynamic_modules:ensure_modules(domain(), [{mod_adhoc, [{report_commands_node, true}]}]),
Config1 = escalus:init_per_suite(dynamic_modules:save_modules(host_type(), Config)),
dynamic_modules:ensure_modules(host_type(), [{mod_adhoc, [{report_commands_node, true}]}]),
Config1;
init_modules(_, Config) ->
Config.

restore_modules(disco_visible, Config) ->
dynamic_modules:restore_modules(domain(), Config);
restore_modules(_, Config) ->
dynamic_modules:restore_modules(host_type(), Config);
restore_modules(_, _Config) ->
ok.

%%--------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions rel/mim1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
[[host_config]]
host_type = \"test type\"

[host_config.modules.mod_adhoc]

[host_config.modules.mod_bosh]

[host_config.modules.mod_cache_users]
Expand Down
147 changes: 82 additions & 65 deletions src/mod_adhoc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
-type command_hook_acc() :: {error, exml:element()} | exml:element() | ignore | empty.
-export_type([command_hook_acc/0]).

%% Gen_mod callbacks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
%% Gen_mod callbacks
%% gen_mod callbacks

Copy link
Member

@chrzaszcz chrzaszcz Sep 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same comment as in mod_last so I decided we could merge it as it is. I could fix both with some upcoming PR.

-export([start/2,
stop/1,
config_spec/0,
process_local_iq/4,
process_sm_iq/4,
supported_features/0]).

%% IQ and hook handlers
-export([process_local_iq/5,
process_sm_iq/5,
disco_local_items/1,
disco_local_identity/1,
disco_local_features/1,
Expand All @@ -46,36 +50,41 @@

-ignore_xref([disco_local_features/1, disco_local_identity/1, disco_local_items/1,
disco_sm_features/1, disco_sm_identity/1, disco_sm_items/1,
ping_command/4, process_local_iq/4, process_sm_iq/4]).
ping_command/4, process_local_iq/5, process_sm_iq/5]).

-include("mongoose.hrl").
-include("jlib.hrl").
-include("adhoc.hrl").
-include("mongoose_config_spec.hrl").

start(Host, Opts) ->
start(HostType, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),

gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_COMMANDS,
?MODULE, process_local_iq, IQDisc),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_COMMANDS,
?MODULE, process_sm_iq, IQDisc),
ejabberd_hooks:add(hooks(Host)).
[gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_COMMANDS, Component, Fn, #{}, IQDisc) ||
{Component, Fn} <- iq_handlers()],
ejabberd_hooks:add(hooks(HostType)).

stop(HostType) ->
ejabberd_hooks:delete(hooks(HostType)),
[gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_COMMANDS, Component) ||
{Component, _Fn} <- iq_handlers()].

stop(Host) ->
ejabberd_hooks:delete(hooks(Host)),
iq_handlers() ->
[{ejabberd_local, fun ?MODULE:process_local_iq/5},
{ejabberd_sm, fun ?MODULE:process_sm_iq/5}].

gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_COMMANDS),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_COMMANDS).
hooks(HostType) ->
[{disco_local_identity, HostType, ?MODULE, disco_local_identity, 99},
{disco_local_features, HostType, ?MODULE, disco_local_features, 99},
{disco_local_items, HostType, ?MODULE, disco_local_items, 99},
{disco_sm_identity, HostType, ?MODULE, disco_sm_identity, 99},
{disco_sm_features, HostType, ?MODULE, disco_sm_features, 99},
{disco_sm_items, HostType, ?MODULE, disco_sm_items, 99},
{adhoc_local_commands, HostType, ?MODULE, ping_command, 100}].

hooks(Host) ->
[{disco_local_identity, Host, ?MODULE, disco_local_identity, 99},
{disco_local_features, Host, ?MODULE, disco_local_features, 99},
{disco_local_items, Host, ?MODULE, disco_local_items, 99},
{disco_sm_identity, Host, ?MODULE, disco_sm_identity, 99},
{disco_sm_features, Host, ?MODULE, disco_sm_features, 99},
{disco_sm_items, Host, ?MODULE, disco_sm_items, 99},
{adhoc_local_commands, Host, ?MODULE, ping_command, 100}].
%%%
%%% config_spec
%%%

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
Expand All @@ -84,11 +93,56 @@ config_spec() ->
<<"iqdisc">> => mongoose_config_spec:iqdisc()}
}.

%%-------------------------------------------------------------------------
-spec supported_features() -> [atom()].
supported_features() -> [dynamic_domains].

%%%
%%% IQ handlers
%%%

-spec process_local_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(), map())
-> {mongoose_acc:t(), ignore | jlib:iq()}.
process_local_iq(Acc, From, To, IQ, _Extra) ->
{Acc, process_adhoc_request(Acc, From, To, IQ, adhoc_local_commands)}.

-spec process_sm_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(), map()) ->
{mongoose_acc:t(), ignore | jlib:iq()}.
process_sm_iq(Acc, From, To, IQ, _Extra) ->
{Acc, process_adhoc_request(Acc, From, To, IQ, adhoc_sm_commands)}.

-spec process_adhoc_request(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(),
Hook :: atom()) -> ignore | jlib:iq().
process_adhoc_request(Acc, From, To, #iq{sub_el = SubEl} = IQ, Hook) ->
?LOG_DEBUG(#{what => adhoc_parse_request, iq => IQ, hook => Hook}),
case adhoc:parse_request(IQ) of
{error, Error} ->
IQ#iq{type = error, sub_el = [SubEl, Error]};
#adhoc_request{} = AdhocRequest ->
HostType = mongoose_acc:host_type(Acc),
case run_request_hook(Hook, HostType, From, To, AdhocRequest) of
ignore ->
ignore;
empty ->
IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:item_not_found()]};
{error, Error} ->
IQ#iq{type = error, sub_el = [SubEl, Error]};
Command ->
IQ#iq{type = result, sub_el = [Command]}
end
end.

run_request_hook(adhoc_local_commands, HostType, From, To, AdhocRequest) ->
mongoose_hooks:adhoc_local_commands(HostType, From, To, AdhocRequest);
run_request_hook(adhoc_sm_commands, HostType, From, To, AdhocRequest) ->
mongoose_hooks:adhoc_sm_commands(HostType, From, To, AdhocRequest).

%%%
%%% Hooks handlers
%%%

-spec disco_local_items(mongoose_disco:item_acc()) -> mongoose_disco:item_acc().
disco_local_items(Acc = #{to_jid := #jid{lserver = LServer}, node := <<>>, lang := Lang}) ->
Items = case are_commands_visible(LServer) of
disco_local_items(Acc = #{host_type := HostType, to_jid := #jid{lserver = LServer}, node := <<>>, lang := Lang}) ->
Items = case are_commands_visible(HostType) of
false ->
[];
_ ->
Expand All @@ -106,8 +160,8 @@ disco_local_items(Acc) ->
%%-------------------------------------------------------------------------

-spec disco_sm_items(mongoose_disco:item_acc()) -> mongoose_disco:item_acc().
disco_sm_items(Acc = #{to_jid := #jid{lserver = LServer} = To, node := <<>>, lang := Lang}) ->
Items = case are_commands_visible(LServer) of
disco_sm_items(Acc = #{host_type := HostType, to_jid := To, node := <<>>, lang := Lang}) ->
Items = case are_commands_visible(HostType) of
false ->
[];
_ ->
Expand All @@ -117,8 +171,8 @@ disco_sm_items(Acc = #{to_jid := #jid{lserver = LServer} = To, node := <<>>, lan
disco_sm_items(Acc) ->
Acc.

are_commands_visible(LServer) ->
gen_mod:get_module_opt(LServer, ?MODULE, report_commands_node, false).
are_commands_visible(HostType) ->
gen_mod:get_module_opt(HostType, ?MODULE, report_commands_node, false).

item(LServer, Node, Name, Lang) ->
#{jid => LServer, node => Node, name => translate:translate(Lang, Name)}.
Expand Down Expand Up @@ -180,43 +234,6 @@ disco_sm_features(Acc) ->

%%-------------------------------------------------------------------------

-spec process_local_iq(jid:jid(), jid:jid(), mongoose_acc:t(), jlib:iq()) ->
{mongoose_acc:t(), ignore | jlib:iq()}.
process_local_iq(From, To, Acc, IQ) ->
{Acc, process_adhoc_request(From, To, IQ, adhoc_local_commands)}.

-spec process_sm_iq(jid:jid(), jid:jid(), mongoose_acc:t(), jlib:iq()) ->
{mongoose_acc:t(), ignore | jlib:iq()}.
process_sm_iq(From, To, Acc, IQ) ->
{Acc, process_adhoc_request(From, To, IQ, adhoc_sm_commands)}.

-spec process_adhoc_request(jid:jid(), jid:jid(), jlib:iq(),
Hook :: atom()) -> ignore | jlib:iq().
process_adhoc_request(From, To, #iq{sub_el = SubEl} = IQ, Hook) ->
?LOG_DEBUG(#{what => adhoc_parse_request, iq => IQ, hook => Hook}),
case adhoc:parse_request(IQ) of
{error, Error} ->
IQ#iq{type = error, sub_el = [SubEl, Error]};
#adhoc_request{} = AdhocRequest ->
Host = To#jid.lserver,
case run_request_hook(Hook, Host, From, To, AdhocRequest) of
ignore ->
ignore;
empty ->
IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:item_not_found()]};
{error, Error} ->
IQ#iq{type = error, sub_el = [SubEl, Error]};
Command ->
IQ#iq{type = result, sub_el = [Command]}
end
end.

run_request_hook(adhoc_local_commands, Host, From, To, AdhocRequest) ->
mongoose_hooks:adhoc_local_commands(Host, From, To, AdhocRequest);
run_request_hook(adhoc_sm_commands, Host, From, To, AdhocRequest) ->
mongoose_hooks:adhoc_sm_commands(Host, From, To, AdhocRequest).


-spec ping_command(Acc :: command_hook_acc(),
From :: jid:jid(),
To :: jid:jid(),
Expand Down
16 changes: 8 additions & 8 deletions src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,24 @@ c2s_remote_hook(HostType, Tag, Args, HandlerState, C2SState) ->
run_hook_for_host_type(c2s_remote_hook, HostType, HandlerState,
[Tag, Args, C2SState]).

-spec adhoc_local_commands(LServer, From, To, AdhocRequest) -> Result when
LServer :: jid:lserver(),
-spec adhoc_local_commands(HostType, From, To, AdhocRequest) -> Result when
HostType :: mongooseim:host_type(),
From :: jid:jid(),
To :: jid:jid(),
AdhocRequest :: adhoc:request(),
Result :: mod_adhoc:command_hook_acc().
adhoc_local_commands(LServer, From, To, AdhocRequest) ->
run_hook_for_host_type(adhoc_local_commands, LServer, empty,
adhoc_local_commands(HostType, From, To, AdhocRequest) ->
run_hook_for_host_type(adhoc_local_commands, HostType, empty,
[From, To, AdhocRequest]).

-spec adhoc_sm_commands(LServer, From, To, AdhocRequest) -> Result when
LServer :: jid:lserver(),
-spec adhoc_sm_commands(HostType, From, To, AdhocRequest) -> Result when
HostType :: mongooseim:host_type(),
From :: jid:jid(),
To :: jid:jid(),
AdhocRequest :: adhoc:request(),
Result :: mod_adhoc:command_hook_acc().
adhoc_sm_commands(LServer, From, To, AdhocRequest) ->
run_hook_for_host_type(adhoc_sm_commands, LServer, empty, [From, To, AdhocRequest]).
adhoc_sm_commands(HostType, From, To, AdhocRequest) ->
run_hook_for_host_type(adhoc_sm_commands, HostType, empty, [From, To, AdhocRequest]).

%%% @doc The `anonymous_purge_hook' hook is called when anonymous user's data is removed.
-spec anonymous_purge_hook(LServer, Acc, LUser) -> Result when
Expand Down