Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C2s/refactor user send packet #3857

Merged
merged 5 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/c2s/mongoose_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

%% utils
-export([start_link/2, start/2, stop/2, exit/2, async/3]).
-export([get_host_type/1, get_lserver/1, get_sid/1, get_jid/1,
-export([create_data/1, get_host_type/1, get_lserver/1, get_sid/1, get_jid/1,
get_mod_state/2, merge_mod_state/2, remove_mod_state/2,
get_ip/1, get_socket/1, get_lang/1, get_stream_id/1]).
get_ip/1, get_socket/1, get_lang/1, get_stream_id/1, hook_arg/5]).
-export([filter_mechanism/2, c2s_stream_error/2, maybe_retry_state/1,
reroute/2, merge_states/2]).

Expand All @@ -31,7 +31,7 @@
socket :: undefined | mongoose_c2s_socket:socket(),
parser :: undefined | exml_stream:parser(),
shaper :: undefined | shaper:shaper(),
listener_opts :: listener_opts(),
listener_opts :: undefined | listener_opts(),
auth_module :: undefined | module(),
state_mod = #{} :: #{module() => term()}
}).
Expand Down Expand Up @@ -936,6 +936,10 @@ exit(Pid, Reason) ->
async(Pid, Fun, Args) ->
gen_statem:cast(Pid, {async, Fun, Args}).

