./ct_report/coverage/mongoose_domain_api.COVER.html

1 %% Main module other parts of MongooseIM should use to access the domain
2 %% management.
3 -module(mongoose_domain_api).
4
5 -export([init/0,
6 stop/0,
7 get_host_type/1]).
8
9 %% domain API
10 -export([insert_domain/2,
11 delete_domain/2,
12 disable_domain/1,
13 enable_domain/1,
14 get_domain_host_type/1,
15 get_all_static/0,
16 get_domains_by_host_type/1]).
17
18 %% domain admin API
19 -export([check_domain_password/2,
20 set_domain_password/2,
21 delete_domain_password/1]).
22
23 %% subdomain API
24 -export([register_subdomain/3,
25 unregister_subdomain/2,
26 get_subdomain_host_type/1,
27 get_subdomain_info/1,
28 get_all_subdomains_for_domain/1]).
29
30 %% For testing
31 -export([get_all_dynamic/0]).
32
33 -ignore_xref([get_all_static/0]).
34 -ignore_xref([get_all_dynamic/0]).
35 -ignore_xref([stop/0]).
36
37 -type domain() :: jid:lserver().
38 -type host_type() :: mongooseim:host_type().
39 -type subdomain_pattern() :: mongoose_subdomain_utils:subdomain_pattern().
40
41
42 -spec init() -> ok | {error, term()}.
43 init() ->
44 83 mongoose_domain_core:start(),
45 83 mongoose_subdomain_core:start(),
46 83 mongoose_lazy_routing:start().
47
48 %% Stops gen_servers, that are started from init/0
49 %% Does not fail, even if servers are already stopped
50 -spec stop() -> ok.
51 stop() ->
52
:-(
catch mongoose_domain_core:stop(),
53
:-(
catch mongoose_subdomain_core:stop(),
54
:-(
catch mongoose_lazy_routing:stop(),
55
:-(
ok.
56
57 %% Domain should be nameprepped using `jid:nameprep'.
58 -spec insert_domain(domain(), host_type()) ->
59 ok | {error, duplicate} | {error, static} | {error, {db_error, term()}}
60 | {error, service_disabled} | {error, unknown_host_type}.
61 insert_domain(Domain, HostType) ->
62 1 case check_domain(Domain, HostType) of
63 ok ->
64
:-(
check_db(mongoose_domain_sql:insert_domain(Domain, HostType));
65 Other ->
66 1 Other
67 end.
68
69 %% Returns ok, if domain not found.
70 %% Domain should be nameprepped using `jid:nameprep'.
71 -spec delete_domain(domain(), host_type()) ->
72 ok | {error, static} | {error, {db_error, term()}}
73 | {error, service_disabled} | {error, wrong_host_type} | {error, unknown_host_type}.
74 delete_domain(Domain, HostType) ->
75
:-(
case check_domain(Domain, HostType) of
76 ok ->
77
:-(
Res = check_db(mongoose_domain_sql:delete_domain(Domain, HostType)),
78
:-(
case Res of
79 ok ->
80
:-(
delete_domain_password(Domain),
81
:-(
mongoose_hooks:remove_domain(HostType, Domain);
82 _ ->
83
:-(
ok
84 end,
85
:-(
Res;
86 Other ->
87
:-(
Other
88 end.
89
90 -spec disable_domain(domain()) ->
91 ok | {error, not_found} | {error, static} | {error, service_disabled}
92 | {error, {db_error, term()}}.
93 disable_domain(Domain) ->
94 1 case mongoose_domain_core:is_static(Domain) of
95 true ->
96 1 {error, static};
97 false ->
98
:-(
case service_domain_db:enabled() of
99 true ->
100
:-(
check_db(mongoose_domain_sql:disable_domain(Domain));
101 false ->
102
:-(
{error, service_disabled}
103 end
104 end.
105
106 -spec enable_domain(domain()) ->
107 ok | {error, not_found} | {error, static} | {error, service_disabled}
108 | {error, {db_error, term()}}.
109 enable_domain(Domain) ->
110 1 case mongoose_domain_core:is_static(Domain) of
111 true ->
112 1 {error, static};
113 false ->
114
:-(
case service_domain_db:enabled() of
115 true ->
116
:-(
check_db(mongoose_domain_sql:enable_domain(Domain));
117 false ->
118
:-(
{error, service_disabled}
119 end
120 end.
121
122 check_db(ok) ->
123 %% Speedup the next check. %% It's async.
124
:-(
service_domain_db:force_check_for_updates(),
125
:-(
ok;
126 check_db(Result) ->
127
:-(
Result.
128
129 %% Domain should be nameprepped using `jid:nameprep'
130 -spec get_host_type(domain()) ->
131 {ok, host_type()} | {error, not_found}.
132 get_host_type(Domain) ->
133 22975 case get_domain_host_type(Domain) of
134 20827 {ok, HostType} -> {ok, HostType};
135 {error, not_found} ->
136 2148 get_subdomain_host_type(Domain)
137 end.
138
139 %% Domain should be nameprepped using `jid:nameprep'
140 -spec get_domain_host_type(domain()) ->
141 {ok, host_type()} | {error, not_found}.
142 get_domain_host_type(Domain) ->
143 40298 mongoose_domain_core:get_host_type(Domain).
144
145 %% Subdomain should be nameprepped using `jid:nameprep'
146 -spec get_subdomain_host_type(domain()) ->
147 {ok, host_type()} | {error, not_found}.
148 get_subdomain_host_type(Subdomain) ->
149 2931 mongoose_subdomain_core:get_host_type(Subdomain).
150
151 %% Subdomain should be nameprepped using `jid:nameprep'
152 -spec get_subdomain_info(domain()) ->
153 {ok, mongoose_subdomain_core:subdomain_info()} | {error, not_found}.
154 get_subdomain_info(Subdomain) ->
155 981 mongoose_subdomain_core:get_subdomain_info(Subdomain).
156
157 %% Get the list of the host_types provided during initialisation
158 %% This has complexity N, where N is the number of online domains.
159 -spec get_all_static() -> [{domain(), host_type()}].
160 get_all_static() ->
161 1 mongoose_domain_core:get_all_static().
162
163 %% Get domains, loaded from DB to this node
164 -spec get_all_dynamic() -> [{domain(), host_type()}].
165 get_all_dynamic() ->
166
:-(
mongoose_domain_core:get_all_dynamic().
167
168 %% Get the list of the host_types provided during initialisation
169 %% This has complexity N, where N is the number of online domains.
170 -spec get_domains_by_host_type(host_type()) -> [domain()].
171 get_domains_by_host_type(HostType) ->
172 3 mongoose_domain_core:get_domains_by_host_type(HostType).
173
174 check_domain(Domain, HostType) ->
175 1 Static = mongoose_domain_core:is_static(Domain),
176 1 Allowed = mongoose_domain_core:is_host_type_allowed(HostType),
177 1 HasDb = service_domain_db:enabled(),
178 1 if
179 Static ->
180 1 {error, static};
181 not Allowed ->
182
:-(
{error, unknown_host_type};
183 not HasDb ->
184
:-(
{error, service_disabled};
185 true ->
186
:-(
ok
187 end.
188
189 -type password() :: binary().
190
191 -spec check_domain_password(domain(), password()) -> ok | {error, wrong_password | not_found}.
192 check_domain_password(Domain, Password) ->
193
:-(
case mongoose_domain_sql:select_domain_admin(Domain) of
194 {ok, {Domain, Password}} ->
195
:-(
ok;
196 {ok, _} ->
197
:-(
{error, wrong_password};
198 {error, not_found} ->
199
:-(
{error, not_found}
200 end.
201
202 -spec set_domain_password(domain(), password()) -> ok | {error, not_found}.
203 set_domain_password(Domain, Password) ->
204
:-(
HostType = get_host_type(Domain),
205
:-(
case HostType of
206 {ok, _} ->
207
:-(
mongoose_domain_sql:set_domain_admin(Domain, Password);
208 {error, not_found} ->
209
:-(
{error, not_found}
210 end.
211
212 -spec delete_domain_password(domain()) -> ok.
213 delete_domain_password(Domain) ->
214
:-(
mongoose_domain_sql:delete_domain_admin(Domain).
215
216 -spec register_subdomain(host_type(), subdomain_pattern(),
217 mongoose_packet_handler:t()) ->
218 ok | {error, already_registered | subdomain_already_exists}.
219 register_subdomain(HostType, SubdomainPattern, PacketHandler) ->
220 430 mongoose_subdomain_core:register_subdomain(HostType, SubdomainPattern,
221 PacketHandler).
222
223 -spec unregister_subdomain(host_type(), subdomain_pattern()) -> ok.
224 unregister_subdomain(HostType, SubdomainPattern) ->
225 430 mongoose_subdomain_core:unregister_subdomain(HostType, SubdomainPattern).
226
227 -spec get_all_subdomains_for_domain(domain()) ->
228 [mongoose_subdomain_core:subdomain_info()].
229 get_all_subdomains_for_domain(Domain) ->
230 29 mongoose_subdomain_core:get_all_subdomains_for_domain(Domain).
Line Hits Source