Skip to content

Commit

Permalink
Replace ejabberd_config with mongoose_config
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Oct 15, 2021
1 parent a3e0576 commit dc88ddc
Show file tree
Hide file tree
Showing 45 changed files with 256 additions and 547 deletions.
4 changes: 2 additions & 2 deletions include/ejabberd_config.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
-ifndef(MONGOOSEIM_EJABBERD_CONFIG_HRL).
-define(MONGOOSEIM_EJABBERD_CONFIG_HRL, true).

-record(local_config, {key :: mongoose_config_parser:key(),
value :: mongoose_config_parser:value()
-record(local_config, {key :: mongoose_config:key(),
value :: mongoose_config:value()
}).

-endif.
9 changes: 4 additions & 5 deletions include/mongoose.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
%% If the ejabberd application description isn't loaded, returns atom: undefined
-define(MONGOOSE_VERSION, element(2, application:get_key(mongooseim,vsn))).

-define(MYHOSTS, ejabberd_config:get_local_option_or_default(hosts, [])).
-define(ALL_HOST_TYPES, ejabberd_config:get_local_option_or_default(hosts, []) ++
ejabberd_config:get_local_option_or_default(host_types, [])).
-define(MYNAME, ejabberd_config:get_local_option(default_server_domain)).
-define(MYLANG, ejabberd_config:get_local_option(language)).
-define(MYHOSTS, mongoose_config:get_opt(hosts, [])).
-define(ALL_HOST_TYPES, mongoose_config:get_opt(hosts, []) ++ mongoose_config:get_opt(host_types, [])).
-define(MYNAME, mongoose_config:get_opt(default_server_domain)).
-define(MYLANG, mongoose_config:get_opt(language, <<"en">>)).

-define(CONFIG_PATH, "etc/mongooseim.toml").

Expand Down
8 changes: 4 additions & 4 deletions src/acl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ match_rule_for_host_type(_HostType, _, all, _, _Default) ->
match_rule_for_host_type(_HostType, _, none, _, _Default) ->
deny;
match_rule_for_host_type(global, Domain, Rule, JID, Default) ->
case ejabberd_config:get_local_option({access, Rule, global}) of
case mongoose_config:get_opt({access, Rule, global}, undefined) of
undefined ->
Default;
GACLs ->
match_acls(GACLs, JID, global, Domain)
end;
match_rule_for_host_type(HostType, Domain, Rule, JID, Default) ->
GlobalACLs = ejabberd_config:get_local_option({access, Rule, global}),
HostACLs = ejabberd_config:get_local_option({access, Rule, HostType}),
GlobalACLs = mongoose_config:get_opt({access, Rule, global}, undefined),
HostACLs = mongoose_config:get_opt({access, Rule, HostType}, undefined),
case {GlobalACLs, HostACLs} of
{undefined, undefined} ->
Default;
Expand Down Expand Up @@ -162,7 +162,7 @@ match_acl(Rule, JID, HostType, Domain) ->

-spec get_acl_specs(rule(), host_type_or_global()) -> [aclspec()].
get_acl_specs(Rule, HostType) ->
ejabberd_config:get_local_option_or_default({acl, Rule, HostType}, []).
mongoose_config:get_opt({acl, Rule, HostType}, []).

-spec is_server_valid(domain_or_global(), jid:lserver()) -> boolean().
is_server_valid(Domain, Domain) ->
Expand Down
19 changes: 7 additions & 12 deletions src/auth/ejabberd_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,23 @@ hooks(HostType) ->
].

-spec set_opts(HostType :: mongooseim:host_type(),
KVs :: [tuple()]) -> {atomic|aborted, _}.
KVs :: [tuple()]) -> ok.
set_opts(HostType, KVs) ->
OldOpts = ejabberd_config:get_local_option({auth_opts, HostType}),
OldOpts = mongoose_config:get_opt({auth_opts, HostType}),
AccFunc = fun({K, V}, Acc) ->
lists:keystore(K, 1, Acc, {K, V})
end,
NewOpts = lists:foldl(AccFunc, OldOpts, KVs),
ejabberd_config:add_local_option({auth_opts, HostType}, NewOpts).
mongoose_config:set_opt({auth_opts, HostType}, NewOpts).

