25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-30 16:36:29 +01:00

Update some modules to the new gen_mod API

This commit is contained in:
Alexey Shchepin 2023-08-09 17:08:45 +03:00
parent caf3807bcc
commit eeacace02a
4 changed files with 24 additions and 100 deletions

View File

@ -64,6 +64,7 @@
{hook, atom(), module(), atom(), integer()} | {hook, atom(), module(), atom(), integer()} |
{iq_handler, component(), binary(), atom()} | {iq_handler, component(), binary(), atom()} |
{iq_handler, component(), binary(), module(), atom()}. {iq_handler, component(), binary(), module(), atom()}.
-export_type([registration/0]).
-callback start(binary(), opts()) -> -callback start(binary(), opts()) ->
ok | {ok, pid()} | ok | {ok, pid()} |

View File

@ -46,15 +46,14 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_mod callbacks. %% gen_mod callbacks.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec start(binary(), gen_mod:opts()) -> ok. -spec start(binary(), gen_mod:opts()) -> {ok, [gen_mod:registration()]}.
start(Host, _Opts) -> start(_Host, _Opts) ->
register_iq_handlers(Host), {ok, [{iq_handler, ejabberd_local, ?NS_JIDPREP_0, process_iq},
register_hooks(Host). {hook, disco_local_features, disco_local_features, 50}]}.
-spec stop(binary()) -> ok. -spec stop(binary()) -> ok.
stop(Host) -> stop(_Host) ->
unregister_hooks(Host), ok.
unregister_iq_handlers(Host).
-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok. -spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok.
reload(_Host, _NewOpts, _OldOpts) -> reload(_Host, _NewOpts, _OldOpts) ->
@ -88,19 +87,6 @@ mod_doc() ->
"be used to control who is allowed to use this " "be used to control who is allowed to use this "
"service. The default value is 'local'.")}}]}. "service. The default value is 'local'.")}}]}.
%%--------------------------------------------------------------------
%% Register/unregister hooks.
%%--------------------------------------------------------------------
-spec register_hooks(binary()) -> ok.
register_hooks(Host) ->
ejabberd_hooks:add(disco_local_features, Host, ?MODULE,
disco_local_features, 50).
-spec unregister_hooks(binary()) -> ok.
unregister_hooks(Host) ->
ejabberd_hooks:delete(disco_local_features, Host, ?MODULE,
disco_local_features, 50).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Service discovery. %% Service discovery.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -123,15 +109,6 @@ disco_local_features(Acc, _From, _To, _Node, _Lang) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% IQ handlers. %% IQ handlers.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec register_iq_handlers(binary()) -> ok.
register_iq_handlers(Host) ->
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
?NS_JIDPREP_0, ?MODULE, process_iq).
-spec unregister_iq_handlers(binary()) -> ok.
unregister_iq_handlers(Host) ->
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_JIDPREP_0).
-spec process_iq(iq()) -> iq(). -spec process_iq(iq()) -> iq().
process_iq(#iq{type = set, lang = Lang} = IQ) -> process_iq(#iq{type = set, lang = Lang} = IQ) ->
Txt = ?T("Value 'set' of 'type' attribute is not allowed"), Txt = ?T("Value 'set' of 'type' attribute is not allowed"),

View File

@ -44,18 +44,11 @@
%%% gen_mod %%% gen_mod
%%% %%%
start(Host, _Opts) -> start(_Host, _Opts) ->
ejabberd_hooks:add(muc_filter_presence, Host, {ok, [{hook, muc_filter_presence, filter_packet, 10},
?MODULE, filter_packet, 10), {hook, muc_filter_message, filter_packet, 10}]}.
ejabberd_hooks:add(muc_filter_message, Host,
?MODULE, filter_packet, 10),
ok.
stop(Host) -> stop(_Host) ->
ejabberd_hooks:delete(muc_filter_presence, Host,
?MODULE, filter_packet, 10),
ejabberd_hooks:delete(muc_filter_message, Host,
?MODULE, filter_packet, 10),
ok. ok.
%%% %%%

View File

@ -91,19 +91,26 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% gen_mod callbacks. %% gen_mod callbacks.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec start(binary(), gen_mod:opts()) -> ok. -spec start(binary(), gen_mod:opts()) -> {ok, [gen_mod:registration()]}.
start(Host, Opts) -> start(Host, Opts) ->
Mod = gen_mod:db_mod(Opts, ?MODULE), Mod = gen_mod:db_mod(Opts, ?MODULE),
Mod:init(Host, Opts), Mod:init(Host, Opts),
init_cache(Mod, Host, Opts), init_cache(Mod, Host, Opts),
register_iq_handlers(Host), ejabberd_commands:register_commands(?MODULE, get_commands_spec()),
register_hooks(Host), {ok, [{iq_handler, ejabberd_sm, ?NS_PUSH_0, process_iq},
ejabberd_commands:register_commands(?MODULE, get_commands_spec()). {hook, disco_sm_features, disco_sm_features, 50},
{hook, c2s_session_pending, c2s_session_pending, 50},
{hook, c2s_copy_session, c2s_copy_session, 50},
{hook, c2s_session_resumed, c2s_session_resumed, 50},
{hook, c2s_handle_cast, c2s_handle_cast, 50},
{hook, c2s_handle_send, c2s_stanza, 50},
{hook, store_mam_message, mam_message, 50},
{hook, offline_message_hook, offline_message, 55},
{hook, remove_user, remove_user, 50}]}.
-spec stop(binary()) -> ok. -spec stop(binary()) -> ok.
stop(Host) -> stop(Host) ->
unregister_hooks(Host),
unregister_iq_handlers(Host),
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
false -> false ->
ejabberd_commands:unregister_commands(get_commands_spec()); ejabberd_commands:unregister_commands(get_commands_spec());
@ -248,51 +255,6 @@ delete_old_sessions(Days) ->
Reason Reason
end. end.
%%--------------------------------------------------------------------
%% Register/unregister hooks.
%%--------------------------------------------------------------------
-spec register_hooks(binary()) -> ok.
register_hooks(Host) ->
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE,
disco_sm_features, 50),
ejabberd_hooks:add(c2s_session_pending, Host, ?MODULE,
c2s_session_pending, 50),
ejabberd_hooks:add(c2s_copy_session, Host, ?MODULE,
c2s_copy_session, 50),
ejabberd_hooks:add(c2s_session_resumed, Host, ?MODULE,
c2s_session_resumed, 50),
ejabberd_hooks:add(c2s_handle_cast, Host, ?MODULE,
c2s_handle_cast, 50),
ejabberd_hooks:add(c2s_handle_send, Host, ?MODULE,
c2s_stanza, 50),
ejabberd_hooks:add(store_mam_message, Host, ?MODULE,
mam_message, 50),
ejabberd_hooks:add(offline_message_hook, Host, ?MODULE,
offline_message, 55),
ejabberd_hooks:add(remove_user, Host, ?MODULE,
remove_user, 50).
-spec unregister_hooks(binary()) -> ok.
unregister_hooks(Host) ->
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE,
disco_sm_features, 50),
ejabberd_hooks:delete(c2s_session_pending, Host, ?MODULE,
c2s_session_pending, 50),
ejabberd_hooks:delete(c2s_copy_session, Host, ?MODULE,
c2s_copy_session, 50),
ejabberd_hooks:delete(c2s_session_resumed, Host, ?MODULE,
c2s_session_resumed, 50),
ejabberd_hooks:delete(c2s_handle_cast, Host, ?MODULE,
c2s_handle_cast, 50),
ejabberd_hooks:delete(c2s_handle_send, Host, ?MODULE,
c2s_stanza, 50),
ejabberd_hooks:delete(store_mam_message, Host, ?MODULE,
mam_message, 50),
ejabberd_hooks:delete(offline_message_hook, Host, ?MODULE,
offline_message, 55),
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
remove_user, 50).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Service discovery. %% Service discovery.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -311,15 +273,6 @@ disco_sm_features(Acc, _From, _To, _Node, _Lang) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% IQ handlers. %% IQ handlers.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec register_iq_handlers(binary()) -> ok.
register_iq_handlers(Host) ->
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_PUSH_0,
?MODULE, process_iq).
-spec unregister_iq_handlers(binary()) -> ok.
unregister_iq_handlers(Host) ->
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PUSH_0).
-spec process_iq(iq()) -> iq(). -spec process_iq(iq()) -> iq().
process_iq(#iq{type = get, lang = Lang} = IQ) -> process_iq(#iq{type = get, lang = Lang} = IQ) ->
Txt = ?T("Value 'get' of 'type' attribute is not allowed"), Txt = ?T("Value 'get' of 'type' attribute is not allowed"),