From 4bd77797fc562221c081f84aacc60b0d01a20d7c Mon Sep 17 00:00:00 2001 From: Alexey Shchepin Date: Tue, 1 Aug 2023 18:14:08 +0300 Subject: [PATCH] Add shorter forms for gen_mod hook/iq_handler API --- src/gen_mod.erl | 31 +++++++++++++++++++++---------- src/mod_disco.erl | 26 +++++++++++--------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/gen_mod.erl b/src/gen_mod.erl index 8abf9e771..4b9f1fc4b 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -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). diff --git a/src/mod_disco.erl b/src/mod_disco.erl index 8f8093793..ed4a37caa 100644 --- a/src/mod_disco.erl +++ b/src/mod_disco.erl @@ -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,