From: Badlop Date: Thu, 7 Jul 2011 21:03:01 +0000 (+0200) Subject: Improve static_modules support in hooks (EJAB-1478) X-Git-Tag: v3.0.0-alpha-4~64 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=466481d126a5a8e11515eacfd7359ef400859da9;p=ejabberd Improve static_modules support in hooks (EJAB-1478) Also removed code that calls a hook for both Host and for global. --- diff --git a/src/ejabberd_hooks.erl b/src/ejabberd_hooks.erl index bd5af9a00..0c1bd4af4 100644 --- a/src/ejabberd_hooks.erl +++ b/src/ejabberd_hooks.erl @@ -119,27 +119,14 @@ delete_dist(Hook, Host, Node, Module, Function, Seq) -> %% @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. run(Hook, Args) -> - runx(Hook, global, Args). - -run(Hook, Host, Args) when is_binary(Host) -> - case runx(Hook, Host, Args) of - stop -> stop; - _ -> runx(Hook, global, Args) - end; -run(Hook, Host, Args) when Host == global -> - runx(Hook, Host, Args). - -runx(Hook, Host, Args) when is_binary(Host) orelse is_atom(Host) -> - case ets:lookup(hooks, {Hook, ejabberd:normalize_host(Host)}) of - [{_, Ls}] -> - run1(Ls, Hook, Args); - [] -> - case ets:lookup(hooks, {Hook, global}) of - [{_, Ls}] -> - run1(Ls, Hook, Args); - [] -> - ok - end + run(Hook, global, Args). + +run(Hook, Host, Args) -> + case get_registered_hooks_for(Hook, Host) of + [] -> + ok; + Ls -> + run1(Ls, Hook, Args) end. %% @spec (Hook::atom(), Val, Args) -> Val | stopped | NewVal @@ -149,29 +136,16 @@ runx(Hook, Host, Args) when is_binary(Host) orelse is_atom(Host) -> %% If a call returns 'stop', no more calls are performed and 'stopped' is returned. %% If a call returns {stopped, NewVal}, no more calls are performed and NewVal is returned. run_fold(Hook, Val, Args) -> - run_foldx(Hook, global, Val, Args). + run_fold(Hook, global, Val, Args). %% @spec (Hook::atom(), Host, Val, Args) -> Val | stopped | NewVal %% Host = global | binary() -run_fold(Hook, Host, Val, Args) when is_binary(Host) -> - case run_foldx(Hook, Host, Val, Args) of - stopped -> stopped; - Val2 -> run_foldx(Hook, global, Val2, Args) - end; -run_fold(Hook, Host, Val, Args) when Host == global -> - run_foldx(Hook, Host, Val, Args). - -run_foldx(Hook, Host, Val, Args) -> - case ets:lookup(hooks, {Hook, ejabberd:normalize_host(Host)}) of - [{_, Ls}] -> - run_fold1(Ls, Hook, Val, Args); - [] -> - case ets:lookup(hooks, {Hook, global}) of - [{_, Ls}] -> - run_fold1(Ls, Hook, Val, Args); - [] -> - Val - end +run_fold(Hook, Host, Val, Args) -> + case get_registered_hooks_for(Hook, Host) of + [] -> + Val; + Ls -> + run_fold1(Ls, Hook, Val, Args) end. %%%---------------------------------------------------------------------- @@ -240,7 +214,10 @@ handle_call({delete, Hook, Host, Module, Function, Seq}, _From, State) -> Reply = case ets:lookup(hooks, {Hook, NHost}) of [{_, Ls}] -> NewLs = lists:delete({Seq, Module, Function}, Ls), - ets:insert(hooks, {{Hook, NHost}, NewLs}), + case NewLs of + [] -> ets:delete(hooks, {Hook, NHost}); + _ -> ets:insert(hooks, {{Hook, NHost}, NewLs}) + end, ok; [] -> ok @@ -250,7 +227,10 @@ handle_call({delete, Hook, Host, Node, Module, Function, Seq}, _From, State) -> Reply = case ets:lookup(hooks, {Hook, Host}) of [{_, Ls}] -> NewLs = lists:delete({Seq, Node, Module, Function}, Ls), - ets:insert(hooks, {{Hook, Host}, NewLs}), + case NewLs of + [] -> ets:delete(hooks, {Hook, Host}); + _ -> ets:insert(hooks, {{Hook, Host}, NewLs}) + end, ok; [] -> ok @@ -374,3 +354,21 @@ run_fold1([{_Seq, Module, Function} | Ls], Hook, Val, Args) -> NewVal -> run_fold1(Ls, Hook, NewVal, Args) end. + +get_registered_hooks_for(Hook, global) -> + case ets:lookup(hooks, {Hook, global}) of + [{_, Ls}] -> + Ls; + [] -> + [] + end; + +get_registered_hooks_for(Hook, Host) -> + GlobalHooks = get_registered_hooks_for(Hook, global), + HostHooks = case ets:lookup(hooks, {Hook, ejabberd:normalize_host(Host)}) of + [{_, Ls}] -> + Ls; + [] -> + [] + end, + lists:umerge(GlobalHooks, HostHooks).