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