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