-spec create_data(#{host_type := mongooseim:host_type(), jid := jid:jid()}) -> c2s_data().
create_data(#{host_type := HostType, jid := Jid}) ->
#c2s_data{host_type = HostType, jid = Jid}.

-spec get_host_type(c2s_data()) -> mongooseim:host_type().
get_host_type(#c2s_data{host_type = HostType}) ->
HostType.
Expand Down
77 changes: 38 additions & 39 deletions src/c2s/mongoose_c2s_hooks.erl
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
%% @doc This module builds an interface to c2s event handling
-module(mongoose_c2s_hooks).

-type hook_fn() :: fun((mongoose_acc:t(), mongoose_c2s_hooks:hook_params(), gen_hook:hook_extra()) ->
hook_result()).
-type hook_params() :: #{c2s_data := mongoose_c2s:state(),
-type fn() :: fun((mongoose_acc:t(), params(), gen_hook:hook_extra()) -> result()).
-type params() :: #{c2s_data := mongoose_c2s:state(),
c2s_state := mongoose_c2s:c2s_state(),
event_type := undefined | gen_statem:event_type(),
event_content := undefined | term(),
reason := undefined | term()}.
-type hook_result() :: gen_hook:hook_fn_ret(mongoose_acc:t()).
-export_type([hook_fn/0, hook_params/0, hook_result/0]).
-type result() :: gen_hook:hook_fn_ret(mongoose_acc:t()).
-export_type([fn/0, params/0, result/0]).

%% XML handlers
-export([user_send_packet/3,
Expand Down Expand Up @@ -39,8 +38,8 @@
-spec user_send_packet(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_send_packet(HostType, Acc, Params) ->
{From, To, El} = mongoose_acc:packet(Acc),
Args = [From, To, El],
Expand All @@ -52,30 +51,30 @@ user_send_packet(HostType, Acc, Params) ->
-spec user_receive_packet(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
user_receive_packet(HostType, Acc, #{c2s_data := C2SState} = Params) ->
Params :: params(),
Result :: result().
user_receive_packet(HostType, Acc, #{c2s_data := C2SData} = Params) ->
{From, To, El} = mongoose_acc:packet(Acc),
Jid = mongoose_c2s:get_jid(C2SState),
Jid = mongoose_c2s:get_jid(C2SData),
Args = [Jid, From, To, El],
ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params#{jid => Jid}, Args),
ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args),
gen_hook:run_fold(user_receive_packet, HostType, Acc, ParamsWithLegacyArgs).

%% @doc Triggered when the user sends a stanza of type `message'
-spec user_send_message(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_send_message(HostType, Acc, Params) ->
gen_hook:run_fold(user_send_message, HostType, Acc, Params).

%% @doc Triggered when the user sends a stanza of type `iq'
-spec user_send_iq(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_send_iq(HostType, Acc, Params) ->
Acc1 = mongoose_iq:update_acc_info(Acc),
gen_hook:run_fold(user_send_iq, HostType, Acc1, Params).
Expand All @@ -84,8 +83,8 @@ user_send_iq(HostType, Acc, Params) ->
-spec user_send_presence(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_send_presence(HostType, Acc, Params) ->
gen_hook:run_fold(user_send_presence, HostType, Acc, Params).

Expand All @@ -94,8 +93,8 @@ user_send_presence(HostType, Acc, Params) ->
-spec user_send_xmlel(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_send_xmlel(HostType, Acc, Params) ->
gen_hook:run_fold(user_send_xmlel, HostType, Acc, Params).

Expand All @@ -104,17 +103,17 @@ user_send_xmlel(HostType, Acc, Params) ->
-spec user_receive_message(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_receive_message(HostType, Acc, Params) ->
gen_hook:run_fold(user_receive_message, HostType, Acc, Params).

%% @doc Triggered when the user received a stanza of type `iq'
-spec user_receive_iq(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_receive_iq(HostType, Acc, Params) ->
Acc1 = mongoose_iq:update_acc_info(Acc),
gen_hook:run_fold(user_receive_iq, HostType, Acc1, Params).
Expand All @@ -123,8 +122,8 @@ user_receive_iq(HostType, Acc, Params) ->
-spec user_receive_presence(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_receive_presence(HostType, Acc, Params) ->
gen_hook:run_fold(user_receive_presence, HostType, Acc, Params).

Expand All @@ -133,8 +132,8 @@ user_receive_presence(HostType, Acc, Params) ->
-spec user_receive_xmlel(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_receive_xmlel(HostType, Acc, Params) ->
gen_hook:run_fold(user_receive_xmlel, HostType, Acc, Params).

Expand All @@ -146,7 +145,7 @@ user_receive_xmlel(HostType, Acc, Params) ->
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: map(),
Result :: hook_result().
Result :: result().
foreign_event(HostType, Acc, Params) ->
gen_hook:run_fold(foreign_event, HostType, Acc, Params).

Expand All @@ -157,8 +156,8 @@ foreign_event(HostType, Acc, Params) ->
-spec user_open_session(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_open_session(HostType, Acc, Params) ->
gen_hook:run_fold(user_open_session, HostType, Acc, Params).

Expand All @@ -167,7 +166,7 @@ user_open_session(HostType, Acc, Params) ->
-spec user_terminate(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Params :: params(),
Result :: mongoose_acc:t().
user_terminate(HostType, Acc, Params) ->
{_, Res} = gen_hook:run_fold(user_terminate, HostType, Acc, Params),
Expand All @@ -178,7 +177,7 @@ user_terminate(HostType, Acc, Params) ->
-spec reroute_unacked_messages(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Params :: params(),
Result :: mongoose_acc:t().
reroute_unacked_messages(HostType, Acc, Params) ->
{_, Res} = gen_hook:run_fold(reroute_unacked_messages, HostType, Acc, Params),
Expand All @@ -193,25 +192,25 @@ reroute_unacked_messages(HostType, Acc, Params) ->
-spec user_stop_request(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_stop_request(HostType, Acc, Params) ->
gen_hook:run_fold(user_stop_request, HostType, Acc, Params).

%% @doc Triggered when the socket dies.
-spec user_socket_closed(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_socket_closed(HostType, Acc, Params) ->
gen_hook:run_fold(user_socket_closed, HostType, Acc, Params).

%% @doc Triggered when the socket errors out.
-spec user_socket_error(HostType, Acc, Params) -> Result when
HostType :: mongooseim:host_type(),
Acc :: mongoose_acc:t(),
Params :: hook_params(),
Result :: hook_result().
Params :: params(),
Result :: result().
user_socket_error(HostType, Acc, Params) ->
gen_hook:run_fold(user_socket_error, HostType, Acc, Params).
4 changes: 2 additions & 2 deletions src/c2s/mongoose_c2s_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ start_listener(Opts) ->
ok.

%% Hooks and handlers
-spec handle_user_open_session(mongoose_acc:t(), mongoose_c2s_hooks:hook_params(), map()) ->
mongoose_c2s_hooks:hook_result().
-spec handle_user_open_session(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) ->
mongoose_c2s_hooks:result().
handle_user_open_session(Acc, #{c2s_data := StateData}, #{host_type := HostType, access := Access}) ->
Jid = mongoose_c2s:get_jid(StateData),
LServer = mongoose_c2s:get_lserver(StateData),
Expand Down
6 changes: 2 additions & 4 deletions src/event_pusher/mod_event_pusher_hook_translator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ filter_local_packet({From, To, Acc0, Packet}, _, _) ->
end,
{ok, {From, To, Acc, Packet}}.

-spec user_send_packet(Acc, Args, Extra) -> {ok, Acc} when
Acc :: mongoose_acc:t(),
Args :: map(),
Extra :: map().
-spec user_send_packet(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) ->
mongoose_c2s_hooks:result().
user_send_packet(Acc, _, _) ->
Packet = mongoose_acc:packet(Acc),
ChatType = chat_type(Acc),
Expand Down
21 changes: 8 additions & 13 deletions src/inbox/mod_inbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
-export([process_iq/5]).

%% hook handlers
-export([user_send_packet/3,
-export([user_send_message/3,
filter_local_packet/3,
inbox_unread_count/3,
remove_user/3,
Expand Down Expand Up @@ -229,17 +229,12 @@ send_message(Acc, To = #jid{lserver = LServer}, Msg) ->

%%%%%%%%%%%%%%%%%%%
%% Handlers
-spec user_send_packet(Acc, Args, Extra) -> {ok, Acc} when
Acc :: mongoose_acc:t(),
Args :: #{from := jid:jid(), to := jid:jid(), packet := exml:element()},
Extra :: map().
user_send_packet(Acc, _, _) ->
-spec user_send_message(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) ->
mongoose_c2s_hooks:result().
user_send_message(Acc, _, _) ->
{From, To, Msg} = mongoose_acc:packet(Acc),
NewAcc = case Msg of
#xmlel{name = <<"message">>} -> maybe_process_message(Acc, From, To, Msg, outgoing);
_ -> Acc
end,
{ok, NewAcc}.
Acc1 = maybe_process_message(Acc, From, To, Msg, outgoing),
{ok, Acc1}.

-spec inbox_unread_count(Acc, Params, Extra) -> {ok, Acc} when
Acc :: mongoose_acc:t(),
Expand Down Expand Up @@ -570,7 +565,7 @@ hooks(HostType) ->
{disco_local_features, HostType, fun ?MODULE:disco_local_features/3, #{}, 99},
{remove_user, HostType, fun ?MODULE:remove_user/3, #{}, 50},
{remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 50},
{user_send_packet, HostType, fun ?MODULE:user_send_packet/3, #{}, 70},
{user_send_message, HostType, fun ?MODULE:user_send_message/3, #{}, 70},
{filter_local_packet, HostType, fun ?MODULE:filter_local_packet/3, #{}, 90},
{inbox_unread_count, HostType, fun ?MODULE:inbox_unread_count/3, #{}, 80},
{get_personal_data, HostType, fun ?MODULE:get_personal_data/3, #{}, 50}
Expand Down Expand Up @@ -606,6 +601,6 @@ should_be_stored_in_inbox(Acc, From, To, Msg, Dir, Type) ->
inbox_owner_exists(Acc, _, To, incoming, MessageType) -> % filter_local_packet
HostType = mongoose_acc:host_type(Acc),
mongoose_lib:does_local_user_exist(HostType, To, MessageType);
inbox_owner_exists(Acc, From, _, outgoing, _) -> % user_send_packet
inbox_owner_exists(Acc, From, _, outgoing, _) -> % user_send_message
HostType = mongoose_acc:host_type(Acc),
ejabberd_auth:does_user_exist(HostType, From, stored).
4 changes: 2 additions & 2 deletions src/jingle_sip/mod_jingle_sip.erl
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ process_u2p(#{username := U, phone := P}) ->
hooks(Host) ->
[{user_send_iq, Host, fun ?MODULE:user_send_iq/3, #{}, 10}].

-spec user_send_iq(mongoose_acc:t(), mongoose_c2s_hooks:hook_params(), gen_hook:extra()) ->
mongoose_c2s_hooks:hook_result().
-spec user_send_iq(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) ->
mongoose_c2s_hooks:result().
user_send_iq(Acc, _, _) ->
{From, To, Packet} = mongoose_acc:packet(Acc),
#jid{luser = StanzaTo} = To,
Expand Down
6 changes: 6 additions & 0 deletions src/metrics/mongoose_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,13 @@ filter_hook(sm_register_connection_hook) -> skip;
filter_hook(sm_remove_connection_hook) -> skip;
filter_hook(auth_failed) -> skip;
filter_hook(user_send_packet) -> skip;
filter_hook(user_send_message) -> skip;
filter_hook(user_send_presence) -> skip;
filter_hook(user_send_iq) -> skip;
filter_hook(user_receive_packet) -> skip;
filter_hook(user_receive_message) -> skip;
filter_hook(user_receive_presence) -> skip;
filter_hook(user_receive_iq) -> skip;
filter_hook(xmpp_bounce_message) -> skip;
filter_hook(xmpp_stanza_dropped) -> skip;
filter_hook(xmpp_send_element) -> skip;
Expand Down
6 changes: 3 additions & 3 deletions src/metrics/mongoose_metrics_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ get_hooks(HostType) ->
{sm_broadcast, HostType, fun ?MODULE:privacy_list_push/3, #{}, 1}
| c2s_hooks(HostType)].

-spec c2s_hooks(mongooseim:host_type()) -> gen_hook:hook_list(mongoose_c2s_hooks:hook_fn()).
-spec c2s_hooks(mongooseim:host_type()) -> gen_hook:hook_list(mongoose_c2s_hooks:fn()).
c2s_hooks(HostType) ->
[{user_send_packet, HostType, fun ?MODULE:user_send_packet/3, #{}, 50},
{user_open_session, HostType, fun ?MODULE:user_open_session/3, #{}, 50}].
Expand Down Expand Up @@ -95,7 +95,7 @@ auth_failed(Acc, #{server := Server}, _) ->
{ok, Acc}.

-spec user_send_packet(mongoose_acc:t(), mongoose_c2s:hook_params(), map()) ->
mongoose_c2s_hooks:hook_result().
mongoose_c2s_hooks:result().
user_send_packet(Acc, _Params, #{host_type := HostType}) ->
mongoose_metrics:update(HostType, xmppStanzaSent, 1),
mongoose_metrics:update(HostType, xmppStanzaCount, 1),
Expand Down Expand Up @@ -170,7 +170,7 @@ xmpp_send_element_type(HostType, #xmlel{name = <<"presence">>}) ->
mongoose_metrics:update(HostType, xmppPresenceReceived, 1).

-spec user_open_session(mongoose_acc:t(), mongoose_c2s:hook_params(), map()) ->
mongoose_c2s_hooks:hook_result().
mongoose_c2s_hooks:result().
user_open_session(Acc, _Params, #{host_type := HostType}) ->
mongoose_metrics:update(HostType, xmppStanzaSent, 1),
mongoose_metrics:update(HostType, xmppStanzaCount, 1),
Expand Down
Loading