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