2004-08-08 21:07:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_hooks.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-08-08 21:07:55 +02:00
|
|
|
%%% Purpose : Manage hooks
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 8 Aug 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2021-01-27 16:55:25 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2021 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
|
|
|
%%%
|
2014-02-22 11:27:40 +01: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
|
|
|
%%%
|
2004-08-08 21:07:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
-module(ejabberd_hooks).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-08-08 21:07:55 +02:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% External exports
|
|
|
|
-export([start_link/0,
|
2009-03-06 10:34:13 +01:00
|
|
|
add/3,
|
2004-08-08 21:07:55 +02:00
|
|
|
add/4,
|
2015-03-08 19:03:02 +01:00
|
|
|
add/5,
|
2009-03-06 10:34:13 +01:00
|
|
|
delete/3,
|
2004-08-08 21:07:55 +02:00
|
|
|
delete/4,
|
2005-06-20 05:18:13 +02:00
|
|
|
delete/5,
|
2015-03-08 19:03:02 +01:00
|
|
|
run/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
run/3,
|
2015-03-08 19:03:02 +01:00
|
|
|
run_fold/3,
|
2019-07-09 00:26:48 +02:00
|
|
|
run_fold/4]).
|
2004-08-08 21:07:55 +02:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1,
|
|
|
|
handle_call/3,
|
|
|
|
handle_cast/2,
|
|
|
|
code_change/3,
|
|
|
|
handle_info/2,
|
|
|
|
terminate/2]).
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2004-08-08 21:07:55 +02:00
|
|
|
|
|
|
|
-record(state, {}).
|
2019-07-09 00:26:48 +02:00
|
|
|
-type hook() :: {Seq :: integer(), Module :: atom(), Function :: atom() | fun()}.
|
2004-08-08 21:07:55 +02:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
start_link() ->
|
2019-07-09 00:26:48 +02:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec add(atom(), fun(), integer()) -> ok.
|
2009-03-06 10:34:13 +01:00
|
|
|
%% @doc See add/4.
|
|
|
|
add(Hook, Function, Seq) when is_function(Function) ->
|
|
|
|
add(Hook, global, undefined, Function, Seq).
|
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec add(atom(), HostOrModule :: binary() | atom(), fun() | atom() , integer()) -> ok.
|
2009-03-06 10:34:13 +01:00
|
|
|
add(Hook, Host, Function, Seq) when is_function(Function) ->
|
|
|
|
add(Hook, Host, undefined, Function, Seq);
|
|
|
|
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Add a module and function to this hook.
|
|
|
|
%% The integer sequence is used to sort the calls: low number is called before high number.
|
2004-08-08 21:07:55 +02:00
|
|
|
add(Hook, Module, Function, Seq) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
add(Hook, global, Module, Function, Seq).
|
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec add(atom(), binary() | global, atom(), atom() | fun(), integer()) -> ok.
|
2005-06-20 05:18:13 +02:00
|
|
|
add(Hook, Host, Module, Function, Seq) ->
|
2019-07-09 00:26:48 +02:00
|
|
|
gen_server:call(?MODULE, {add, Hook, Host, Module, Function, Seq}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec delete(atom(), fun(), integer()) -> ok.
|
2009-03-06 10:34:13 +01:00
|
|
|
%% @doc See del/4.
|
|
|
|
delete(Hook, Function, Seq) when is_function(Function) ->
|
|
|
|
delete(Hook, global, undefined, Function, Seq).
|
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec delete(atom(), binary() | atom(), atom() | fun(), integer()) -> ok.
|
2009-03-06 11:03:29 +01:00
|
|
|
delete(Hook, Host, Function, Seq) when is_function(Function) ->
|
2009-03-06 10:34:13 +01:00
|
|
|
delete(Hook, Host, undefined, Function, Seq);
|
|
|
|
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Delete a module and function from this hook.
|
|
|
|
%% It is important to indicate exactly the same information than when the call was added.
|
2004-08-08 21:07:55 +02:00
|
|
|
delete(Hook, Module, Function, Seq) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
delete(Hook, global, Module, Function, Seq).
|
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec delete(atom(), binary() | global, atom(), atom() | fun(), integer()) -> ok.
|
2005-06-20 05:18:13 +02:00
|
|
|
delete(Hook, Host, Module, Function, Seq) ->
|
2019-07-09 00:26:48 +02:00
|
|
|
gen_server:call(?MODULE, {delete, Hook, Host, Module, Function, Seq}).
|
2015-03-08 17:06:43 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec run(atom(), list()) -> ok.
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Run the calls of this hook in order, don't care about function results.
|
|
|
|
%% If a call returns stop, no more calls are performed.
|
2004-08-08 21:07:55 +02:00
|
|
|
run(Hook, Args) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
run(Hook, global, Args).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec run(atom(), binary() | global, list()) -> ok.
|
2005-06-20 05:18:13 +02:00
|
|
|
run(Hook, Host, Args) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
try ets:lookup(hooks, {Hook, Host}) of
|
2004-08-08 21:07:55 +02:00
|
|
|
[{_, Ls}] ->
|
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
[] ->
|
|
|
|
ok
|
2019-06-14 11:33:26 +02:00
|
|
|
catch _:badarg ->
|
|
|
|
ok
|
2004-08-08 21:07:55 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:25 +02:00
|
|
|
-spec run_fold(atom(), T, list()) -> T.
|
2009-01-21 18:36:28 +01:00
|
|
|
%% @doc Run the calls of this hook in order.
|
|
|
|
%% The arguments passed to the function are: [Val | Args].
|
|
|
|
%% The result of a call is used as Val for the next call.
|
2019-07-09 13:30:25 +02:00
|
|
|
%% If a call returns 'stop', no more calls are performed.
|
2014-08-26 01:04:15 +02:00
|
|
|
%% If a call returns {stop, NewVal}, no more calls are performed and NewVal is returned.
|
2004-08-08 21:07:55 +02:00
|
|
|
run_fold(Hook, Val, Args) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
run_fold(Hook, global, Val, Args).
|
|
|
|
|
2019-07-09 13:30:25 +02:00
|
|
|
-spec run_fold(atom(), binary() | global, T, list()) -> T.
|
2005-06-20 05:18:13 +02:00
|
|
|
run_fold(Hook, Host, Val, Args) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
try ets:lookup(hooks, {Hook, Host}) of
|
2004-08-08 21:07:55 +02:00
|
|
|
[{_, Ls}] ->
|
|
|
|
run_fold1(Ls, Hook, Val, Args);
|
|
|
|
[] ->
|
2004-08-13 00:34:19 +02:00
|
|
|
Val
|
2019-06-14 11:33:26 +02:00
|
|
|
catch _:badarg ->
|
|
|
|
Val
|
2004-08-08 21:07:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_server
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
init([]) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
_ = ets:new(hooks, [named_table, {read_concurrency, true}]),
|
2004-08-08 21:07:55 +02:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call({add, Hook, Host, Module, Function, Seq}, _From, State) ->
|
2015-03-08 18:23:22 +01:00
|
|
|
HookFormat = {Seq, Module, Function},
|
|
|
|
Reply = handle_add(Hook, Host, HookFormat),
|
2004-08-08 21:07:55 +02:00
|
|
|
{reply, Reply, State};
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call({delete, Hook, Host, Module, Function, Seq}, _From, State) ->
|
2015-03-08 18:23:22 +01:00
|
|
|
HookFormat = {Seq, Module, Function},
|
|
|
|
Reply = handle_delete(Hook, Host, HookFormat),
|
2004-08-08 21:07:55 +02:00
|
|
|
{reply, Reply, State};
|
2019-07-09 00:26:48 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2004-08-08 21:07:55 +02:00
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec handle_add(atom(), atom(), hook()) -> ok.
|
2015-03-08 18:23:22 +01:00
|
|
|
handle_add(Hook, Host, El) ->
|
|
|
|
case ets:lookup(hooks, {Hook, Host}) of
|
|
|
|
[{_, Ls}] ->
|
|
|
|
case lists:member(El, Ls) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
NewLs = lists:merge(Ls, [El]),
|
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
NewLs = [El],
|
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec handle_delete(atom(), atom(), hook()) -> ok.
|
2015-03-08 18:23:22 +01:00
|
|
|
handle_delete(Hook, Host, El) ->
|
|
|
|
case ets:lookup(hooks, {Hook, Host}) of
|
|
|
|
[{_, Ls}] ->
|
|
|
|
NewLs = lists:delete(El, Ls),
|
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
|
|
|
ok;
|
|
|
|
[] ->
|
|
|
|
ok
|
2018-12-13 11:45:45 +01:00
|
|
|
end.
|
2015-03-08 18:23:22 +01:00
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2004-08-08 21:07:55 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2004-08-08 21:07:55 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
terminate(_Reason, _State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec run1([hook()], atom(), list()) -> ok.
|
2007-12-07 00:15:04 +01:00
|
|
|
run1([], _Hook, _Args) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
ok;
|
|
|
|
run1([{_Seq, Module, Function} | Ls], Hook, Args) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Res = safe_apply(Hook, Module, Function, Args),
|
2009-03-06 10:34:13 +01:00
|
|
|
case Res of
|
2017-01-09 15:02:17 +01:00
|
|
|
'EXIT' ->
|
2004-08-08 21:07:55 +02:00
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
stop ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
run1(Ls, Hook, Args)
|
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:25 +02:00
|
|
|
-spec run_fold1([hook()], atom(), T, list()) -> T.
|
2007-12-07 00:15:04 +01:00
|
|
|
run_fold1([], _Hook, Val, _Args) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
Val;
|
|
|
|
run_fold1([{_Seq, Module, Function} | Ls], Hook, Val, Args) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Res = safe_apply(Hook, Module, Function, [Val | Args]),
|
2009-03-06 10:34:13 +01:00
|
|
|
case Res of
|
2017-01-09 15:02:17 +01:00
|
|
|
'EXIT' ->
|
2004-08-08 21:07:55 +02:00
|
|
|
run_fold1(Ls, Hook, Val, Args);
|
|
|
|
stop ->
|
2019-07-09 13:30:25 +02:00
|
|
|
Val;
|
2004-08-08 21:07:55 +02:00
|
|
|
{stop, NewVal} ->
|
|
|
|
NewVal;
|
|
|
|
NewVal ->
|
|
|
|
run_fold1(Ls, Hook, NewVal, Args)
|
|
|
|
end.
|
2015-03-08 18:44:43 +01:00
|
|
|
|
2019-07-09 00:26:48 +02:00
|
|
|
-spec safe_apply(atom(), atom(), atom() | fun(), list()) -> any().
|
2017-01-09 15:02:17 +01:00
|
|
|
safe_apply(Hook, Module, Function, Args) ->
|
2019-07-08 08:57:33 +02:00
|
|
|
?DEBUG("Running hook ~p: ~p:~p/~B",
|
|
|
|
[Hook, Module, Function, length(Args)]),
|
2016-12-28 07:47:11 +01:00
|
|
|
try if is_function(Function) ->
|
|
|
|
apply(Function, Args);
|
2015-03-08 18:44:43 +01:00
|
|
|
true ->
|
2016-12-28 07:47:11 +01:00
|
|
|
apply(Module, Function, Args)
|
|
|
|
end
|
2018-12-13 11:45:45 +01:00
|
|
|
catch ?EX_RULE(E, R, St) when E /= exit; R /= normal ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Stack = ?EX_STACK(St),
|
2019-03-14 10:28:37 +01:00
|
|
|
?ERROR_MSG("Hook ~p crashed when running ~p:~p/~p:~n" ++
|
|
|
|
string:join(
|
2019-09-23 14:17:20 +02:00
|
|
|
["** ~ts"|
|
2019-03-14 10:28:37 +01:00
|
|
|
["** Arg " ++ integer_to_list(I) ++ " = ~p"
|
|
|
|
|| I <- lists:seq(1, length(Args))]],
|
|
|
|
"~n"),
|
2017-01-09 15:02:17 +01:00
|
|
|
[Hook, Module, Function, length(Args),
|
2019-06-14 11:33:26 +02:00
|
|
|
misc:format_exception(2, E, R, Stack)|Args]),
|
2017-01-09 15:02:17 +01:00
|
|
|
'EXIT'
|
2015-03-08 18:44:43 +01:00
|
|
|
end.
|