%% @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
%% 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.
%%%----------------------------------------------------------------------
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
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
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).