]> granicus.if.org Git - ejabberd/commitdiff
Improve static_modules support in hooks (EJAB-1478)
authorBadlop <badlop@process-one.net>
Thu, 7 Jul 2011 21:03:01 +0000 (23:03 +0200)
committerBadlop <badlop@process-one.net>
Mon, 11 Jul 2011 17:47:03 +0000 (19:47 +0200)
Also removed code that calls a hook for both Host and for global.

src/ejabberd_hooks.erl

index bd5af9a008bc02857a9dddbb8f2b5ac64e1c3875..0c1bd4af4cdf2cfc0c72c19698e64c32b9da954c 100644 (file)
@@ -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).