Skip to content

Commit

Permalink
Move c2s_preprocessing_hook into mongoose_c2s_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Sep 19, 2022
1 parent 415fb86 commit 62a1dbd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
17 changes: 17 additions & 0 deletions src/c2s/mongoose_c2s_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit 62a1dbd

Please sign in to comment.