diff --git a/src/c2s/mongoose_c2s.erl b/src/c2s/mongoose_c2s.erl index 992f748798..68ff758647 100644 --- a/src/c2s/mongoose_c2s.erl +++ b/src/c2s/mongoose_c2s.erl @@ -587,10 +587,11 @@ handle_foreign_packet(StateData = #c2s_data{host_type = HostType, lserver = LSer -spec handle_c2s_packet(c2s_data(), c2s_state(), exml:element()) -> fsm_res(). handle_c2s_packet(StateData = #c2s_data{host_type = HostType}, C2SState, El) -> Acc0 = element_to_origin_accum(StateData, El), - Acc1 = mongoose_hooks:c2s_preprocessing_hook(HostType, Acc0, StateData), - case mongoose_acc:get(hook, result, undefined, Acc1) of - drop -> {next_state, session_established, StateData}; - _ -> do_handle_c2s_packet(StateData, C2SState, Acc1) + case mongoose_c2s_hooks:c2s_preprocessing_hook(HostType, Acc0, hook_arg(StateData)) of + {ok, Acc1} -> + do_handle_c2s_packet(StateData, C2SState, Acc1); + {stop, _Acc1} -> + {next_state, session_established, StateData} end. -spec do_handle_c2s_packet(c2s_data(), c2s_state(), mongoose_acc:t()) -> fsm_res(). diff --git a/src/c2s/mongoose_c2s_hooks.erl b/src/c2s/mongoose_c2s_hooks.erl index 0c1f7c504a..3aad17064c 100644 --- a/src/c2s/mongoose_c2s_hooks.erl +++ b/src/c2s/mongoose_c2s_hooks.erl @@ -10,6 +10,7 @@ -export_type([hook_fn/0, hook_params/0, hook_result/0]). %% XML handlers +-export([c2s_preprocessing_hook/3]). -export([user_send_packet/3, user_receive_packet/3, user_send_message/3, @@ -30,6 +31,22 @@ user_socket_closed/3, user_socket_error/3]). +%%% @doc Event triggered after a user sends _any_ packet to the server. +%%% The purpose is to modify, or reject, packets, before they are further processed. +%%% TODO: this can really be merged into `user_send_packet' with early priority. +-spec c2s_preprocessing_hook(HostType, Acc, Params) -> Result when + HostType :: mongooseim:host_type(), + Acc :: mongoose_acc:t(), + Params :: hook_params(), + Result :: hook_result(). +c2s_preprocessing_hook(HostType, Acc, #{c2s_data := StateData} = Params) -> + ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, [StateData]), + {Tag, Acc1} = gen_hook:run_fold(c2s_preprocessing_hook, HostType, Acc, ParamsWithLegacyArgs), + case mongoose_acc:get(hook, result, undefined, Acc1) of + drop -> {stop, Acc1}; + _ -> {Tag, Acc1} + end. + %%% @doc Event triggered after a user sends _any_ packet to the server. %%% Examples of handlers can be metrics, archives, and any other subsystem %%% that wants to see all stanzas the user delivers.