Skip to content

Commit

Permalink
Treat session iq as hook instead of iq_handler to make it a blocking …
Browse files Browse the repository at this point in the history
…operation
  • Loading branch information
NelsonVides committed Oct 5, 2022
1 parent f902f3f commit cdb15bf
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/c2s/mongoose_c2s_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

-include("jlib.hrl").
-include("mongoose.hrl").
-include("mongoose_ns.hrl").

-behaviour(mongoose_listener).
-export([socket_type/0, start_listener/1]).
Expand All @@ -15,9 +14,7 @@
-ignore_xref([start_link/1]).

%% Hook handlers
-export([handle_user_open_session/3]).
%% backwards compatibility, process iq-session
-export([process_iq/5]).
-export([handle_user_open_session/3, user_send_iq/3]).

-type options() :: #{module := module(),
atom() => any()}.
Expand Down Expand Up @@ -50,8 +47,21 @@ handle_user_open_session(Acc, #{c2s_data := StateData}, #{host_type := HostType,
{stop, Acc}
end.

process_iq(Acc, _From, _To, #iq{type = set, sub_el = #xmlel{name = <<"session">>}} = IQ, _) ->
{Acc, IQ#iq{type = result}}.
%% backwards compatibility, process iq-session
-spec user_send_iq(mongoose_acc:t(), mongoose_c2s:hook_params(), map()) ->
mongoose_c2s_hooks:hook_result().
user_send_iq(Acc, _, _) ->
case mongoose_iq:info(Acc) of
{#iq{xmlns = ?NS_SESSION, type = set} = IQ, Acc1} ->
{stop, answer_session_iq(Acc1, IQ)};
_ ->
{ok, Acc}
end.

-spec answer_session_iq(mongoose_acc:t(), jlib:iq()) -> mongoose_acc:t().
answer_session_iq(Acc, IqSet) ->
IqResult = jlib:make_result_iq_reply(IqSet),
mongoose_c2s_acc:to_acc(Acc, socket_send, jlib:iq_to_xml(IqResult)).

%% ranch_protocol
start_link(Ref, Transport, Opts = #{hibernate_after := HibernateAfterTimeout}) ->
Expand All @@ -65,18 +75,18 @@ start_link(Opts) ->
-spec init(options()) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init(#{module := Module} = Opts) ->
HostTypes = ?ALL_HOST_TYPES,
acc_session_iq_handler(HostTypes),
iq_session_handler(HostTypes),
maybe_add_access_check(HostTypes, Opts),
TransportOpts = prepare_socket_opts(Opts),
ListenerId = mongoose_listener_config:listener_id(Opts),
OptsWithTlsConfig = process_tls_opts(Opts),
Child = ranch:child_spec(ListenerId, ranch_tcp, TransportOpts, Module, OptsWithTlsConfig),
{ok, {#{strategy => one_for_one, intensity => 100, period => 1}, [Child]}}.

acc_session_iq_handler(HostTypes) ->
[ gen_iq_handler:add_iq_handler_for_domain(
HostType, ?NS_SESSION, ejabberd_sm, fun ?MODULE:process_iq/5, #{}, no_queue)
|| HostType <- HostTypes ].
iq_session_handler(HostTypes) ->
SessionHooks = [ {user_send_iq, HostType, fun ?MODULE:user_send_iq/3, #{}, 10}
|| HostType <- HostTypes ],
gen_hook:add_handlers(SessionHooks).

maybe_add_access_check(_, #{access := all}) ->
ok;
Expand Down

0 comments on commit cdb15bf

Please sign in to comment.