2003-01-24 21:18:33 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : gen_mod.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2013-03-14 10:33:02 +01:00
|
|
|
%%% Purpose :
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 24 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2015-08-05 09:52:54 +02:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2003-01-24 21:18:33 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(gen_mod).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
2017-02-14 10:39:26 +01:00
|
|
|
-behaviour(supervisor).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2017-02-14 10:39:26 +01:00
|
|
|
-export([init/1, start_link/0, start_child/3, start_child/4,
|
2017-02-22 17:46:47 +01:00
|
|
|
stop_child/1, stop_child/2, config_reloaded/0]).
|
2018-03-13 16:18:53 +01:00
|
|
|
-export([start_module/2, stop_module/2, stop_module_keep_config/2,
|
2018-01-23 08:54:52 +01:00
|
|
|
get_opt/2, get_opt_hosts/2, opt_type/1, is_equal_opt/3,
|
|
|
|
get_module_opt/3, get_module_opt_host/3,
|
2015-06-01 14:38:27 +02:00
|
|
|
loaded_modules/1, loaded_modules_with_opts/1,
|
2017-02-24 14:31:39 +01:00
|
|
|
get_hosts/2, get_module_proc/2, is_loaded/2, is_loaded_elsewhere/2,
|
2016-03-29 15:25:24 +02:00
|
|
|
start_modules/0, start_modules/1, stop_modules/0, stop_modules/1,
|
2017-01-13 10:03:39 +01:00
|
|
|
db_mod/2, db_mod/3, ram_db_mod/2, ram_db_mod/3,
|
2018-01-23 08:54:52 +01:00
|
|
|
is_db_configured/2]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
%% Deprecated functions
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([get_opt/3, get_opt/4, get_module_opt/4, get_module_opt/5,
|
|
|
|
get_opt_host/3, get_opt_hosts/3, db_type/2, db_type/3,
|
|
|
|
ram_db_type/2, ram_db_type/3]).
|
|
|
|
-deprecated([{get_opt, 3},
|
|
|
|
{get_opt, 4},
|
|
|
|
{get_opt_host, 3},
|
|
|
|
{get_opt_hosts, 3},
|
|
|
|
{get_module_opt, 4},
|
|
|
|
{get_module_opt, 5},
|
|
|
|
{db_type, 2},
|
|
|
|
{db_type, 3},
|
|
|
|
{ram_db_type, 2},
|
|
|
|
{ram_db_type, 3}]).
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2017-02-22 17:46:47 +01:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(ejabberd_module,
|
|
|
|
{module_host = {undefined, <<"">>} :: {atom(), binary()},
|
2018-03-13 16:18:53 +01:00
|
|
|
opts = [] :: opts() | '_' | '$2',
|
|
|
|
order = 0 :: integer()}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-type opts() :: [{atom(), any()}].
|
2017-02-16 12:18:36 +01:00
|
|
|
-type db_type() :: atom().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2018-09-17 11:08:04 +02:00
|
|
|
-callback start(binary(), opts()) -> ok | {ok, pid()} | {error, term()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
-callback stop(binary()) -> any().
|
2017-02-22 17:46:47 +01:00
|
|
|
-callback reload(binary(), opts(), opts()) -> ok | {ok, pid()}.
|
2016-05-01 22:09:40 +02:00
|
|
|
-callback mod_opt_type(atom()) -> fun((term()) -> term()) | [atom()].
|
2018-01-23 08:54:52 +01:00
|
|
|
-callback mod_options(binary()) -> opts().
|
2016-07-06 13:58:48 +02:00
|
|
|
-callback depends(binary(), opts()) -> [{module(), hard | soft}].
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-optional_callbacks([mod_opt_type/1, reload/3]).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export_type([opts/0]).
|
2015-06-01 14:38:27 +02:00
|
|
|
-export_type([db_type/0]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-02-14 10:39:26 +01:00
|
|
|
-ifndef(GEN_SERVER).
|
|
|
|
-define(GEN_SERVER, gen_server).
|
|
|
|
-endif.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2017-02-14 10:39:26 +01:00
|
|
|
start_link() ->
|
2017-02-24 10:05:47 +01:00
|
|
|
case supervisor:start_link({local, ejabberd_gen_mod_sup}, ?MODULE, []) of
|
|
|
|
{ok, Pid} ->
|
2017-04-30 18:01:47 +02:00
|
|
|
start_modules(),
|
2017-02-24 10:05:47 +01:00
|
|
|
{ok, Pid};
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2017-02-14 10:39:26 +01:00
|
|
|
|
|
|
|
init([]) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 50),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:add(host_up, ?MODULE, start_modules, 40),
|
2017-02-23 14:19:22 +01:00
|
|
|
ejabberd_hooks:add(host_down, ?MODULE, stop_modules, 70),
|
2013-03-14 10:33:02 +01:00
|
|
|
ets:new(ejabberd_modules,
|
|
|
|
[named_table, public,
|
2017-04-05 14:10:18 +02:00
|
|
|
{keypos, #ejabberd_module.module_host},
|
|
|
|
{read_concurrency, true}]),
|
2017-02-14 10:39:26 +01:00
|
|
|
{ok, {{one_for_one, 10, 1}, []}}.
|
|
|
|
|
|
|
|
-spec start_child(module(), binary() | global, opts()) -> ok | {error, any()}.
|
|
|
|
start_child(Mod, Host, Opts) ->
|
|
|
|
start_child(Mod, Host, Opts, get_module_proc(Host, Mod)).
|
|
|
|
|
|
|
|
-spec start_child(module(), binary() | global, opts(), atom()) -> ok | {error, any()}.
|
|
|
|
start_child(Mod, Host, Opts, Proc) ->
|
|
|
|
Spec = {Proc, {?GEN_SERVER, start_link,
|
|
|
|
[{local, Proc}, Mod, [Host, Opts], []]},
|
2017-05-17 16:33:07 +02:00
|
|
|
transient, timer:minutes(1), worker, [Mod]},
|
2017-02-14 10:39:26 +01:00
|
|
|
supervisor:start_child(ejabberd_gen_mod_sup, Spec).
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec stop_child(module(), binary() | global) -> ok | {error, any()}.
|
2017-02-14 10:39:26 +01:00
|
|
|
stop_child(Mod, Host) ->
|
|
|
|
stop_child(get_module_proc(Host, Mod)).
|
|
|
|
|
|
|
|
-spec stop_child(atom()) -> ok | {error, any()}.
|
|
|
|
stop_child(Proc) ->
|
|
|
|
supervisor:terminate_child(ejabberd_gen_mod_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_gen_mod_sup, Proc).
|
2003-01-28 20:45:13 +01:00
|
|
|
|
2016-03-29 15:25:24 +02:00
|
|
|
-spec start_modules() -> any().
|
|
|
|
|
|
|
|
%% Start all the modules in all the hosts
|
|
|
|
start_modules() ->
|
2018-06-14 13:00:47 +02:00
|
|
|
Hosts = ejabberd_config:get_myhosts(),
|
2018-06-14 09:18:10 +02:00
|
|
|
?INFO_MSG("Loading modules for ~s", [format_hosts_list(Hosts)]),
|
|
|
|
lists:foreach(fun start_modules/1, Hosts).
|
2016-03-29 15:25:24 +02:00
|
|
|
|
|
|
|
get_modules_options(Host) ->
|
2018-01-30 19:44:27 +01:00
|
|
|
sort_modules(Host, ejabberd_config:get_option({modules, Host}, [])).
|
2016-07-06 13:58:48 +02:00
|
|
|
|
|
|
|
sort_modules(Host, ModOpts) ->
|
|
|
|
G = digraph:new([acyclic]),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Mod, Opts}) ->
|
|
|
|
digraph:add_vertex(G, Mod, Opts),
|
|
|
|
Deps = try Mod:depends(Host, Opts) catch _:undef -> [] end,
|
|
|
|
lists:foreach(
|
|
|
|
fun({DepMod, Type}) ->
|
|
|
|
case lists:keyfind(DepMod, 1, ModOpts) of
|
|
|
|
false when Type == hard ->
|
|
|
|
ErrTxt = io_lib:format(
|
2018-01-23 08:54:52 +01:00
|
|
|
"Failed to load module '~s' "
|
2016-07-06 13:58:48 +02:00
|
|
|
"because it depends on module '~s' "
|
|
|
|
"which is not found in the config",
|
|
|
|
[Mod, DepMod]),
|
|
|
|
?ERROR_MSG(ErrTxt, []),
|
|
|
|
digraph:del_vertex(G, Mod),
|
2018-03-29 11:14:31 +02:00
|
|
|
maybe_halt_ejabberd();
|
2016-07-06 13:58:48 +02:00
|
|
|
false when Type == soft ->
|
2018-01-23 08:54:52 +01:00
|
|
|
?WARNING_MSG("Module '~s' is recommended for "
|
2016-07-06 13:58:48 +02:00
|
|
|
"module '~s' but is not found in "
|
|
|
|
"the config",
|
|
|
|
[DepMod, Mod]);
|
|
|
|
{DepMod, DepOpts} ->
|
|
|
|
digraph:add_vertex(G, DepMod, DepOpts),
|
2016-07-07 10:34:17 +02:00
|
|
|
case digraph:add_edge(G, DepMod, Mod) of
|
|
|
|
{error, {bad_edge, Path}} ->
|
2018-01-23 08:54:52 +01:00
|
|
|
?WARNING_MSG("Cyclic dependency detected "
|
2016-07-07 10:34:17 +02:00
|
|
|
"between modules: ~p",
|
|
|
|
[Path]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
2016-07-06 13:58:48 +02:00
|
|
|
end
|
|
|
|
end, Deps)
|
|
|
|
end, ModOpts),
|
2018-03-13 16:18:53 +01:00
|
|
|
{Result, _} = lists:mapfoldl(
|
|
|
|
fun(V, Order) ->
|
|
|
|
{M, O} = digraph:vertex(G, V),
|
|
|
|
{{M, O, Order}, Order+1}
|
|
|
|
end, 1, digraph_utils:topsort(G)),
|
2017-05-01 09:14:00 +02:00
|
|
|
digraph:delete(G),
|
|
|
|
Result.
|
2016-03-29 15:25:24 +02:00
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec start_modules(binary()) -> ok.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
start_modules(Host) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Modules = get_modules_options(Host),
|
2015-06-01 14:38:27 +02:00
|
|
|
lists:foreach(
|
2018-03-13 16:18:53 +01:00
|
|
|
fun({Module, Opts, Order}) ->
|
|
|
|
start_module(Host, Module, Opts, Order)
|
2016-03-29 15:25:24 +02:00
|
|
|
end, Modules).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec start_module(binary(), atom()) -> ok | {ok, pid()} | {error, not_found_in_config}.
|
2014-07-08 18:57:43 +02:00
|
|
|
|
|
|
|
start_module(Host, Module) ->
|
2016-03-29 15:25:24 +02:00
|
|
|
Modules = get_modules_options(Host),
|
2014-07-08 18:57:43 +02:00
|
|
|
case lists:keyfind(Module, 1, Modules) of
|
2018-03-13 16:18:53 +01:00
|
|
|
{_, Opts, Order} ->
|
|
|
|
start_module(Host, Module, Opts, Order);
|
2014-07-08 18:57:43 +02:00
|
|
|
false ->
|
|
|
|
{error, not_found_in_config}
|
|
|
|
end.
|
|
|
|
|
2018-03-13 16:18:53 +01:00
|
|
|
-spec start_module(binary(), atom(), opts(), integer()) -> ok | {ok, pid()}.
|
|
|
|
start_module(Host, Module, Opts, Order) ->
|
|
|
|
start_module(Host, Module, Opts, Order, true).
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2018-03-13 16:18:53 +01:00
|
|
|
-spec start_module(binary(), atom(), opts(), integer(), boolean()) -> ok | {ok, pid()}.
|
|
|
|
start_module(Host, Module, Opts0, Order, NeedValidation) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
?DEBUG("Loading ~s at ~s", [Module, Host]),
|
|
|
|
Res = if NeedValidation ->
|
|
|
|
validate_opts(Host, Module, Opts0);
|
|
|
|
true ->
|
|
|
|
{ok, Opts0}
|
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
{ok, Opts} ->
|
2018-03-13 16:18:53 +01:00
|
|
|
store_options(Host, Module, Opts, Order),
|
2018-01-23 08:54:52 +01:00
|
|
|
try case Module:start(Host, Opts) of
|
|
|
|
ok -> ok;
|
|
|
|
{ok, Pid} when is_pid(Pid) -> {ok, Pid};
|
2018-05-28 08:19:49 +02:00
|
|
|
Err -> erlang:error({bad_return, Module, Err})
|
2018-01-23 08:54:52 +01:00
|
|
|
end
|
|
|
|
catch Class:Reason ->
|
2018-06-03 17:53:30 +02:00
|
|
|
StackTrace = erlang:get_stacktrace(),
|
2018-01-23 08:54:52 +01:00
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
2018-06-03 17:53:30 +02:00
|
|
|
ErrorText = format_module_error(
|
|
|
|
Module, start, 2,
|
|
|
|
Opts, Class, Reason,
|
|
|
|
StackTrace),
|
2018-01-23 08:54:52 +01:00
|
|
|
?CRITICAL_MSG(ErrorText, []),
|
2018-03-29 11:14:31 +02:00
|
|
|
maybe_halt_ejabberd(),
|
2018-06-03 17:53:30 +02:00
|
|
|
erlang:raise(Class, Reason, StackTrace)
|
2018-01-23 08:54:52 +01:00
|
|
|
end;
|
2018-03-29 11:14:31 +02:00
|
|
|
{error, _ErrorText} ->
|
|
|
|
maybe_halt_ejabberd()
|
2016-07-06 13:58:48 +02:00
|
|
|
end.
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-spec reload_modules(binary()) -> ok.
|
|
|
|
reload_modules(Host) ->
|
2018-03-13 16:18:53 +01:00
|
|
|
NewMods = get_modules_options(Host),
|
|
|
|
OldMods = lists:reverse(loaded_modules_with_opts(Host)),
|
2017-02-22 17:46:47 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun({Mod, _Opts}) ->
|
|
|
|
case lists:keymember(Mod, 1, NewMods) of
|
|
|
|
false ->
|
|
|
|
stop_module(Host, Mod);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, OldMods),
|
|
|
|
lists:foreach(
|
2018-03-13 16:18:53 +01:00
|
|
|
fun({Mod, Opts, Order}) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
case lists:keymember(Mod, 1, OldMods) of
|
|
|
|
false ->
|
2018-03-13 16:18:53 +01:00
|
|
|
start_module(Host, Mod, Opts, Order);
|
2017-02-22 17:46:47 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, NewMods),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Mod, OldOpts}) ->
|
|
|
|
case lists:keyfind(Mod, 1, NewMods) of
|
2018-03-13 16:18:53 +01:00
|
|
|
{_, NewOpts0, Order} ->
|
2017-05-04 16:34:32 +02:00
|
|
|
case validate_opts(Host, Mod, NewOpts0) of
|
2018-01-23 08:54:52 +01:00
|
|
|
{ok, OldOpts} ->
|
2017-05-01 13:01:12 +02:00
|
|
|
ok;
|
2018-01-23 08:54:52 +01:00
|
|
|
{ok, NewOpts} ->
|
2018-03-13 16:18:53 +01:00
|
|
|
reload_module(Host, Mod, NewOpts, OldOpts, Order);
|
2018-01-23 08:54:52 +01:00
|
|
|
{error, _} ->
|
|
|
|
ok
|
2017-05-01 13:01:12 +02:00
|
|
|
end;
|
2017-02-22 17:46:47 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, OldMods).
|
|
|
|
|
2018-03-13 16:18:53 +01:00
|
|
|
-spec reload_module(binary(), module(), opts(), opts(), integer()) -> ok | {ok, pid()}.
|
|
|
|
reload_module(Host, Module, NewOpts, OldOpts, Order) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
case erlang:function_exported(Module, reload, 3) of
|
|
|
|
true ->
|
2018-01-23 08:54:52 +01:00
|
|
|
?DEBUG("Reloading ~s at ~s", [Module, Host]),
|
2018-03-13 16:18:53 +01:00
|
|
|
store_options(Host, Module, NewOpts, Order),
|
2017-02-22 17:46:47 +01:00
|
|
|
try case Module:reload(Host, NewOpts, OldOpts) of
|
|
|
|
ok -> ok;
|
|
|
|
{ok, Pid} when is_pid(Pid) -> {ok, Pid};
|
2018-06-03 17:53:30 +02:00
|
|
|
Err -> erlang:error({bad_return, Module, Err})
|
2017-02-22 17:46:47 +01:00
|
|
|
end
|
|
|
|
catch Class:Reason ->
|
|
|
|
StackTrace = erlang:get_stacktrace(),
|
2018-06-03 17:53:30 +02:00
|
|
|
ErrorText = format_module_error(
|
|
|
|
Module, reload, 3,
|
|
|
|
NewOpts, Class, Reason,
|
|
|
|
StackTrace),
|
|
|
|
?CRITICAL_MSG(ErrorText, []),
|
2017-02-22 17:46:47 +01:00
|
|
|
erlang:raise(Class, Reason, StackTrace)
|
|
|
|
end;
|
|
|
|
false ->
|
2018-01-23 08:54:52 +01:00
|
|
|
?WARNING_MSG("Module ~s doesn't support reloading "
|
2017-02-22 17:46:47 +01:00
|
|
|
"and will be restarted", [Module]),
|
|
|
|
stop_module(Host, Module),
|
2018-03-13 16:18:53 +01:00
|
|
|
start_module(Host, Module, NewOpts, Order, false)
|
2017-02-22 17:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2018-03-13 16:18:53 +01:00
|
|
|
-spec store_options(binary(), module(), opts(), integer()) -> true.
|
|
|
|
store_options(Host, Module, Opts, Order) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
ets:insert(ejabberd_modules,
|
|
|
|
#ejabberd_module{module_host = {Module, Host},
|
2018-03-13 16:18:53 +01:00
|
|
|
opts = Opts, order = Order}).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2018-03-29 11:14:31 +02:00
|
|
|
maybe_halt_ejabberd() ->
|
2016-07-06 13:58:48 +02:00
|
|
|
case is_app_running(ejabberd) of
|
|
|
|
false ->
|
|
|
|
?CRITICAL_MSG("ejabberd initialization was aborted "
|
|
|
|
"because a module start failed.",
|
|
|
|
[]),
|
2018-03-29 11:14:31 +02:00
|
|
|
ejabberd:halt();
|
2016-07-06 13:58:48 +02:00
|
|
|
true ->
|
|
|
|
ok
|
2003-01-24 21:18:33 +01:00
|
|
|
end.
|
|
|
|
|
2011-05-16 22:33:08 +02:00
|
|
|
is_app_running(AppName) ->
|
|
|
|
Timeout = 15000,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:keymember(AppName, 1,
|
|
|
|
application:which_applications(Timeout)).
|
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec stop_modules() -> ok.
|
2016-03-29 15:25:24 +02:00
|
|
|
|
|
|
|
stop_modules() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
stop_modules(Host)
|
2018-06-14 13:00:47 +02:00
|
|
|
end, ejabberd_config:get_myhosts()).
|
2016-03-29 15:25:24 +02:00
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec stop_modules(binary()) -> ok.
|
2016-03-29 15:25:24 +02:00
|
|
|
|
|
|
|
stop_modules(Host) ->
|
2018-03-13 16:18:53 +01:00
|
|
|
Modules = lists:reverse(loaded_modules_with_opts(Host)),
|
2016-03-29 15:25:24 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({Module, _Args}) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
stop_module_keep_config(Host, Module)
|
2016-03-29 15:25:24 +02:00
|
|
|
end, Modules).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec stop_module(binary(), atom()) -> error | {aborted, any()} | {atomic, any()}.
|
2011-05-16 22:33:08 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop_module(Host, Module) ->
|
2008-07-30 20:11:14 +02:00
|
|
|
case stop_module_keep_config(Host, Module) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> error;
|
2013-08-12 14:25:05 +02:00
|
|
|
ok -> ok
|
2008-07-30 20:11:14 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec stop_module_keep_config(binary(), atom()) -> error | ok.
|
|
|
|
|
2008-07-30 20:11:14 +02:00
|
|
|
stop_module_keep_config(Host, Module) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
?DEBUG("Stopping ~s at ~s", [Module, Host]),
|
2005-06-20 05:18:13 +02:00
|
|
|
case catch Module:stop(Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} -> ?ERROR_MSG("~p", [Reason]), error;
|
|
|
|
{wait, ProcList} when is_list(ProcList) ->
|
|
|
|
lists:foreach(fun wait_for_process/1, ProcList),
|
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
|
|
|
ok;
|
|
|
|
{wait, Process} ->
|
|
|
|
wait_for_process(Process),
|
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
|
|
|
ok;
|
|
|
|
_ -> ets:delete(ejabberd_modules, {Module, Host}), ok
|
2003-01-28 20:45:13 +01:00
|
|
|
end.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2005-05-23 02:30:29 +02:00
|
|
|
wait_for_process(Process) ->
|
|
|
|
MonitorReference = erlang:monitor(process, Process),
|
|
|
|
wait_for_stop(Process, MonitorReference).
|
|
|
|
|
|
|
|
wait_for_stop(Process, MonitorReference) ->
|
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
{'DOWN', MonitorReference, _Type, _Object, _Info} -> ok
|
|
|
|
after 5000 ->
|
|
|
|
catch exit(whereis(Process), kill),
|
|
|
|
wait_for_stop1(MonitorReference)
|
2005-05-23 02:30:29 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
wait_for_stop1(MonitorReference) ->
|
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
{'DOWN', MonitorReference, _Type, _Object, _Info} -> ok
|
|
|
|
after 5000 -> ok
|
2005-05-23 02:30:29 +02:00
|
|
|
end.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-type check_fun() :: fun((any()) -> any()) | {module(), atom()}.
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec get_opt(atom(), opts()) -> any().
|
2017-04-30 18:01:47 +02:00
|
|
|
get_opt(Opt, Opts) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
case lists:keyfind(Opt, 1, Opts) of
|
|
|
|
{_, Val} -> Val;
|
|
|
|
false ->
|
|
|
|
?DEBUG("Attempt to read unspecified option ~s", [Opt]),
|
|
|
|
undefined
|
|
|
|
end.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec get_opt(atom(), opts(), check_fun() | any()) -> any().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
get_opt(Opt, Opts, F) when is_function(F) ->
|
|
|
|
get_opt(Opt, Opts, undefined);
|
|
|
|
get_opt(Opt, Opts, Default) ->
|
|
|
|
case lists:keyfind(Opt, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
|
|
|
Default;
|
2017-04-30 18:01:47 +02:00
|
|
|
{_, Val} ->
|
|
|
|
Val
|
2003-01-24 21:18:33 +01:00
|
|
|
end.
|
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
-spec get_opt(atom() | {atom(), binary()}, opts(), check_fun(), any()) -> any().
|
|
|
|
get_opt(Opt, Opts, _, Default) ->
|
|
|
|
get_opt(Opt, Opts, Default).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
-spec get_module_opt(global | binary(), atom(), atom()) -> any().
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
get_module_opt(Host, Module, Opt) ->
|
|
|
|
get_module_opt(Host, Module, Opt, undefined).
|
|
|
|
|
|
|
|
-spec get_module_opt(global | binary(), atom(), atom(), any()) -> any().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
get_module_opt(Host, Module, Opt, F) when is_function(F) ->
|
|
|
|
get_module_opt(Host, Module, Opt, undefined);
|
|
|
|
get_module_opt(global, Module, Opt, Default) ->
|
2018-06-14 13:00:47 +02:00
|
|
|
Hosts = ejabberd_config:get_myhosts(),
|
2013-03-14 10:33:02 +01:00
|
|
|
[Value | Values] = lists:map(fun (Host) ->
|
|
|
|
get_module_opt(Host, Module, Opt,
|
2017-04-30 18:01:47 +02:00
|
|
|
Default)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
Hosts),
|
|
|
|
Same_all = lists:all(fun (Other_value) ->
|
|
|
|
Other_value == Value
|
|
|
|
end,
|
|
|
|
Values),
|
|
|
|
case Same_all of
|
|
|
|
true -> Value;
|
|
|
|
false -> Default
|
|
|
|
end;
|
2017-04-30 18:01:47 +02:00
|
|
|
get_module_opt(Host, Module, Opt, Default) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
OptsList = ets:lookup(ejabberd_modules, {Module, Host}),
|
2004-07-11 22:51:54 +02:00
|
|
|
case OptsList of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] -> Default;
|
|
|
|
[#ejabberd_module{opts = Opts} | _] ->
|
2017-04-30 18:01:47 +02:00
|
|
|
get_opt(Opt, Opts, Default)
|
2004-07-11 22:51:54 +02:00
|
|
|
end.
|
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
-spec get_module_opt(global | binary(), atom(), atom(), check_fun(), any()) -> any().
|
|
|
|
get_module_opt(Host, Module, Opt, _, Default) ->
|
|
|
|
get_module_opt(Host, Module, Opt, Default).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_module_opt_host(global | binary(), atom(), binary()) -> binary().
|
|
|
|
|
2007-09-11 13:20:36 +02:00
|
|
|
get_module_opt_host(Host, Module, Default) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
Val = get_module_opt(Host, Module, host, Default),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_regexp:greplace(Val, <<"@HOST@">>, Host).
|
|
|
|
|
|
|
|
-spec get_opt_host(binary(), opts(), binary()) -> binary().
|
2007-09-11 13:20:36 +02:00
|
|
|
|
2007-08-25 19:24:00 +02:00
|
|
|
get_opt_host(Host, Opts, Default) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
Val = get_opt(host, Opts, Default),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_regexp:greplace(Val, <<"@HOST@">>, Host).
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec get_opt_hosts(binary(), opts()) -> [binary()].
|
|
|
|
get_opt_hosts(Host, Opts) ->
|
|
|
|
get_opt_hosts(Host, Opts, undefined).
|
2017-08-08 16:46:26 +02:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec get_opt_hosts(binary(), opts(), binary()) -> [binary()].
|
2017-08-08 16:46:26 +02:00
|
|
|
get_opt_hosts(Host, Opts, Default) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Vals = case get_opt(hosts, Opts) of
|
|
|
|
L when L == [] orelse L == undefined ->
|
|
|
|
case get_opt(host, Opts) of
|
|
|
|
undefined -> [Default];
|
|
|
|
H -> [H]
|
|
|
|
end;
|
|
|
|
L ->
|
|
|
|
L
|
2017-08-08 16:46:26 +02:00
|
|
|
end,
|
|
|
|
[ejabberd_regexp:greplace(V, <<"@HOST@">>, Host) || V <- Vals].
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec get_validators(binary(), {module(), [module()]}) -> list() | undef.
|
|
|
|
get_validators(Host, {Module, SubMods}) ->
|
|
|
|
Validators =
|
|
|
|
dict:to_list(
|
|
|
|
lists:foldl(
|
|
|
|
fun(Mod, D) ->
|
|
|
|
try list_known_opts(Host, Mod) of
|
|
|
|
Os ->
|
|
|
|
lists:foldl(
|
|
|
|
fun({Opt, SubOpt} = O, Acc) ->
|
|
|
|
SubF = Mod:mod_opt_type(O),
|
|
|
|
F = try Mod:mod_opt_type(Opt)
|
|
|
|
catch _:_ -> fun(X) -> X end
|
|
|
|
end,
|
|
|
|
dict:append_list(
|
|
|
|
Opt, [F, {SubOpt, [SubF]}], Acc);
|
|
|
|
(O, Acc) ->
|
|
|
|
F = Mod:mod_opt_type(O),
|
|
|
|
dict:store(O, [F], Acc)
|
|
|
|
end, D, Os)
|
|
|
|
catch _:undef ->
|
|
|
|
D
|
|
|
|
end
|
|
|
|
end, dict:new(), [Module|SubMods])),
|
|
|
|
case Validators of
|
|
|
|
[] ->
|
|
|
|
case have_validators(Module) of
|
|
|
|
false ->
|
2018-02-19 20:07:09 +01:00
|
|
|
case code:ensure_loaded(Module) of
|
|
|
|
{module, _} ->
|
|
|
|
?WARNING_MSG("Third-party module '~s' doesn't export "
|
|
|
|
"options validator; consider to upgrade "
|
|
|
|
"the module", [Module]);
|
|
|
|
_ ->
|
|
|
|
%% Silently ignore this, the error will be
|
|
|
|
%% generated later
|
|
|
|
ok
|
|
|
|
end,
|
2018-01-23 08:54:52 +01:00
|
|
|
undef;
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Validators
|
2016-11-04 12:57:57 +01:00
|
|
|
end.
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec have_validators(module()) -> boolean().
|
|
|
|
have_validators(Module) ->
|
|
|
|
erlang:function_exported(Module, mod_options, 1)
|
|
|
|
orelse erlang:function_exported(Module, mod_opt_type, 1).
|
|
|
|
|
|
|
|
-spec validate_opts(binary(), module(), opts()) -> {ok, opts()} | {error, string()}.
|
|
|
|
validate_opts(Host, Module, Opts0) ->
|
|
|
|
SubMods = get_submodules(Host, Module, Opts0),
|
|
|
|
DefaultOpts = lists:flatmap(
|
|
|
|
fun(M) ->
|
|
|
|
try M:mod_options(Host)
|
|
|
|
catch _:undef -> []
|
|
|
|
end
|
|
|
|
end, [Module|SubMods]),
|
|
|
|
try
|
2018-04-16 10:06:57 +02:00
|
|
|
Opts = merge_opts(Opts0, DefaultOpts, Module),
|
2018-01-23 08:54:52 +01:00
|
|
|
{ok, case get_validators(Host, {Module, SubMods}) of
|
|
|
|
undef ->
|
|
|
|
Opts;
|
|
|
|
Validators ->
|
2018-05-09 10:44:24 +02:00
|
|
|
Opts1 = validate_opts(Host, Module, Opts, Validators),
|
2018-03-07 15:46:16 +01:00
|
|
|
remove_duplicated_opts(Opts1)
|
2018-01-23 08:54:52 +01:00
|
|
|
end}
|
|
|
|
catch _:{missing_required_option, Opt} ->
|
|
|
|
ErrTxt = io_lib:format("Module '~s' is missing required option '~s'",
|
|
|
|
[Module, Opt]),
|
2018-05-09 10:44:24 +02:00
|
|
|
module_error(ErrTxt);
|
|
|
|
_:{invalid_option, Opt, Val} ->
|
2018-07-08 19:42:53 +02:00
|
|
|
ErrTxt = io_lib:format("Invalid value for option '~s' of "
|
|
|
|
"module ~s: ~s",
|
2018-09-17 11:08:04 +02:00
|
|
|
[Opt, Module, misc:format_val({yaml, Val})]),
|
2018-05-09 10:44:24 +02:00
|
|
|
module_error(ErrTxt);
|
|
|
|
_:{invalid_option, Opt, Val, Reason} ->
|
2018-07-08 19:42:53 +02:00
|
|
|
ErrTxt = io_lib:format("Invalid value for option '~s' of "
|
|
|
|
"module ~s (~s): ~s",
|
2018-09-17 11:08:04 +02:00
|
|
|
[Opt, Module, Reason, misc:format_val({yaml, Val})]),
|
2018-05-09 10:44:24 +02:00
|
|
|
module_error(ErrTxt);
|
|
|
|
_:{unknown_option, Opt, []} ->
|
|
|
|
ErrTxt = io_lib:format("Unknown option '~s' of module '~s': "
|
|
|
|
"the module doesn't have any options",
|
|
|
|
[Opt, Module]),
|
|
|
|
module_error(ErrTxt);
|
|
|
|
_:{unknown_option, Opt, KnownOpts} ->
|
|
|
|
ErrTxt = io_lib:format("Unknown option '~s' of module '~s',"
|
|
|
|
" available options are: ~s",
|
|
|
|
[Opt, Module,
|
|
|
|
misc:join_atoms(KnownOpts, <<", ">>)]),
|
|
|
|
module_error(ErrTxt)
|
2017-04-30 18:01:47 +02:00
|
|
|
end.
|
|
|
|
|
2018-05-09 10:44:24 +02:00
|
|
|
-spec module_error(iolist()) -> {error, iolist()}.
|
|
|
|
module_error(ErrTxt) ->
|
|
|
|
?ERROR_MSG(ErrTxt, []),
|
|
|
|
{error, ErrTxt}.
|
|
|
|
|
|
|
|
-spec err_invalid_option(atom(), any()) -> no_return().
|
|
|
|
err_invalid_option(Opt, Val) ->
|
|
|
|
erlang:error({invalid_option, Opt, Val}).
|
|
|
|
|
|
|
|
-spec err_invalid_option(atom(), any(), iolist()) -> no_return().
|
|
|
|
err_invalid_option(Opt, Val, Reason) ->
|
|
|
|
erlang:error({invalid_option, Opt, Val, Reason}).
|
|
|
|
|
|
|
|
-spec err_unknown_option(atom(), [atom()]) -> no_return().
|
|
|
|
err_unknown_option(Opt, KnownOpts) ->
|
|
|
|
erlang:error({unknown_option, Opt, KnownOpts}).
|
|
|
|
|
|
|
|
-spec err_missing_required_option(atom()) -> no_return().
|
|
|
|
err_missing_required_option(Opt) ->
|
|
|
|
erlang:error({missing_required_option, Opt}).
|
|
|
|
|
|
|
|
validate_opts(Host, Module, Opts, Validators) when is_list(Opts) ->
|
2017-05-04 16:34:32 +02:00
|
|
|
lists:flatmap(
|
|
|
|
fun({Opt, Val}) when is_atom(Opt) ->
|
|
|
|
case lists:keyfind(Opt, 1, Validators) of
|
2017-05-05 10:11:17 +02:00
|
|
|
{_, L} ->
|
|
|
|
case lists:partition(fun is_function/1, L) of
|
|
|
|
{[VFun|_], []} ->
|
2018-05-09 10:44:24 +02:00
|
|
|
validate_opt(Opt, Val, VFun);
|
2017-05-05 10:11:17 +02:00
|
|
|
{[VFun|_], SubValidators} ->
|
2018-05-09 10:44:24 +02:00
|
|
|
try validate_opts(Host, Module, Val, SubValidators) of
|
2017-05-05 10:11:17 +02:00
|
|
|
SubOpts ->
|
2018-05-09 10:44:24 +02:00
|
|
|
validate_opt(Opt, SubOpts, VFun)
|
2017-05-05 10:11:17 +02:00
|
|
|
catch _:bad_option ->
|
2018-05-09 10:44:24 +02:00
|
|
|
err_invalid_option(Opt, Val)
|
2017-05-05 10:11:17 +02:00
|
|
|
end
|
2015-06-01 14:38:27 +02:00
|
|
|
end;
|
2017-05-04 16:34:32 +02:00
|
|
|
false ->
|
2018-05-09 10:44:24 +02:00
|
|
|
err_unknown_option(Opt, [K || {K, _} <- Validators])
|
2015-06-01 14:38:27 +02:00
|
|
|
end;
|
2017-05-04 16:34:32 +02:00
|
|
|
(_) ->
|
|
|
|
erlang:error(bad_option)
|
|
|
|
end, Opts);
|
2018-05-09 10:44:24 +02:00
|
|
|
validate_opts(_, _, _, _) ->
|
2017-05-04 16:34:32 +02:00
|
|
|
erlang:error(bad_option).
|
|
|
|
|
2018-05-09 10:44:24 +02:00
|
|
|
-spec validate_opt(atom(), any(), check_fun()) -> [{atom(), any()}].
|
|
|
|
validate_opt(Opt, Val, VFun) ->
|
2017-05-04 16:34:32 +02:00
|
|
|
try VFun(Val) of
|
|
|
|
NewVal -> [{Opt, NewVal}]
|
|
|
|
catch {invalid_syntax, Error} ->
|
2018-05-09 10:44:24 +02:00
|
|
|
err_invalid_option(Opt, Val, Error);
|
2018-09-19 00:00:50 +02:00
|
|
|
_:R when R /= undef ->
|
2018-05-09 10:44:24 +02:00
|
|
|
err_invalid_option(Opt, Val)
|
2018-01-23 08:54:52 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec list_known_opts(binary(), module()) -> [atom() | {atom(), atom()}].
|
|
|
|
list_known_opts(Host, Module) ->
|
|
|
|
try Module:mod_options(Host) of
|
|
|
|
DefaultOpts ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun({Opt, [{A, _}|_] = Vals}) when is_atom(A) ->
|
|
|
|
[{Opt, Val} || {Val, _} <- Vals];
|
|
|
|
({Opt, _}) -> [Opt];
|
|
|
|
(Opt) -> [Opt]
|
|
|
|
end, DefaultOpts)
|
|
|
|
catch _:undef ->
|
|
|
|
Module:mod_opt_type('')
|
|
|
|
end.
|
|
|
|
|
2018-04-16 10:06:57 +02:00
|
|
|
-spec merge_opts(opts(), opts(), module()) -> opts().
|
|
|
|
merge_opts(Opts, DefaultOpts, Module) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Result =
|
|
|
|
lists:foldr(
|
|
|
|
fun({Opt, Default}, Acc) ->
|
|
|
|
case lists:keyfind(Opt, 1, Opts) of
|
|
|
|
{_, Val} ->
|
|
|
|
case Default of
|
|
|
|
[{A, _}|_] when is_atom(A) andalso is_list(Val) ->
|
2018-04-16 14:48:06 +02:00
|
|
|
case is_opt_list(Val) of
|
|
|
|
true ->
|
|
|
|
[{Opt, merge_opts(Val, Default, Module)}|Acc];
|
|
|
|
false ->
|
2018-05-09 10:44:24 +02:00
|
|
|
err_invalid_option(Opt, Val)
|
2018-04-16 10:06:57 +02:00
|
|
|
end;
|
2018-01-23 08:54:52 +01:00
|
|
|
Val ->
|
|
|
|
[{Opt, Default}|Acc];
|
|
|
|
_ ->
|
|
|
|
[{Opt, Val}, {Opt, Default}|Acc]
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[{Opt, Default}|Acc]
|
|
|
|
end;
|
|
|
|
(Opt, Acc) ->
|
|
|
|
case lists:keyfind(Opt, 1, Opts) of
|
|
|
|
{_, Val} ->
|
|
|
|
[{Opt, Val}|Acc];
|
|
|
|
false ->
|
2018-05-09 10:44:24 +02:00
|
|
|
err_missing_required_option(Opt)
|
2018-01-23 08:54:52 +01:00
|
|
|
end
|
|
|
|
end, [], DefaultOpts),
|
|
|
|
lists:foldl(
|
|
|
|
fun({Opt, Val}, Acc) ->
|
|
|
|
case lists:keymember(Opt, 1, Result) of
|
|
|
|
true -> Acc;
|
|
|
|
false -> [{Opt, Val}|Acc]
|
|
|
|
end
|
|
|
|
end, Result, Opts).
|
|
|
|
|
2018-03-07 15:46:16 +01:00
|
|
|
remove_duplicated_opts([{Opt, Val}, {Opt, _Default}|Opts]) ->
|
|
|
|
[{Opt, Val}|remove_duplicated_opts(Opts)];
|
|
|
|
remove_duplicated_opts([{Opt, [{SubOpt, _}|_] = SubOpts}|Opts])
|
|
|
|
when is_atom(SubOpt) ->
|
|
|
|
[{Opt, remove_duplicated_opts(SubOpts)}|remove_duplicated_opts(Opts)];
|
|
|
|
remove_duplicated_opts([OptVal|Opts]) ->
|
|
|
|
[OptVal|remove_duplicated_opts(Opts)];
|
|
|
|
remove_duplicated_opts([]) ->
|
|
|
|
[].
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec get_submodules(binary(), module(), opts()) -> [module()].
|
|
|
|
get_submodules(Host, Module, Opts) ->
|
|
|
|
try Module:mod_options(Host) of
|
|
|
|
DefaultOpts ->
|
|
|
|
Mod1 = case lists:keyfind(db_type, 1, DefaultOpts) of
|
|
|
|
{_, T1} ->
|
|
|
|
DBType = proplists:get_value(db_type, Opts, T1),
|
|
|
|
[db_mod(DBType, Module)];
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Mod2 = case lists:keyfind(ram_db_type, 1, DefaultOpts) of
|
|
|
|
{_, T2} ->
|
|
|
|
RamDBType = proplists:get_value(ram_db_type, Opts, T2),
|
|
|
|
[ram_db_mod(RamDBType, Module)];
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Mod1 ++ Mod2
|
|
|
|
catch _:undef ->
|
2017-05-04 16:34:32 +02:00
|
|
|
[]
|
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2018-06-03 17:53:30 +02:00
|
|
|
-spec format_module_error(atom(), start | reload, non_neg_integer(), opts(),
|
|
|
|
error | exit | throw, any(),
|
|
|
|
[erlang:stack_item()]) -> iolist().
|
|
|
|
format_module_error(Module, Fun, Arity, Opts, Class, Reason, St) ->
|
2018-05-28 08:19:49 +02:00
|
|
|
IsLoaded = code:ensure_loaded(Module) == {module, Module},
|
2018-06-03 17:53:30 +02:00
|
|
|
IsCallbackExported = erlang:function_exported(Module, Fun, Arity),
|
2018-05-28 08:19:49 +02:00
|
|
|
case {Class, Reason} of
|
|
|
|
{error, undef} when not IsLoaded ->
|
2018-06-03 17:53:30 +02:00
|
|
|
io_lib:format("Failed to ~s unknown module ~s: "
|
2018-05-28 08:19:49 +02:00
|
|
|
"make sure there is no typo and ~s.beam "
|
|
|
|
"exists inside either ~s or ~s "
|
|
|
|
"directory",
|
2018-06-03 17:53:30 +02:00
|
|
|
[Fun, Module, Module,
|
2018-05-28 08:19:49 +02:00
|
|
|
filename:dirname(code:which(?MODULE)),
|
|
|
|
ext_mod:modules_dir()]);
|
2018-06-03 17:53:30 +02:00
|
|
|
{error, undef} when not IsCallbackExported ->
|
|
|
|
io_lib:format("Failed to ~s module ~s because "
|
|
|
|
"it doesn't export ~s/~B callback: "
|
|
|
|
"is it really an ejabberd module?",
|
|
|
|
[Fun, Module, Fun, Arity]);
|
2018-09-17 11:08:04 +02:00
|
|
|
{error, {bad_return, Module, {error, _} = Err}} ->
|
|
|
|
io_lib:format("Failed to ~s module ~s: ~s",
|
|
|
|
[Fun, Module, misc:format_val(Err)]);
|
2018-05-28 08:19:49 +02:00
|
|
|
{error, {bad_return, Module, Ret}} ->
|
2018-08-28 09:41:57 +02:00
|
|
|
io_lib:format("Module ~s returned unexpected value from ~s/~B:~n"
|
|
|
|
"** Error: ~p~n"
|
|
|
|
"** Hint: this is either not an ejabberd module "
|
|
|
|
"or it implements ejabbed API incorrectly",
|
2018-06-03 17:53:30 +02:00
|
|
|
[Module, Fun, Arity, Ret]);
|
2018-05-28 08:19:49 +02:00
|
|
|
_ ->
|
|
|
|
io_lib:format("Internal error of module ~s has "
|
2018-06-03 17:53:30 +02:00
|
|
|
"occured during ~s:~n"
|
2018-05-28 08:19:49 +02:00
|
|
|
"** Options: ~p~n"
|
|
|
|
"** Class: ~p~n"
|
|
|
|
"** Reason: ~p~n"
|
|
|
|
"** Stacktrace: ~p",
|
2018-06-03 17:53:30 +02:00
|
|
|
[Module, Fun, Opts, Class, Reason, St])
|
2018-05-28 08:19:49 +02:00
|
|
|
end.
|
|
|
|
|
2018-06-14 09:18:10 +02:00
|
|
|
format_hosts_list([Host]) ->
|
|
|
|
Host;
|
|
|
|
format_hosts_list([H1, H2]) ->
|
|
|
|
[H1, " and ", H2];
|
|
|
|
format_hosts_list([H1, H2, H3]) ->
|
|
|
|
[H1, ", ", H2, " and ", H3];
|
|
|
|
format_hosts_list([H1, H2|Hs]) ->
|
|
|
|
io_lib:format("~s, ~s and ~B more hosts",
|
|
|
|
[H1, H2, length(Hs)]).
|
|
|
|
|
2016-04-27 16:10:50 +02:00
|
|
|
-spec db_type(binary() | global, module()) -> db_type();
|
|
|
|
(opts(), module()) -> db_type().
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2016-04-27 16:10:50 +02:00
|
|
|
db_type(Opts, Module) when is_list(Opts) ->
|
|
|
|
db_type(global, Opts, Module);
|
2015-03-30 11:15:29 +02:00
|
|
|
db_type(Host, Module) when is_atom(Module) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
case get_module_opt(Host, Module, db_type) of
|
|
|
|
undefined ->
|
|
|
|
ejabberd_config:default_db(Host, Module);
|
|
|
|
Type ->
|
|
|
|
Type
|
2016-04-27 16:10:50 +02:00
|
|
|
end.
|
2015-03-30 11:15:29 +02:00
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec db_type(binary() | global, opts(), module()) -> db_type().
|
2016-04-27 16:10:50 +02:00
|
|
|
|
|
|
|
db_type(Host, Opts, Module) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
case get_opt(db_type, Opts) of
|
|
|
|
undefined ->
|
|
|
|
ejabberd_config:default_db(Host, Module);
|
|
|
|
Type ->
|
|
|
|
Type
|
2016-04-27 16:10:50 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-04-13 08:56:10 +02:00
|
|
|
-spec db_mod(binary() | global | db_type(), module()) -> module().
|
|
|
|
|
2016-04-27 16:10:50 +02:00
|
|
|
db_mod(Type, Module) when is_atom(Type) ->
|
|
|
|
list_to_atom(atom_to_list(Module) ++ "_" ++ atom_to_list(Type));
|
2016-04-13 08:56:10 +02:00
|
|
|
db_mod(Host, Module) when is_binary(Host) orelse Host == global ->
|
|
|
|
db_mod(db_type(Host, Module), Module).
|
|
|
|
|
|
|
|
-spec db_mod(binary() | global, opts(), module()) -> module().
|
|
|
|
|
|
|
|
db_mod(Host, Opts, Module) when is_list(Opts) ->
|
2016-04-27 16:10:50 +02:00
|
|
|
db_mod(db_type(Host, Opts, Module), Module).
|
2016-04-13 08:56:10 +02:00
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec ram_db_type(binary() | global, module()) -> db_type();
|
|
|
|
(opts(), module()) -> db_type().
|
|
|
|
ram_db_type(Opts, Module) when is_list(Opts) ->
|
|
|
|
ram_db_type(global, Opts, Module);
|
|
|
|
ram_db_type(Host, Module) when is_atom(Module) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
case get_module_opt(Host, Module, ram_db_type) of
|
|
|
|
undefined ->
|
|
|
|
ejabberd_config:default_ram_db(Host, Module);
|
|
|
|
Type ->
|
|
|
|
Type
|
2017-01-13 10:03:39 +01:00
|
|
|
end.
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec ram_db_type(binary() | global, opts(), module()) -> db_type().
|
2017-01-13 10:03:39 +01:00
|
|
|
ram_db_type(Host, Opts, Module) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
case get_opt(ram_db_type, Opts) of
|
|
|
|
undefined ->
|
|
|
|
ejabberd_config:default_ram_db(Host, Module);
|
|
|
|
Type ->
|
|
|
|
Type
|
2017-01-13 10:03:39 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec ram_db_mod(binary() | global | db_type(), module()) -> module().
|
|
|
|
ram_db_mod(Type, Module) when is_atom(Type), Type /= global ->
|
|
|
|
list_to_atom(atom_to_list(Module) ++ "_" ++ atom_to_list(Type));
|
|
|
|
ram_db_mod(Host, Module) when is_binary(Host) orelse Host == global ->
|
|
|
|
ram_db_mod(ram_db_type(Host, Module), Module).
|
|
|
|
|
|
|
|
-spec ram_db_mod(binary() | global, opts(), module()) -> module().
|
|
|
|
ram_db_mod(Host, Opts, Module) when is_list(Opts) ->
|
|
|
|
ram_db_mod(ram_db_type(Host, Opts, Module), Module).
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
is_db_configured(Type, Host) ->
|
|
|
|
lists:any(
|
|
|
|
fun(#ejabberd_module{module_host = {_, H}, opts = Opts})
|
|
|
|
when H == Host orelse Host == global ->
|
|
|
|
case lists:keyfind(db_type, 1, Opts) of
|
|
|
|
{_, Type} -> true;
|
|
|
|
_ ->
|
|
|
|
case lists:keyfind(ram_db_type, 1, Opts) of
|
|
|
|
{_, Type} -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
false
|
|
|
|
end, ets:tab2list(ejabberd_modules)).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec loaded_modules(binary()) -> [atom()].
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
loaded_modules(Host) ->
|
2018-03-13 16:18:53 +01:00
|
|
|
Mods = ets:select(
|
|
|
|
ejabberd_modules,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#ejabberd_module{module_host = {Mod, H},
|
|
|
|
order = Order}) when H == Host ->
|
|
|
|
{Mod, Order}
|
|
|
|
end)),
|
|
|
|
[Mod || {Mod, _} <- lists:keysort(2, Mods)].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec loaded_modules_with_opts(binary()) -> [{atom(), opts()}].
|
2005-05-23 02:30:29 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
loaded_modules_with_opts(Host) ->
|
2018-03-13 16:18:53 +01:00
|
|
|
Mods = ets:select(
|
|
|
|
ejabberd_modules,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#ejabberd_module{module_host = {Mod, H}, opts = Opts,
|
|
|
|
order = Order}) when H == Host ->
|
|
|
|
{Mod, Opts, Order}
|
|
|
|
end)),
|
|
|
|
[{Mod, Opts} || {Mod, Opts, _} <- lists:keysort(3, Mods)].
|
2004-07-11 22:51:54 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_hosts(opts(), binary()) -> [binary()].
|
2007-11-27 23:30:51 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_hosts(Opts, Prefix) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
case get_opt(hosts, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
2017-04-30 18:01:47 +02:00
|
|
|
case get_opt(host, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined ->
|
2018-06-14 13:00:47 +02:00
|
|
|
[<<Prefix/binary, Host/binary>> || Host <- ejabberd_config:get_myhosts()];
|
2013-03-14 10:33:02 +01:00
|
|
|
Host ->
|
|
|
|
[Host]
|
|
|
|
end;
|
|
|
|
Hosts ->
|
|
|
|
Hosts
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
2005-06-20 05:18:13 +02:00
|
|
|
|
2018-04-04 18:25:19 +02:00
|
|
|
-spec get_module_proc(binary() | global, atom()) -> atom().
|
|
|
|
get_module_proc(global, Base) ->
|
|
|
|
get_module_proc(<<"global">>, Base);
|
2005-06-20 05:18:13 +02:00
|
|
|
get_module_proc(Host, Base) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
binary_to_atom(
|
|
|
|
<<(erlang:atom_to_binary(Base, latin1))/binary, "_", Host/binary>>,
|
|
|
|
latin1).
|
|
|
|
|
|
|
|
-spec is_loaded(binary(), atom()) -> boolean().
|
2005-06-20 05:18:13 +02:00
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
is_loaded(Host, Module) ->
|
|
|
|
ets:member(ejabberd_modules, {Module, Host}).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-02-24 14:31:39 +01:00
|
|
|
-spec is_loaded_elsewhere(binary(), atom()) -> boolean().
|
|
|
|
is_loaded_elsewhere(Host, Module) ->
|
|
|
|
ets:select_count(
|
|
|
|
ejabberd_modules,
|
|
|
|
ets:fun2ms(
|
|
|
|
fun(#ejabberd_module{module_host = {Mod, H}}) ->
|
|
|
|
(Mod == Module) and (H /= Host)
|
|
|
|
end)) /= 0.
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-spec config_reloaded() -> ok.
|
|
|
|
config_reloaded() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
|
|
|
reload_modules(Host)
|
2018-06-14 13:00:47 +02:00
|
|
|
end, ejabberd_config:get_myhosts()).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec is_equal_opt(atom(), opts(), opts()) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
true | {false, any(), any()}.
|
2018-01-23 08:54:52 +01:00
|
|
|
is_equal_opt(Opt, NewOpts, OldOpts) ->
|
|
|
|
NewVal = get_opt(Opt, NewOpts),
|
|
|
|
OldVal = get_opt(Opt, OldOpts),
|
2017-02-22 17:46:47 +01:00
|
|
|
if NewVal /= OldVal ->
|
|
|
|
{false, NewVal, OldVal};
|
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
2018-04-16 14:48:06 +02:00
|
|
|
-spec is_opt_list(term()) -> boolean().
|
|
|
|
is_opt_list([]) ->
|
|
|
|
true;
|
|
|
|
is_opt_list(L) when is_list(L) ->
|
|
|
|
lists:all(
|
|
|
|
fun({Opt, _Val}) -> is_atom(Opt);
|
|
|
|
(_) -> false
|
|
|
|
end, L);
|
|
|
|
is_opt_list(_) ->
|
|
|
|
false.
|
|
|
|
|
2018-09-09 08:59:08 +02:00
|
|
|
-spec opt_type(atom()) -> fun((any()) -> any()) | [atom()].
|
2017-02-22 17:46:47 +01:00
|
|
|
opt_type(modules) ->
|
|
|
|
fun(Mods) ->
|
|
|
|
lists:map(
|
2018-04-16 14:48:06 +02:00
|
|
|
fun({M, A}) when is_atom(M) ->
|
|
|
|
true = is_opt_list(A),
|
2017-02-22 17:46:47 +01:00
|
|
|
{M, A}
|
|
|
|
end, Mods)
|
|
|
|
end;
|
2017-01-11 14:25:43 +01:00
|
|
|
opt_type(_) -> [modules].
|