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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 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).
|
2017-02-14 10:39:26 +01:00
|
|
|
-behaviour(supervisor).
|
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,
|
2019-07-29 09:46:20 +02:00
|
|
|
stop_child/1, stop_child/2, stop/0, config_reloaded/0]).
|
2018-03-13 16:18:53 +01:00
|
|
|
-export([start_module/2, stop_module/2, stop_module_keep_config/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
get_opt/2, set_opt/3, get_opt_hosts/1, is_equal_opt/3,
|
2019-06-23 16:06:53 +02:00
|
|
|
get_module_opt/3, get_module_opts/2, get_module_opt_hosts/2,
|
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,
|
2019-06-14 11:33:26 +02:00
|
|
|
db_mod/2, ram_db_mod/2]).
|
|
|
|
-export([validate/2]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-04-30 18:01:47 +02:00
|
|
|
%% Deprecated functions
|
2019-06-14 11:33:26 +02:00
|
|
|
%% update_module/3 is used by test suite ONLY
|
|
|
|
-export([update_module/3]).
|
|
|
|
-deprecated([{update_module, 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").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.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',
|
2023-07-31 15:26:39 +02:00
|
|
|
registrations = [] :: [registration()],
|
2018-03-13 16:18:53 +01:00
|
|
|
order = 0 :: integer()}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-type opts() :: #{atom() => term()}.
|
2017-02-16 12:18:36 +01:00
|
|
|
-type db_type() :: atom().
|
2020-01-08 10:24:51 +01:00
|
|
|
-type opt_desc() :: #{desc => binary() | [binary()],
|
|
|
|
value => string() | binary()}.
|
|
|
|
-type opt_doc() :: {atom(), opt_desc()} | {atom(), opt_desc(), [opt_doc()]}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2023-07-31 15:26:39 +02:00
|
|
|
-type component() :: ejabberd_sm | ejabberd_local.
|
|
|
|
-type registration() ::
|
2023-08-01 17:14:08 +02:00
|
|
|
{hook, atom(), atom(), integer()} |
|
2023-07-31 15:26:39 +02:00
|
|
|
{hook, atom(), module(), atom(), integer()} |
|
2023-08-01 17:14:08 +02:00
|
|
|
{iq_handler, component(), binary(), atom()} |
|
2023-07-31 15:26:39 +02:00
|
|
|
{iq_handler, component(), binary(), module(), atom()}.
|
2023-08-09 16:08:45 +02:00
|
|
|
-export_type([registration/0]).
|
2023-07-31 15:26:39 +02:00
|
|
|
|
|
|
|
-callback start(binary(), opts()) ->
|
|
|
|
ok | {ok, pid()} |
|
|
|
|
{ok, [registration()]} | {error, term()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
-callback stop(binary()) -> any().
|
2019-08-06 11:31:33 +02:00
|
|
|
-callback reload(binary(), opts(), opts()) -> ok | {ok, pid()} | {error, term()}.
|
2019-06-14 11:33:26 +02:00
|
|
|
-callback mod_opt_type(atom()) -> econf:validator().
|
|
|
|
-callback mod_options(binary()) -> [{atom(), term()} | atom()].
|
2020-01-08 10:24:51 +01:00
|
|
|
-callback mod_doc() -> #{desc => binary() | [binary()],
|
|
|
|
opts => [opt_doc()],
|
|
|
|
example => [string()] | [{binary(), [string()]}]}.
|
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([]) ->
|
2020-04-22 01:08:48 +02:00
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 60),
|
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}, []}}.
|
|
|
|
|
2019-07-29 09:46:20 +02:00
|
|
|
-spec stop() -> ok.
|
|
|
|
stop() ->
|
2020-04-22 01:08:48 +02:00
|
|
|
ejabberd_hooks:delete(config_reloaded, ?MODULE, config_reloaded, 60),
|
2019-07-29 09:46:20 +02:00
|
|
|
ejabberd_hooks:delete(host_up, ?MODULE, start_modules, 40),
|
|
|
|
ejabberd_hooks:delete(host_down, ?MODULE, stop_modules, 70),
|
|
|
|
stop_modules(),
|
|
|
|
ejabberd_sup:stop_child(ejabberd_gen_mod_sup).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec start_child(module(), binary(), opts()) -> {ok, pid()} | {error, any()}.
|
2017-02-14 10:39:26 +01:00
|
|
|
start_child(Mod, Host, Opts) ->
|
|
|
|
start_child(Mod, Host, Opts, get_module_proc(Host, Mod)).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec start_child(module(), binary(), opts(), atom()) -> {ok, pid()} | {error, any()}.
|
2017-02-14 10:39:26 +01:00
|
|
|
start_child(Mod, Host, Opts, Proc) ->
|
|
|
|
Spec = {Proc, {?GEN_SERVER, start_link,
|
2019-07-04 09:26:59 +02:00
|
|
|
[{local, Proc}, Mod, [Host, Opts],
|
|
|
|
ejabberd_config:fsm_limit_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).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec stop_child(module(), binary()) -> 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_modules() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Hosts = ejabberd_option:hosts(),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Loading modules for ~ts", [misc:format_hosts_list(Hosts)]),
|
2018-06-14 09:18:10 +02:00
|
|
|
lists:foreach(fun start_modules/1, Hosts).
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Modules = ejabberd_option:modules(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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Modules = ejabberd_option:modules(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) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Loading ~ts at ~ts", [Module, Host]),
|
2019-06-14 11:33:26 +02:00
|
|
|
store_options(Host, Module, Opts, Order),
|
|
|
|
try case Module:start(Host, Opts) of
|
|
|
|
ok -> ok;
|
|
|
|
{ok, Pid} when is_pid(Pid) -> {ok, Pid};
|
2023-07-31 15:26:39 +02:00
|
|
|
{ok, Registrations} when is_list(Registrations) ->
|
|
|
|
store_options(Host, Module, Opts, Registrations, Order),
|
2023-08-01 17:14:08 +02:00
|
|
|
add_registrations(Host, Module, Registrations),
|
2023-07-31 15:26:39 +02:00
|
|
|
ok;
|
2019-06-14 11:33:26 +02:00
|
|
|
Err ->
|
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
|
|
|
erlang:error({bad_return, Module, Err})
|
|
|
|
end
|
|
|
|
catch ?EX_RULE(Class, Reason, Stack) ->
|
|
|
|
StackTrace = ?EX_STACK(Stack),
|
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
|
|
|
ErrorText = format_module_error(
|
|
|
|
Module, start, 2,
|
|
|
|
Opts, Class, Reason,
|
|
|
|
StackTrace),
|
|
|
|
?CRITICAL_MSG(ErrorText, []),
|
|
|
|
maybe_halt_ejabberd(),
|
|
|
|
erlang:raise(Class, Reason, StackTrace)
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
NewMods = ejabberd_option:modules(Host),
|
2018-03-13 16:18:53 +01:00
|
|
|
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
|
2019-06-14 11:33:26 +02:00
|
|
|
{_, NewOpts, Order} ->
|
|
|
|
if OldOpts /= NewOpts ->
|
2018-03-13 16:18:53 +01:00
|
|
|
reload_module(Host, Mod, NewOpts, OldOpts, Order);
|
2019-06-14 11:33:26 +02:00
|
|
|
true ->
|
2018-01-23 08:54:52 +01:00
|
|
|
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 ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Reloading ~ts at ~ts", [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
|
2018-12-13 11:45:45 +01:00
|
|
|
catch ?EX_RULE(Class, Reason, Stack) ->
|
|
|
|
StackTrace = ?EX_STACK(Stack),
|
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 ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Module ~ts doesn't support reloading "
|
2017-02-22 17:46:47 +01:00
|
|
|
"and will be restarted", [Module]),
|
|
|
|
stop_module(Host, Module),
|
2019-06-14 11:33:26 +02:00
|
|
|
start_module(Host, Module, NewOpts, Order)
|
2017-02-22 17:46:47 +01:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec update_module(binary(), module(), opts()) -> ok | {ok, pid()}.
|
|
|
|
update_module(Host, Module, Opts) ->
|
2019-06-17 19:49:43 +02:00
|
|
|
case ets:lookup(ejabberd_modules, {Module, Host}) of
|
|
|
|
[#ejabberd_module{opts = OldOpts, order = Order}] ->
|
|
|
|
NewOpts = maps:merge(OldOpts, Opts),
|
|
|
|
reload_module(Host, Module, NewOpts, OldOpts, Order);
|
|
|
|
[] ->
|
|
|
|
erlang:error({module_not_loaded, Module, Host})
|
|
|
|
end.
|
2019-06-14 11:33:26 +02:00
|
|
|
|
2018-03-13 16:18:53 +01:00
|
|
|
-spec store_options(binary(), module(), opts(), integer()) -> true.
|
|
|
|
store_options(Host, Module, Opts, Order) ->
|
2023-07-31 15:26:39 +02:00
|
|
|
case ets:lookup(ejabberd_modules, {Module, Host}) of
|
|
|
|
[M] ->
|
|
|
|
store_options(
|
|
|
|
Host, Module, Opts, M#ejabberd_module.registrations, Order);
|
|
|
|
[] ->
|
|
|
|
store_options(Host, Module, Opts, [], Order)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec store_options(binary(), module(), opts(), [registration()], integer()) -> true.
|
|
|
|
store_options(Host, Module, Opts, Registrations, Order) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
ets:insert(ejabberd_modules,
|
|
|
|
#ejabberd_module{module_host = {Module, Host},
|
2023-07-31 15:26:39 +02:00
|
|
|
opts = Opts,
|
|
|
|
registrations = Registrations,
|
|
|
|
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(
|
2019-07-29 09:46:20 +02:00
|
|
|
fun(Host) ->
|
|
|
|
stop_modules(Host)
|
|
|
|
end, ejabberd_option:hosts()).
|
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).
|
|
|
|
|
2023-07-31 15:26:39 +02:00
|
|
|
-spec stop_module(binary(), atom()) -> error | ok.
|
2005-06-20 05:18:13 +02:00
|
|
|
stop_module(Host, Module) ->
|
2023-07-31 15:26:39 +02:00
|
|
|
stop_module_keep_config(Host, Module).
|
2008-07-30 20:11:14 +02:00
|
|
|
|
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) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Stopping ~ts at ~ts", [Module, Host]),
|
2023-07-31 15:26:39 +02:00
|
|
|
Registrations =
|
|
|
|
case ets:lookup(ejabberd_modules, {Module, Host}) of
|
|
|
|
[M] ->
|
|
|
|
M#ejabberd_module.registrations;
|
|
|
|
[] ->
|
|
|
|
[]
|
|
|
|
end,
|
2023-08-01 17:14:08 +02:00
|
|
|
del_registrations(Host, Module, Registrations),
|
2019-07-15 11:43:55 +02:00
|
|
|
try Module:stop(Host) of
|
|
|
|
_ ->
|
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
2019-04-24 13:46:09 +02:00
|
|
|
ok
|
2019-07-15 11:43:55 +02:00
|
|
|
catch ?EX_RULE(Class, Reason, St) ->
|
|
|
|
StackTrace = ?EX_STACK(St),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to stop module ~ts at ~ts:~n** ~ts",
|
2019-07-15 11:43:55 +02:00
|
|
|
[Module, Host,
|
|
|
|
misc:format_exception(2, Class, Reason, StackTrace)]),
|
|
|
|
error
|
2005-05-23 02:30:29 +02:00
|
|
|
end.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2023-08-01 17:14:08 +02:00
|
|
|
-spec add_registrations(binary(), module(), [registration()]) -> ok.
|
|
|
|
add_registrations(Host, Module, Registrations) ->
|
2023-07-31 15:26:39 +02:00
|
|
|
lists:foreach(
|
2023-08-01 17:14:08 +02:00
|
|
|
fun({hook, Hook, Function, Seq}) ->
|
2023-07-31 15:26:39 +02:00
|
|
|
ejabberd_hooks:add(Hook, Host, Module, Function, Seq);
|
2023-08-01 17:14:08 +02:00
|
|
|
({hook, Hook, Module1, Function, Seq}) ->
|
|
|
|
ejabberd_hooks:add(Hook, Host, Module1, Function, Seq);
|
|
|
|
({iq_handler, Component, NS, Function}) ->
|
2023-07-31 15:26:39 +02:00
|
|
|
gen_iq_handler:add_iq_handler(
|
2023-08-01 17:14:08 +02:00
|
|
|
Component, Host, NS, Module, Function);
|
|
|
|
({iq_handler, Component, NS, Module1, Function}) ->
|
|
|
|
gen_iq_handler:add_iq_handler(
|
|
|
|
Component, Host, NS, Module1, Function)
|
2023-07-31 15:26:39 +02:00
|
|
|
end, Registrations).
|
|
|
|
|
2023-08-01 17:14:08 +02:00
|
|
|
-spec del_registrations(binary(), module(), [registration()]) -> ok.
|
|
|
|
del_registrations(Host, Module, Registrations) ->
|
2023-07-31 15:26:39 +02:00
|
|
|
lists:foreach(
|
2023-08-01 17:14:08 +02:00
|
|
|
fun({hook, Hook, Function, Seq}) ->
|
2023-07-31 15:26:39 +02:00
|
|
|
ejabberd_hooks:delete(Hook, Host, Module, Function, Seq);
|
2023-08-01 17:14:08 +02:00
|
|
|
({hook, Hook, Module1, Function, Seq}) ->
|
|
|
|
ejabberd_hooks:delete(Hook, Host, Module1, Function, Seq);
|
|
|
|
({iq_handler, Component, NS, _Function}) ->
|
|
|
|
gen_iq_handler:remove_iq_handler(Component, Host, NS);
|
2023-07-31 15:26:39 +02:00
|
|
|
({iq_handler, Component, NS, _Module, _Function}) ->
|
|
|
|
gen_iq_handler:remove_iq_handler(Component, Host, NS)
|
|
|
|
end, Registrations).
|
|
|
|
|
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) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
maps:get(Opt, Opts).
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec set_opt(atom(), term(), opts()) -> opts().
|
|
|
|
set_opt(Opt, Val, Opts) ->
|
|
|
|
maps:put(Opt, Val, Opts).
|
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().
|
2019-06-14 11:33:26 +02:00
|
|
|
get_module_opt(global, Module, Opt) ->
|
|
|
|
get_module_opt(ejabberd_config:get_myname(), Module, Opt);
|
2017-04-30 18:01:47 +02:00
|
|
|
get_module_opt(Host, Module, Opt) ->
|
2019-06-23 16:06:53 +02:00
|
|
|
Opts = get_module_opts(Host, Module),
|
|
|
|
get_opt(Opt, Opts).
|
2015-03-30 11:15:29 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_module_opt_hosts(binary(), module()) -> [binary()].
|
|
|
|
get_module_opt_hosts(Host, Module) ->
|
2019-06-23 16:06:53 +02:00
|
|
|
Opts = get_module_opts(Host, Module),
|
|
|
|
get_opt_hosts(Opts).
|
2016-04-27 16:10:50 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_opt_hosts(opts()) -> [binary()].
|
|
|
|
get_opt_hosts(Opts) ->
|
|
|
|
case get_opt(hosts, Opts) of
|
|
|
|
L when L == [] orelse L == undefined ->
|
|
|
|
[get_opt(host, Opts)];
|
|
|
|
L ->
|
|
|
|
L
|
2016-04-27 16:10:50 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-23 16:06:53 +02:00
|
|
|
-spec get_module_opts(binary(), module()) -> opts().
|
|
|
|
get_module_opts(Host, Module) ->
|
|
|
|
try ets:lookup_element(ejabberd_modules, {Module, Host}, 3)
|
|
|
|
catch _:badarg -> erlang:error({module_not_loaded, Module, Host})
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec db_mod(binary() | global | db_type() | opts(), module()) -> module().
|
|
|
|
db_mod(T, M) ->
|
|
|
|
db_mod(db_type, T, M).
|
2016-04-13 08:56:10 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec ram_db_mod(binary() | global | db_type() | opts(), module()) -> module().
|
|
|
|
ram_db_mod(T, M) ->
|
|
|
|
db_mod(ram_db_type, T, M).
|
2016-04-13 08:56:10 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec db_mod(db_type | ram_db_type,
|
|
|
|
binary() | global | db_type() | opts(), module()) -> module().
|
|
|
|
db_mod(Opt, Host, Module) when is_binary(Host) orelse Host == global ->
|
|
|
|
db_mod(Opt, get_module_opt(Host, Module, Opt), Module);
|
|
|
|
db_mod(Opt, Opts, Module) when is_map(Opts) ->
|
|
|
|
db_mod(Opt, get_opt(Opt, Opts), Module);
|
|
|
|
db_mod(_Opt, Type, Module) when is_atom(Type) ->
|
2019-06-15 14:14:45 +02:00
|
|
|
list_to_existing_atom(atom_to_list(Module) ++ "_" ++ atom_to_list(Type)).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec loaded_modules(binary()) -> [atom()].
|
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-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()].
|
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 ->
|
2019-06-14 11:33:26 +02:00
|
|
|
[<<Prefix/binary, Host/binary>> || Host <- ejabberd_option:hosts()];
|
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().
|
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() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
lists:foreach(fun reload_modules/1, ejabberd_option:hosts()).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec is_equal_opt(atom(), opts(), opts()) -> 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.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Formatters
|
|
|
|
%%%===================================================================
|
|
|
|
-spec format_module_error(atom(), start | reload, non_neg_integer(), opts(),
|
|
|
|
error | exit | throw, any(),
|
2023-06-06 12:34:05 +02:00
|
|
|
[tuple()]) -> iolist().
|
2019-06-14 11:33:26 +02:00
|
|
|
format_module_error(Module, Fun, Arity, Opts, Class, Reason, St) ->
|
|
|
|
case {Class, Reason} of
|
|
|
|
{error, {bad_return, Module, {error, _} = Err}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("Failed to ~ts module ~ts: ~ts",
|
2019-06-14 11:33:26 +02:00
|
|
|
[Fun, Module, misc:format_val(Err)]);
|
|
|
|
{error, {bad_return, Module, Ret}} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("Module ~ts returned unexpected value from ~ts/~B:~n"
|
2019-06-14 11:33:26 +02:00
|
|
|
"** Error: ~p~n"
|
|
|
|
"** Hint: this is either not an ejabberd module "
|
2019-07-07 21:12:14 +02:00
|
|
|
"or it implements ejabberd API incorrectly",
|
2019-06-14 11:33:26 +02:00
|
|
|
[Module, Fun, Arity, Ret]);
|
|
|
|
_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
io_lib:format("Internal error of module ~ts has "
|
|
|
|
"occurred during ~ts:~n"
|
2019-06-14 11:33:26 +02:00
|
|
|
"** Options: ~p~n"
|
2019-09-23 14:17:20 +02:00
|
|
|
"** ~ts",
|
2019-07-07 21:12:14 +02:00
|
|
|
[Module, Fun, Opts,
|
|
|
|
misc:format_exception(2, Class, Reason, St)])
|
2019-06-14 11:33:26 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Validation
|
|
|
|
%%%===================================================================
|
|
|
|
-spec validator(binary()) -> econf:validator().
|
|
|
|
validator(Host) ->
|
|
|
|
econf:options(
|
|
|
|
#{modules =>
|
|
|
|
econf:and_then(
|
|
|
|
econf:map(
|
|
|
|
econf:beam([{start, 2}, {stop, 1},
|
|
|
|
{mod_options, 1}, {depends, 2}]),
|
|
|
|
econf:options(
|
|
|
|
#{db_type => econf:atom(),
|
|
|
|
ram_db_type => econf:atom(),
|
|
|
|
'_' => econf:any()})),
|
|
|
|
fun(L) ->
|
|
|
|
Validators = maps:from_list(
|
|
|
|
lists:map(
|
|
|
|
fun({Mod, Opts}) ->
|
|
|
|
{Mod, validator(Host, Mod, Opts)}
|
|
|
|
end, L)),
|
|
|
|
Validator = econf:options(Validators, [unique]),
|
|
|
|
Validator(L)
|
|
|
|
end)}).
|
|
|
|
|
|
|
|
-spec validator(binary(), module(), [{atom(), term()}]) -> econf:validator().
|
|
|
|
validator(Host, Module, Opts) ->
|
|
|
|
{Required, {DefaultOpts1, Validators}} =
|
|
|
|
lists:mapfoldl(
|
|
|
|
fun({M, DefOpts}, {DAcc, VAcc}) ->
|
|
|
|
lists:mapfoldl(
|
|
|
|
fun({Opt, Def}, {DAcc1, VAcc1}) ->
|
|
|
|
{[], {DAcc1#{Opt => Def},
|
|
|
|
VAcc1#{Opt => get_opt_type(Module, M, Opt)}}};
|
|
|
|
(Opt, {DAcc1, VAcc1}) ->
|
|
|
|
{[Opt], {DAcc1,
|
|
|
|
VAcc1#{Opt => get_opt_type(Module, M, Opt)}}}
|
|
|
|
end, {DAcc, VAcc}, DefOpts)
|
|
|
|
end, {#{}, #{}}, get_defaults(Host, Module, Opts)),
|
|
|
|
econf:and_then(
|
|
|
|
econf:options(
|
|
|
|
Validators,
|
|
|
|
[{required, lists:usort(lists:flatten(Required))},
|
|
|
|
{return, map}, unique]),
|
|
|
|
fun(Opts1) ->
|
|
|
|
maps:merge(DefaultOpts1, Opts1)
|
|
|
|
end).
|
|
|
|
|
|
|
|
-spec validate(binary(), [{module(), opts()}]) ->
|
|
|
|
{ok, [{module(), opts(), integer()}]} |
|
|
|
|
econf:error_return().
|
|
|
|
validate(Host, ModOpts) ->
|
|
|
|
case econf:validate(validator(Host), [{modules, ModOpts}]) of
|
|
|
|
{ok, [{modules, ModOpts1}]} ->
|
|
|
|
try sort_modules(Host, ModOpts1)
|
|
|
|
catch throw:{?MODULE, Reason} ->
|
|
|
|
{error, Reason, [modules]}
|
|
|
|
end;
|
|
|
|
{error, _, _} = Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec get_defaults(binary(), module(), [{atom(), term()}]) ->
|
|
|
|
[{module(), [{atom(), term()} | atom()]}].
|
|
|
|
get_defaults(Host, Module, Opts) ->
|
|
|
|
DefaultOpts = Module:mod_options(Host),
|
|
|
|
[{Module, DefaultOpts}|
|
|
|
|
lists:filtermap(
|
|
|
|
fun({Opt, T1}) when Opt == db_type; Opt == ram_db_type ->
|
|
|
|
T2 = proplists:get_value(Opt, Opts, T1),
|
2019-07-31 17:55:43 +02:00
|
|
|
DBMod = list_to_atom(atom_to_list(Module) ++ "_" ++ atom_to_list(T2)),
|
2019-06-14 11:33:26 +02:00
|
|
|
case code:ensure_loaded(DBMod) of
|
|
|
|
{module, _} ->
|
|
|
|
case erlang:function_exported(DBMod, mod_options, 1) of
|
|
|
|
true ->
|
|
|
|
{true, {DBMod, DBMod:mod_options(Host)}};
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
false
|
|
|
|
end, DefaultOpts)].
|
|
|
|
|
|
|
|
-spec get_opt_type(module(), module(), atom()) -> econf:validator().
|
|
|
|
get_opt_type(Mod, SubMod, Opt) ->
|
|
|
|
try SubMod:mod_opt_type(Opt)
|
|
|
|
catch _:_ -> Mod:mod_opt_type(Opt)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec sort_modules(binary(), [{module(), opts()}]) -> {ok, [{module(), opts(), integer()}]}.
|
|
|
|
sort_modules(Host, ModOpts) ->
|
|
|
|
G = digraph:new([acyclic]),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Mod, Opts}) ->
|
|
|
|
digraph:add_vertex(G, Mod, Opts),
|
|
|
|
Deps = Mod:depends(Host, Opts),
|
|
|
|
lists:foreach(
|
|
|
|
fun({DepMod, Type}) ->
|
|
|
|
case lists:keyfind(DepMod, 1, ModOpts) of
|
|
|
|
false when Type == hard ->
|
|
|
|
throw({?MODULE, {missing_module_dep, Mod, DepMod}});
|
|
|
|
false when Type == soft ->
|
|
|
|
warn_soft_dep_fail(DepMod, Mod);
|
|
|
|
{DepMod, DepOpts} ->
|
|
|
|
digraph:add_vertex(G, DepMod, DepOpts),
|
|
|
|
case digraph:add_edge(G, DepMod, Mod) of
|
|
|
|
{error, {bad_edge, Path}} ->
|
|
|
|
warn_cyclic_dep(Path);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end, Deps)
|
|
|
|
end, ModOpts),
|
|
|
|
{Result, _} = lists:mapfoldl(
|
|
|
|
fun(V, Order) ->
|
|
|
|
{M, O} = digraph:vertex(G, V),
|
|
|
|
{{M, O, Order}, Order+1}
|
|
|
|
end, 1, digraph_utils:topsort(G)),
|
|
|
|
digraph:delete(G),
|
|
|
|
{ok, Result}.
|
|
|
|
|
|
|
|
-spec warn_soft_dep_fail(module(), module()) -> ok.
|
|
|
|
warn_soft_dep_fail(DepMod, Mod) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Module ~ts is recommended for module "
|
|
|
|
"~ts but is not found in the config",
|
2019-06-14 11:33:26 +02:00
|
|
|
[DepMod, Mod]).
|
|
|
|
|
|
|
|
-spec warn_cyclic_dep([module()]) -> ok.
|
|
|
|
warn_cyclic_dep(Path) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Cyclic dependency detected between modules ~ts. "
|
2019-06-14 11:33:26 +02:00
|
|
|
"This is either a bug, or the modules are not "
|
|
|
|
"supposed to work together in this configuration. "
|
|
|
|
"The modules will still be loaded though",
|
2019-09-20 11:36:31 +02:00
|
|
|
[misc:format_cycle(Path)]).
|