./ct_report/coverage/mod_dynamic_domains_test.COVER.html

1 -module(mod_dynamic_domains_test).
2
3 -include("mongoose_config_spec.hrl").
4 -include("jlib.hrl").
5
6 -behaviour(mongoose_packet_handler).
7
8 %% API
9 -export([start/2, stop/1,
10 config_spec/0,
11 supported_features/0]).
12 -export([process_packet/5, process_iq/5]).
13
14 -ignore_xref([config_spec/0, process_packet/5, start/2, stop/1, supported_features/0]).
15
16 -define(DUMMY_NAMESPACE, <<"dummy.namespace">>).
17
18 -spec config_spec() -> mongoose_config_spec:config_section().
19 config_spec() ->
20
:-(
#section{items = #{
21 <<"host1">> =>
22 #option{type = string,
23 validate = subdomain_template,
24 process = fun mongoose_subdomain_utils:make_subdomain_pattern/1},
25 <<"host2">> =>
26 #option{type = string,
27 validate = subdomain_template,
28 process = fun mongoose_subdomain_utils:make_subdomain_pattern/1},
29 <<"namespace">> =>
30 #option{type = binary,
31 validate = non_empty}}}.
32
33
:-(
supported_features() -> [dynamic_domains].
34
35 -spec start(Host :: jid:server(), Opts :: list()) -> ok.
36 start(HostType, Opts) ->
37
:-(
Namespace = gen_mod:get_opt(namespace, Opts),
38
39 %% if we need to intercept traffic to some subdomain, we can create custom packet
40 %% handler and register it. note that IQs will be delivered to this packet handler
41 %% as well, even if an IQ handler is registered for the same subdomain. IQ handler
42 %% doesn't work at all if the IQ packet is not routed to the corresponding component.
43
:-(
SubdomainPattern1 = gen_mod:get_opt(host1, Opts),
44
:-(
Handler1 = mongoose_packet_handler:new(?MODULE),
45
:-(
mongoose_domain_api:register_subdomain(HostType, SubdomainPattern1, Handler1),
46 %% the call below is added for the demo & testing purposes, all the IQs sent for
47 %% SubdomainPattern1 will go only to the process_packet/5 function.
48
:-(
gen_iq_handler:add_iq_handler_for_subdomain(HostType, SubdomainPattern1,
49 Namespace, ejabberd_local,
50 fun ?MODULE:process_iq/5,
51 #{handler_type => subdomain},
52 one_queue),
53
54 %% If we want to just process IQs sent to some subdomain, and we don't care about
55 %% any other messages, then we can use `ejabberd_local` packet handler for such
56 %% subdomain and register IQ handler in the similar way as we do for domains.
57
:-(
SubdomainPattern2 = gen_mod:get_opt(host2, Opts),
58
:-(
Handler2 = mongoose_packet_handler:new(ejabberd_local),
59
:-(
mongoose_domain_api:register_subdomain(HostType, SubdomainPattern2, Handler2),
60
:-(
gen_iq_handler:add_iq_handler_for_subdomain(HostType, SubdomainPattern2,
61 Namespace, ejabberd_local,
62 fun ?MODULE:process_iq/5,
63 #{handler_type => subdomain},
64 one_queue),
65
66 %% we can use the new gen_iq_handler API to register IQ handlers for dynamic domains
67
:-(
gen_iq_handler:add_iq_handler_for_domain(HostType, Namespace, ejabberd_local,
68 fun ?MODULE:process_iq/5,
69 #{handler_type => domain},
70 one_queue),
71
:-(
ok.
72
73 -spec stop(Host :: jid:server()) -> ok.
74 stop(HostType) ->
75
:-(
Opts = gen_mod:get_module_opts(HostType, ?MODULE),
76
:-(
Namespace = gen_mod:get_opt(namespace, Opts),
77
78
:-(
SubdomainPattern1 = gen_mod:get_opt(host1, Opts),
79
:-(
mongoose_domain_api:unregister_subdomain(HostType, SubdomainPattern1),
80
:-(
gen_iq_handler:remove_iq_handler_for_subdomain(HostType, SubdomainPattern1,
81 Namespace, ejabberd_local),
82
83
:-(
SubdomainPattern2 = gen_mod:get_opt(host2, Opts),
84
:-(
mongoose_domain_api:unregister_subdomain(HostType, SubdomainPattern2),
85
:-(
gen_iq_handler:remove_iq_handler_for_subdomain(HostType, SubdomainPattern2,
86 Namespace, ejabberd_local),
87
88
:-(
gen_iq_handler:remove_iq_handler_for_domain(HostType, Namespace, ejabberd_local),
89
90
:-(
ok.
91
92 -spec process_packet(Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(),
93 El :: exml:element(), Extra :: map()) -> mongoose_acc:t().
94 process_packet(Acc, _From, _To, _El, _Extra) ->
95 %% do nothing, just ignore the packet
96
:-(
Acc.
97
98 -spec process_iq(Acc :: mongoose_acc:t(), From :: jid:jid(), To :: jid:jid(),
99 IQ :: jlib:iq(), Extra :: map()) -> {NewAcc :: mongoose_acc:t(),
100 IQResp :: ignore | jlib:iq()}.
101 process_iq(Acc, _From, _To, IQ, _Extra) ->
102 %% reply with empty result IQ stanza
103
:-(
{Acc, IQ#iq{type = result, sub_el = []}}.
Line Hits Source