-spec get_opt(HostType :: mongooseim:host_type(),
Opt :: atom(),
Default :: ejabberd:value()) -> undefined | ejabberd:value().
get_opt(HostType, Opt, Default) ->
case ejabberd_config:get_local_option({auth_opts, HostType}) of
undefined ->
case mongoose_config:lookup_opt({auth_opts, HostType}) of
{error, not_found} ->
Default;
Opts ->
{ok, Opts} ->
case lists:keyfind(Opt, 1, Opts) of
{Opt, Value} ->
Value;
Expand Down Expand Up @@ -444,17 +444,12 @@ auth_modules_for_host_type(HostType) ->

-spec auth_methods(mongooseim:host_type()) -> [atom()].
auth_methods(HostType) ->
Method = ejabberd_config:get_local_option({auth_method, HostType}),
get_auth_method_as_a_list(Method).
mongoose_config:get_opt({auth_method, HostType}, []).

-spec auth_method_to_module(atom()) -> authmodule().
auth_method_to_module(Method) ->
list_to_atom("ejabberd_auth_" ++ atom_to_list(Method)).

get_auth_method_as_a_list(undefined) -> [];
get_auth_method_as_a_list(AuthMethod) when is_list(AuthMethod) -> AuthMethod;
get_auth_method_as_a_list(AuthMethod) when is_atom(AuthMethod) -> [AuthMethod].

-spec remove_domain(mongoose_hooks:simple_acc(), mongooseim:host_type(), jid:lserver()) ->
mongoose_hooks:simple_acc().
remove_domain(Acc, HostType, Domain) ->
Expand Down
4 changes: 2 additions & 2 deletions src/auth/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ stop(HostType) ->
%% defaults to false
-spec allow_multiple_connections(mongooseim:host_type()) -> boolean().
allow_multiple_connections(HostType) ->
ejabberd_config:get_local_option({allow_multiple_connections, HostType}) =:= true.
mongoose_config:get_opt({allow_multiple_connections, HostType}, false).

does_user_exist(_, LUser, LServer) ->
does_anonymous_user_exist(LUser, LServer).
Expand Down Expand Up @@ -274,7 +274,7 @@ is_protocol_enabled(HostType, Protocol) ->
%% @doc Returns the anonymous protocol to use, defaults to sasl_anon
-spec anonymous_protocol(mongooseim:host_type()) -> sasl_anon | login_anon | both.
anonymous_protocol(HostType) ->
ejabberd_config:get_local_option_or_default({anonymous_protocol, HostType}, sasl_anon).
mongoose_config:get_opt({anonymous_protocol, HostType}, sasl_anon).

-spec supported_features() -> [atom()].
supported_features() -> [dynamic_domains].
4 changes: 2 additions & 2 deletions src/auth/ejabberd_auth_external.erl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ supported_features() -> [dynamic_domains].

-spec get_cache_option(mongooseim:host_type()) -> false | {true, CacheTime::integer()}.
get_cache_option(HostType) ->
case ejabberd_config:get_local_option({extauth_cache, HostType}) of
case mongoose_config:get_opt({extauth_cache, HostType}) of
CacheTime when is_integer(CacheTime) -> {true, CacheTime};
_ -> false
end.
Expand Down Expand Up @@ -385,4 +385,4 @@ get_mod_last_configured(HostType) ->
end.

is_configured(HostType, Module) ->
lists:keymember(Module, 1, ejabberd_config:get_local_option({modules, HostType})).
lists:keymember(Module, 1, mongoose_config:get_opt({modules, HostType})).
2 changes: 1 addition & 1 deletion src/auth/ejabberd_auth_jwt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ start(HostType) ->
JWTAlgorithm = ejabberd_auth:get_opt(HostType, jwt_algorithm),
ejabberd_auth:set_opts(HostType,
[{jwt_secret, JWTSecret},
{jwt_algorithm, list_to_binary(JWTAlgorithm)}]),
{jwt_algorithm, list_to_binary(JWTAlgorithm)}]),
ok.

