Add shorter forms for gen_mod hook/iq_handler API

This commit is contained in:
Alexey Shchepin 2023-08-01 18:14:08 +03:00
parent 03ffbe00c1
commit 4bd77797fc
2 changed files with 32 additions and 25 deletions

View File

@ -60,7 +60,9 @@
-type component() :: ejabberd_sm | ejabberd_local.
-type registration() ::
{hook, atom(), atom(), integer()} |
{hook, atom(), module(), atom(), integer()} |
{iq_handler, component(), binary(), atom()} |
{iq_handler, component(), binary(), module(), atom()}.
-callback start(binary(), opts()) ->
@ -165,7 +167,7 @@ start_module(Host, Module, Opts, Order) ->
{ok, Pid} when is_pid(Pid) -> {ok, Pid};
{ok, Registrations} when is_list(Registrations) ->
store_options(Host, Module, Opts, Registrations, Order),
add_registrations(Host, Registrations),
add_registrations(Host, Module, Registrations),
ok;
Err ->
ets:delete(ejabberd_modules, {Module, Host}),
@ -319,7 +321,7 @@ stop_module_keep_config(Host, Module) ->
[] ->
[]
end,
del_registrations(Host, Registrations),
del_registrations(Host, Module, Registrations),
try Module:stop(Host) of
_ ->
ets:delete(ejabberd_modules, {Module, Host}),
@ -332,21 +334,30 @@ stop_module_keep_config(Host, Module) ->
error
end.
-spec add_registrations(binary(), [registration()]) -> ok.
add_registrations(Host, Registrations) ->
-spec add_registrations(binary(), module(), [registration()]) -> ok.
add_registrations(Host, Module, Registrations) ->
lists:foreach(
fun({hook, Hook, Module, Function, Seq}) ->
fun({hook, Hook, Function, Seq}) ->
ejabberd_hooks:add(Hook, Host, Module, Function, Seq);
({iq_handler, Component, NS, Module, Function}) ->
({hook, Hook, Module1, Function, Seq}) ->
ejabberd_hooks:add(Hook, Host, Module1, Function, Seq);
({iq_handler, Component, NS, Function}) ->
gen_iq_handler:add_iq_handler(
Component, Host, NS, Module, Function)
Component, Host, NS, Module, Function);
({iq_handler, Component, NS, Module1, Function}) ->
gen_iq_handler:add_iq_handler(
Component, Host, NS, Module1, Function)
end, Registrations).
-spec del_registrations(binary(), [registration()]) -> ok.
del_registrations(Host, Registrations) ->
-spec del_registrations(binary(), module(), [registration()]) -> ok.
del_registrations(Host, Module, Registrations) ->
lists:foreach(
fun({hook, Hook, Module, Function, Seq}) ->
fun({hook, Hook, Function, Seq}) ->
ejabberd_hooks:delete(Hook, Host, Module, Function, Seq);
({hook, Hook, Module1, Function, Seq}) ->
ejabberd_hooks:delete(Hook, Host, Module1, Function, Seq);
({iq_handler, Component, NS, _Function}) ->
gen_iq_handler:remove_iq_handler(Component, Host, NS);
({iq_handler, Component, NS, _Module, _Function}) ->
gen_iq_handler:remove_iq_handler(Component, Host, NS)
end, Registrations).

View File

@ -59,21 +59,17 @@ start(Host, Opts) ->
register_extra_domain(Host, Domain)
end,
ExtraDomains),
{ok, [{iq_handler, ejabberd_local, ?NS_DISCO_ITEMS,
?MODULE, process_local_iq_items},
{iq_handler, ejabberd_local, ?NS_DISCO_INFO,
?MODULE, process_local_iq_info},
{iq_handler, ejabberd_sm, ?NS_DISCO_ITEMS,
?MODULE, process_sm_iq_items},
{iq_handler, ejabberd_sm, ?NS_DISCO_INFO,
?MODULE, process_sm_iq_info},
{hook, disco_local_items, ?MODULE, get_local_services, 100},
{hook, disco_local_features, ?MODULE, get_local_features, 100},
{hook, disco_local_identity, ?MODULE, get_local_identity, 100},
{hook, disco_sm_items, ?MODULE, get_sm_items, 100},
{hook, disco_sm_features, ?MODULE, get_sm_features, 100},
{hook, disco_sm_identity, ?MODULE, get_sm_identity, 100},
{hook, disco_info, ?MODULE, get_info, 100}]}.
{ok, [{iq_handler, ejabberd_local, ?NS_DISCO_ITEMS, process_local_iq_items},
{iq_handler, ejabberd_local, ?NS_DISCO_INFO, process_local_iq_info},
{iq_handler, ejabberd_sm, ?NS_DISCO_ITEMS, process_sm_iq_items},
{iq_handler, ejabberd_sm, ?NS_DISCO_INFO, process_sm_iq_info},
{hook, disco_local_items, get_local_services, 100},
{hook, disco_local_features, get_local_features, 100},
{hook, disco_local_identity, get_local_identity, 100},
{hook, disco_sm_items, get_sm_items, 100},
{hook, disco_sm_features, get_sm_features, 100},
{hook, disco_sm_identity, get_sm_identity, 100},
{hook, disco_info, get_info, 100}]}.
stop(Host) ->
catch ets:match_delete(disco_extra_domains,