24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-18 22:15:20 +02:00

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

View File

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