-spec stop(HostType :: mongooseim:host_type()) -> ok.
Expand Down
2 changes: 1 addition & 1 deletion src/auth/ejabberd_auth_ldap.erl
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ result_attrs(#state{uids = UIDs,

-spec parse_options(HostType :: mongooseim:host_type()) -> state().
parse_options(HostType) ->
Opts = ejabberd_config:get_local_option_or_default({auth_opts, HostType}, []),
Opts = mongoose_config:get_opt({auth_opts, HostType}, []),
EldapID = eldap_utils:get_mod_opt(ldap_pool_tag, Opts,
fun(A) when is_atom(A) -> A end, default),
BindEldapID = eldap_utils:get_mod_opt(ldap_bind_pool_tag, Opts,
Expand Down
3 changes: 1 addition & 2 deletions src/auth/ejabberd_auth_riak.erl
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ remove_user(HostType, LUser, LServer) ->

-spec bucket_type(mongooseim:host_type(), jid:lserver()) -> {binary(), jid:lserver()}.
bucket_type(HostType, LServer) ->
Opts = ejabberd_config:get_local_option_or_default({auth_opts, HostType}, []),
BucketType = proplists:get_value(bucket_type, Opts, <<"users">>),
BucketType = ejabberd_auth:get_opt(HostType, bucket_type, <<"users">>),
{BucketType, LServer}.

%% -----------------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions src/auth/extauth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ random_instance(MaxNum) ->

-spec get_instances(mongooseim:host_type()) -> integer().
get_instances(HostType) ->
case ejabberd_config:get_local_option({extauth_instances, HostType}) of
Num when is_integer(Num) -> Num;
_ -> 1
end.
mongoose_config:get_opt({extauth_instances, HostType}, 1).


-spec loop(port(), integer(), atom(), any()) -> no_return().
Expand Down
72 changes: 72 additions & 0 deletions src/config/mongoose_config.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-module(mongoose_config).

-export([start/0,
get_config_path/0,
set_opt/2,
lookup_opt/1,
get_opt/1,
get_opt/2]).

-include("mongoose.hrl").
-include("ejabberd_config.hrl").

-type key() :: atom() | host_type_key() | host_type_or_global_key().
-type s2s_domain_key() :: {atom(), jid:lserver()}.
-type host_type_key() :: {atom() | s2s_domain_key(), mongooseim:host_type()}.
-type host_type_or_global_key() :: {shaper | access | acl, atom(), mongooseim:host_type() | global}.

-type value() :: atom()
| binary()
| integer()
| string()
| [value()]
| tuple().

-export_type([key/0, value/0]).

-spec start() -> ok.
start() ->
Path = get_config_path(),
State = mongoose_config_parser:parse_file(Path),
set_opts(State).

%% @doc Get the filename of the ejabberd configuration file.
%% The filename can be specified with: erl -config "/path/to/mongooseim.toml".
%% It can also be specified with the environment variable EJABBERD_CONFIG_PATH.
%% If not specified, the default value 'mongooseim.toml' is assumed.
-spec get_config_path() -> string().
get_config_path() ->
DefaultPath = case os:getenv("MONGOOSE_CONFIG_PATH") of
false -> ?CONFIG_PATH;
Path -> Path
end,
application:get_env(mongooseim, config, DefaultPath).

-spec set_opts(mongoose_config_parser:state()) -> ok.
set_opts(State) ->
Opts = mongoose_config_parser:state_to_opts(State),
[set_opt(Key, Value) || #local_config{key = Key, value = Value} <- Opts],
ok.

-spec set_opt(key(), value()) -> ok.
set_opt(Key, Value) ->
persistent_term:put({?MODULE, Key}, Value).

%% @doc Use instead of get_opt(Key, undefined)
-spec lookup_opt(key()) ->
{ok, value()} | {error, not_found}.
lookup_opt(Key) ->
try persistent_term:get({?MODULE, Key}) of
Value -> {ok, Value}
catch
error:_ -> {error, not_found}
end.

%% @doc Fails if the option does not exist
-spec get_opt(key()) -> value().
get_opt(Key) ->
persistent_term:get({?MODULE, Key}).

-spec get_opt(key(), value()) -> value().
get_opt(Key, Default) ->
persistent_term:get({?MODULE, Key}, Default).
14 changes: 1 addition & 13 deletions src/config/mongoose_config_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,7 @@
-include("mongoose.hrl").
-include("ejabberd_config.hrl").

-type key() :: atom() | host_type_key() | host_type_or_global_key().
-type s2s_domain_key() :: {atom(), jid:lserver()}.
-type host_type_key() :: {atom() | s2s_domain_key(), mongooseim:host_type()}.
-type host_type_or_global_key() :: {shaper | access | acl, atom(), mongooseim:host_type() | global}.

-type value() :: atom()
| binary()
| integer()
| string()
| [value()]
| tuple().

-export_type([state/0, key/0, value/0]).
-export_type([state/0]).

-record(state, {opts = [] :: list(),
hosts = [] :: [domain_name()],
Expand Down
9 changes: 4 additions & 5 deletions src/config/mongoose_config_parser_toml.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ parse_file(FileName) ->
-spec process(toml_section()) -> mongoose_config_parser:state().
process(Content) ->
Config = parse(Content),
Hosts = get_key(Config, hosts),
HostTypes = get_key(Config, host_types),
Hosts = get_value(Config, hosts),
HostTypes = get_value(Config, host_types),
{FOpts, Opts} = lists:partition(fun(Opt) -> is_function(Opt, 1) end, Config),
HostsOpts = lists:flatmap(fun(F) -> lists:flatmap(F, Hosts) end, FOpts),
HostTypesOpts = lists:flatmap(fun(F) -> lists:flatmap(F, HostTypes) end, FOpts),
Expand Down Expand Up @@ -281,9 +281,8 @@ item_key(_, _) -> item.

%% Processing of the parsed options

-spec get_key(config_list(), mongoose_config_parser:key()) ->
[mongoose_config_parser:value()].
get_key(Config, Key) ->
-spec get_value(config_list(), mongoose_config:key()) -> [mongoose_config:value()].
get_value(Config, Key) ->
FilterFn = fun(#local_config{key = K}) when K =:= Key -> true;
(_) -> false
end,
Expand Down
4 changes: 2 additions & 2 deletions src/domain/mongoose_domain_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
-spec init() -> ok | {error, term()}.
init() ->
Pairs = get_static_pairs(),
AllowedHostTypes = ejabberd_config:get_local_option_or_default(host_types, []),
AllowedHostTypes = mongoose_config:get_opt(host_types, []),
mongoose_domain_core:start(Pairs, AllowedHostTypes),
mongoose_subdomain_core:start(),
mongoose_lazy_routing:start().
Expand Down Expand Up @@ -177,7 +177,7 @@ check_domain(Domain, HostType) ->
%% Domains should be nameprepped using `jid:nameprep'
-spec get_static_pairs() -> [pair()].
get_static_pairs() ->
[{H, H} || H <- ejabberd_config:get_local_option_or_default(hosts, [])].
[{H, H} || H <- mongoose_config:get_opt(hosts, [])].

-spec register_subdomain(host_type(), subdomain_pattern(),
mongoose_packet_handler:t()) ->
Expand Down
30 changes: 9 additions & 21 deletions src/ejabberd_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ start(normal, _Args) ->
mongoose_commands:init(),
mongoose_service:start(),
gen_mod:start(),
ejabberd_config:start(),
mongoose_logs:set_global_loglevel(ejabberd_config:get_local_option_or_default(loglevel, warning)),
connect_nodes(),
mongoose_config:start(),
mongoose_logs:set_global_loglevel(mongoose_config:get_opt(loglevel, warning)),
mongoose_deprecations:start(),
{ok, _} = Sup = ejabberd_sup:start_link(),
mongoose_domain_api:init(),
Expand Down Expand Up @@ -120,10 +119,10 @@ db_init() ->
start_modules() ->
lists:foreach(
fun(Host) ->
case ejabberd_config:get_local_option({modules, Host}) of
undefined ->
case mongoose_config:lookup_opt({modules, Host}) of
{error, not_found} ->
ok;
Modules ->
{ok, Modules} ->
gen_mod_deps:start_modules(Host, Modules)
end
end, ?ALL_HOST_TYPES).
Expand All @@ -137,10 +136,10 @@ stop_modules() ->
fun({Module, _Args}) ->
gen_mod:stop_module_keep_config(Host, Module)
end,
case ejabberd_config:get_local_option({modules, Host}) of
undefined ->
case mongoose_config:lookup_opt({modules, Host}) of
{error, not_found} ->
ok;
Modules ->
{ok, Modules} ->
lists:foreach(StopModuleFun, Modules)
end
end, ?ALL_HOST_TYPES).
Expand All @@ -149,7 +148,7 @@ stop_modules() ->
start_services() ->
lists:foreach(
fun({Service, Opts}) -> mongoose_service:ensure_loaded(Service, Opts) end,
ejabberd_config:get_local_option_or_default(services, [])
mongoose_config:get_opt(services, [])
).

-spec stop_services() -> ok.
Expand All @@ -159,17 +158,6 @@ stop_services() ->
mongoose_service:loaded_services_with_opts()
).

-spec connect_nodes() -> 'ok'.
connect_nodes() ->
case ejabberd_config:get_local_option(cluster_nodes) of
undefined ->
ok;
Nodes when is_list(Nodes) ->
lists:foreach(fun(Node) ->
net_kernel:connect_node(Node)
end, Nodes)
end.

-spec broadcast_c2s_shutdown() -> 'ok'.
broadcast_c2s_shutdown() ->
Children = supervisor:which_children(ejabberd_c2s_sup),
Expand Down
Loading

0 comments on commit dc88ddc

Please sign in to comment.