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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 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.
|
|
|
|
%%%
|
|
|
|
%%% 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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
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,
|
|
|
|
add/4,
|
|
|
|
delete/4,
|
|
|
|
run/2,
|
2005-06-20 05:18:13 +02:00
|
|
|
run_fold/3,
|
|
|
|
add/5,
|
|
|
|
delete/5,
|
|
|
|
run/3,
|
|
|
|
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]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
|
|
|
-record(state, {}).
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ejabberd_hooks}, ejabberd_hooks, [], []).
|
|
|
|
|
|
|
|
add(Hook, Module, Function, Seq) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
add(Hook, global, Module, Function, Seq).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
add(Hook, Host, Module, Function, Seq)
|
|
|
|
when is_binary(Host) orelse is_atom(Host) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_server:call(ejabberd_hooks, {add, Hook, Host, Module, Function, Seq}).
|
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).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
delete(Hook, Host, Module, Function, Seq)
|
|
|
|
when is_binary(Host) orelse is_atom(Host) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_server:call(ejabberd_hooks, {delete, Hook, Host, Module, Function, Seq}).
|
2004-08-08 21:07:55 +02:00
|
|
|
|
|
|
|
run(Hook, Args) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
run(Hook, global, Args).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
run(Hook, Host, Args) when is_binary(Host) orelse is_atom(Host) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
case ets:lookup(hooks, {Hook, Host}) of
|
2004-08-08 21:07:55 +02:00
|
|
|
[{_, Ls}] ->
|
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
run_fold(Hook, Val, Args) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
run_fold(Hook, global, Val, Args).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
run_fold(Hook, Host, Val, Args) when is_binary(Host) orelse is_atom(Host) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
case 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
|
2004-08-08 21:07:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_server
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
init([]) ->
|
|
|
|
ets:new(hooks, [named_table]),
|
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_call/3
|
|
|
|
%% Returns: {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} | (terminate/2 is called)
|
|
|
|
%% {stop, Reason, State} (terminate/2 is called)
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call({add, Hook, Host, Module, Function, Seq}, _From, State) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Reply = case ets:lookup(hooks, {Hook, Host}) of
|
2004-08-08 21:07:55 +02:00
|
|
|
[{_, Ls}] ->
|
|
|
|
El = {Seq, Module, Function},
|
|
|
|
case lists:member(El, Ls) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
NewLs = lists:merge(Ls, [El]),
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
2004-08-08 21:07:55 +02:00
|
|
|
ok
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
NewLs = [{Seq, Module, Function}],
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
2004-08-08 21:07:55 +02:00
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{reply, Reply, State};
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call({delete, Hook, Host, Module, Function, Seq}, _From, State) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Reply = case ets:lookup(hooks, {Hook, Host}) of
|
2004-08-08 21:07:55 +02:00
|
|
|
[{_, Ls}] ->
|
|
|
|
NewLs = lists:delete({Seq, Module, Function}, Ls),
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:insert(hooks, {{Hook, Host}, NewLs}),
|
2004-08-08 21:07:55 +02:00
|
|
|
ok;
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{reply, Reply, State};
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_call(_Request, _From, State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_cast/2
|
|
|
|
%% Returns: {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State} (terminate/2 is called)
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_cast(_Msg, State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/2
|
|
|
|
%% Returns: {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State} (terminate/2 is called)
|
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_info(_Info, State) ->
|
2004-08-08 21:07:55 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/2
|
|
|
|
%% Purpose: Shutdown the server
|
|
|
|
%% Returns: any (ignored by gen_server)
|
|
|
|
%%----------------------------------------------------------------------
|
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
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
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) ->
|
|
|
|
case catch apply(Module, Function, Args) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nrunning hook: ~p",
|
|
|
|
[Reason, {Hook, Args}]),
|
|
|
|
run1(Ls, Hook, Args);
|
|
|
|
stop ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
run1(Ls, Hook, Args)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
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) ->
|
|
|
|
case catch apply(Module, Function, [Val | Args]) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nrunning hook: ~p",
|
|
|
|
[Reason, {Hook, Args}]),
|
|
|
|
run_fold1(Ls, Hook, Val, Args);
|
|
|
|
stop ->
|
|
|
|
stopped;
|
|
|
|
{stop, NewVal} ->
|
|
|
|
NewVal;
|
|
|
|
NewVal ->
|
|
|
|
run_fold1(Ls, Hook, NewVal, Args)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|