Skip to content

Commit

Permalink
Merge pull request #3258 from esl/support-dynamic-domains-in-mod_sic
Browse files Browse the repository at this point in the history
Support dynamic domains in mod_sic
  • Loading branch information
chrzaszcz authored Sep 10, 2021
2 parents da5955c + 6755787 commit cf75dd9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
[non_default_http_server_name_is_returned_if_configured],
"at the moment mim2 node is not configured for dynamic domains"}.

{suites, "tests", sic_SUITE}.

{suites, "tests", sm_SUITE}.

{suites, "tests", vcard_SUITE}.
Expand Down
9 changes: 5 additions & 4 deletions big_tests/tests/sic_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
require_rpc_nodes/1,
rpc/4]).

-import(domain_helper, [host_type/0]).

%%%===================================================================
%%% Suite configuration
%%%===================================================================
Expand All @@ -28,8 +30,7 @@ all_tests() ->
[user_sic, forbidden_user_sic].

groups() ->
G = [{mod_sic_tests, [sequence], all_tests()}],
ct_helper:repeat_all_until_all_ok(G).
[{mod_sic_tests, [sequence], all_tests()}].

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().
Expand Down Expand Up @@ -107,11 +108,11 @@ is_sic_response() ->
%%%===================================================================

start_module(ModuleName, Options) ->
Args = [ct:get_config({hosts, mim, domain}), ModuleName, Options],
Args = [host_type(), ModuleName, Options],
rpc(mim(), gen_mod, start_module, Args).

stop_module(ModuleName) ->
Args = [ct:get_config({hosts, mim, domain}), ModuleName],
Args = [host_type(), ModuleName],
rpc(mim(), gen_mod, stop_module, Args).

sic_iq_get() ->
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 @@ -52,6 +52,8 @@

[host_config.modules.mod_register]

[host_config.modules.mod_sic]

{{#mod_last}}
[host_config.modules.mod_last]
{{{mod_last}}}
Expand Down
68 changes: 45 additions & 23 deletions src/mod_sic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,77 @@
-behaviour(gen_mod).
-behaviour(mongoose_module_metrics).

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

-ignore_xref([process_local_iq/4, process_sm_iq/4]).
%% IQ and hook handlers
-export([process_local_iq/5,
process_sm_iq/5
]).

-ignore_xref([process_local_iq/5, process_sm_iq/5]).

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

-define(NS_SIC, <<"urn:xmpp:sic:1">>).

start(Host, Opts) ->
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
start(HostType, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
?NS_SIC, ?MODULE, process_local_iq, IQDisc),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
?NS_SIC, ?MODULE, process_sm_iq, IQDisc).

stop(Host) ->
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_SIC),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_SIC).
[gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_SIC, Component, Fn, #{}, IQDisc) ||
{Component, Fn} <- iq_handlers()],
ok.

-spec stop(mongooseim:host_type()) -> ok.
stop(HostType) ->
[gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_LAST, Component) ||
{Component, _Fn} <- iq_handlers()],
ok.

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

%%%
%%% config_spec
%%%

-spec config_spec() -> mongoose_config_spec:config_section().
config_spec() ->
#section{
items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc()}}.

process_local_iq(#jid{} = JID, _To,
Acc, #iq{type = 'get', sub_el = _SubEl} = IQ) ->
{Acc, get_ip(JID, IQ)};
-spec supported_features() -> [atom()].
supported_features() -> [dynamic_domains].

process_local_iq(_From, _To, Acc, #iq{type = 'set', sub_el = SubEl} = IQ) ->
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:not_allowed()]}}.
%%%
%%% IQ handlers
%%%

-spec process_local_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(), map())
-> {mongoose_acc:t(), jlib:iq()}.
process_local_iq(Acc, #jid{} = JID, _To,
#iq{type = 'get', sub_el = _SubEl} = IQ, _Extra) ->
{Acc, get_ip(JID, IQ)};
process_local_iq(Acc, _From, _To, #iq{type = 'set', sub_el = SubEl} = IQ, _Extra) ->
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:not_allowed()]}}.

process_sm_iq(#jid{user = User, server = Server} = JID,
-spec process_sm_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(), map())
-> {mongoose_acc:t(), jlib:iq()}.
process_sm_iq(Acc, #jid{user = User, server = Server} = JID,
#jid{user = User, server = Server},
Acc,
#iq{type = 'get', sub_el = _SubEl} = IQ) ->
#iq{type = 'get', sub_el = _SubEl} = IQ, _Extra) ->
{Acc, get_ip(JID, IQ)};

process_sm_iq(_From, _To, Acc, #iq{type = 'get', sub_el = SubEl} = IQ) ->
process_sm_iq(Acc, _From, _To, #iq{type = 'get', sub_el = SubEl} = IQ, _Extra) ->
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:forbidden()]}};

process_sm_iq(_From, _To, Acc, #iq{type = 'set', sub_el = SubEl} = IQ) ->
process_sm_iq(Acc, _From, _To, #iq{type = 'set', sub_el = SubEl} = IQ, _Extra) ->
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:not_allowed()]}}.

get_ip(JID, #iq{sub_el = #xmlel{} = SubEl} = IQ) ->
Expand Down

0 comments on commit cf75dd9

Please sign in to comment.