26
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-28 17:38:54 +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()} |
{iq_handler, component(), binary(), atom()} |
{iq_handler, component(), binary(), module(), atom()}.
-export_type([registration/0]).
-callback start(binary(), opts()) ->
ok | {ok, pid()} |

View File

@ -46,15 +46,14 @@
%%--------------------------------------------------------------------
%% gen_mod callbacks.
%%--------------------------------------------------------------------
-spec start(binary(), gen_mod:opts()) -> ok.
start(Host, _Opts) ->
register_iq_handlers(Host),
register_hooks(Host).
-spec start(binary(), gen_mod:opts()) -> {ok, [gen_mod:registration()]}.
start(_Host, _Opts) ->
{ok, [{iq_handler, ejabberd_local, ?NS_JIDPREP_0, process_iq},
{hook, disco_local_features, disco_local_features, 50}]}.
-spec stop(binary()) -> ok.
stop(Host) ->
unregister_hooks(Host),
unregister_iq_handlers(Host).
stop(_Host) ->
ok.
-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok.
reload(_Host, _NewOpts, _OldOpts) ->
@ -88,19 +87,6 @@ mod_doc() ->
"be used to control who is allowed to use this "
"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.
%%--------------------------------------------------------------------
@ -123,15 +109,6 @@ disco_local_features(Acc, _From, _To, _Node, _Lang) ->
%%--------------------------------------------------------------------
%% 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().
process_iq(#iq{type = set, lang = Lang} = IQ) ->
Txt = ?T("Value 'set' of 'type' attribute is not allowed"),

View File

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

View File

@ -91,19 +91,26 @@
%%--------------------------------------------------------------------
%% gen_mod callbacks.
%%--------------------------------------------------------------------
-spec start(binary(), gen_mod:opts()) -> ok.
-spec start(binary(), gen_mod:opts()) -> {ok, [gen_mod:registration()]}.
start(Host, Opts) ->
Mod = gen_mod:db_mod(Opts, ?MODULE),
Mod:init(Host, Opts),
init_cache(Mod, Host, Opts),
register_iq_handlers(Host),
register_hooks(Host),
ejabberd_commands:register_commands(?MODULE, get_commands_spec()).
ejabberd_commands:register_commands(?MODULE, get_commands_spec()),
{ok, [{iq_handler, ejabberd_sm, ?NS_PUSH_0, process_iq},
{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.
stop(Host) ->
unregister_hooks(Host),
unregister_iq_handlers(Host),
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
false ->
ejabberd_commands:unregister_commands(get_commands_spec());
@ -248,51 +255,6 @@ delete_old_sessions(Days) ->
Reason
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.
%%--------------------------------------------------------------------
@ -311,15 +273,6 @@ disco_sm_features(Acc, _From, _To, _Node, _Lang) ->
%%--------------------------------------------------------------------
%% 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().
process_iq(#iq{type = get, lang = Lang} = IQ) ->
Txt = ?T("Value 'get' of 'type' attribute is not allowed"),