From cb56b8a8b71d0643ee972100045f7d0616a22c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Fri, 10 Sep 2021 08:40:50 +0200 Subject: [PATCH 1/6] Add support for dynamic domains in mod_sic --- src/mod_sic.erl | 68 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/src/mod_sic.erl b/src/mod_sic.erl index 4bf1a5f981..ca9f969134 100644 --- a/src/mod_sic.erl +++ b/src/mod_sic.erl @@ -29,14 +29,19 @@ -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"). @@ -44,40 +49,57 @@ -define(NS_SIC, <<"urn:xmpp:sic:1">>). -start(Host, Opts) -> +-spec start(mongooseim:host_type(), list()) -> 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) -> From b02443e1b0d2073915be2ea8dfce3ba8a1a2d12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Fri, 10 Sep 2021 08:58:08 +0200 Subject: [PATCH 2/6] Enable mod_sic for dynamic_domains --- rel/mim1.vars-toml.config | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rel/mim1.vars-toml.config b/rel/mim1.vars-toml.config index f83c5c3e6d..aa960acb8c 100644 --- a/rel/mim1.vars-toml.config +++ b/rel/mim1.vars-toml.config @@ -52,6 +52,8 @@ [host_config.modules.mod_register] + [host_config.modules.mod_sic] + {{#mod_last}} [host_config.modules.mod_last] {{{mod_last}}} From 237e0ea5990cf3c2d95132304e075cfc724f336a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Fri, 10 Sep 2021 08:59:45 +0200 Subject: [PATCH 3/6] Adapts mod_sic tests for dynamic domains --- big_tests/tests/sic_SUITE.erl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/big_tests/tests/sic_SUITE.erl b/big_tests/tests/sic_SUITE.erl index 8171435e3d..8966443745 100644 --- a/big_tests/tests/sic_SUITE.erl +++ b/big_tests/tests/sic_SUITE.erl @@ -17,6 +17,8 @@ require_rpc_nodes/1, rpc/4]). +-import(domain_helper, [host_type/0]). + %%%=================================================================== %%% Suite configuration %%%=================================================================== @@ -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(). @@ -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() -> From 90b7e48dd218c016403bd09e4c4e682bea4df652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Fri, 10 Sep 2021 09:00:56 +0200 Subject: [PATCH 4/6] Enable mod_sic tests for dynamic domains --- big_tests/dynamic_domains.spec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/big_tests/dynamic_domains.spec b/big_tests/dynamic_domains.spec index cc98066002..2b6c3a1212 100644 --- a/big_tests/dynamic_domains.spec +++ b/big_tests/dynamic_domains.spec @@ -72,6 +72,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}. From 2108f0dcd1ea9f4142c60c422c4fae198a640c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wojtasik?= Date: Fri, 10 Sep 2021 11:10:44 +0200 Subject: [PATCH 5/6] Fix indentation --- src/mod_sic.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mod_sic.erl b/src/mod_sic.erl index ca9f969134..50694c820d 100644 --- a/src/mod_sic.erl +++ b/src/mod_sic.erl @@ -60,7 +60,7 @@ start(HostType, Opts) -> -spec stop(mongooseim:host_type()) -> ok. stop(HostType) -> [gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_LAST, Component) || - {Component, _Fn} <- iq_handlers()], + {Component, _Fn} <- iq_handlers()], ok. iq_handlers() -> From 6755787a6f0627f3564b4af4e2cfaef67a33153e Mon Sep 17 00:00:00 2001 From: Premwoik Date: Fri, 10 Sep 2021 12:07:33 +0200 Subject: [PATCH 6/6] Make spec more precise Co-authored-by: Nelson Vides --- src/mod_sic.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mod_sic.erl b/src/mod_sic.erl index 50694c820d..ef83768583 100644 --- a/src/mod_sic.erl +++ b/src/mod_sic.erl @@ -49,7 +49,7 @@ -define(NS_SIC, <<"urn:xmpp:sic:1">>). --spec start(mongooseim:host_type(), list()) -> ok. +-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. start(HostType, Opts) -> IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),