2014-09-11 17:44:29 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_client_state.erl
|
2015-11-04 01:19:19 +01:00
|
|
|
%%% Author : Holger Weiss <holger@zedat.fu-berlin.de>
|
2014-09-11 17:44:29 +02:00
|
|
|
%%% Purpose : Filter stanzas sent to inactive clients (XEP-0352)
|
2015-11-04 01:19:19 +01:00
|
|
|
%%% Created : 11 Sep 2014 by Holger Weiss <holger@zedat.fu-berlin.de>
|
2014-09-11 17:44:29 +02:00
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2014-2020 ProcessOne
|
2014-09-11 17:44:29 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_client_state).
|
|
|
|
-author('holger@zedat.fu-berlin.de').
|
2015-06-22 13:11:11 +02:00
|
|
|
-protocol({xep, 85, '2.1'}).
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 352, '0.1'}).
|
2014-09-11 17:44:29 +02:00
|
|
|
|
2018-01-31 14:57:43 +01:00
|
|
|
-behaviour(gen_mod).
|
2014-09-11 17:44:29 +02:00
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
%% gen_mod callbacks.
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([start/2, stop/1, reload/3, mod_opt_type/1, depends/2, mod_options/1]).
|
2020-01-08 10:24:51 +01:00
|
|
|
-export([mod_doc/0]).
|
2016-05-18 21:30:38 +02:00
|
|
|
|
|
|
|
%% ejabberd_hooks callbacks.
|
2017-01-09 15:02:17 +01:00
|
|
|
-export([filter_presence/1, filter_chat_states/1,
|
|
|
|
filter_pep/1, filter_other/1,
|
|
|
|
c2s_stream_started/2, add_stream_feature/2,
|
2018-04-01 17:13:04 +02:00
|
|
|
c2s_authenticated_packet/2, csi_activity/2,
|
|
|
|
c2s_copy_session/2, c2s_session_resumed/1]).
|
2014-09-11 17:44:29 +02:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2020-01-08 10:24:51 +01:00
|
|
|
-include("translate.hrl").
|
2014-09-11 17:44:29 +02:00
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
-define(CSI_QUEUE_MAX, 100).
|
|
|
|
|
2016-06-05 21:48:03 +02:00
|
|
|
-type csi_type() :: presence | chatstate | {pep, binary()}.
|
2019-06-27 14:22:27 +02:00
|
|
|
-type csi_queue() :: {non_neg_integer(), #{csi_key() => csi_element()}}.
|
2017-01-09 15:02:17 +01:00
|
|
|
-type csi_timestamp() :: {non_neg_integer(), erlang:timestamp()}.
|
2017-04-06 22:19:00 +02:00
|
|
|
-type csi_key() :: {ljid(), csi_type()}.
|
|
|
|
-type csi_element() :: {csi_timestamp(), stanza()}.
|
2017-01-09 15:02:17 +01:00
|
|
|
-type c2s_state() :: ejabberd_c2s:state().
|
|
|
|
-type filter_acc() :: {stanza() | drop, c2s_state()}.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% gen_mod callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec start(binary(), gen_mod:opts()) -> ok.
|
2014-09-11 17:44:29 +02:00
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
QueuePresence = mod_client_state_opt:queue_presence(Opts),
|
|
|
|
QueueChatStates = mod_client_state_opt:queue_chat_states(Opts),
|
|
|
|
QueuePEP = mod_client_state_opt:queue_pep(Opts),
|
2016-05-17 22:12:04 +02:00
|
|
|
if QueuePresence; QueueChatStates; QueuePEP ->
|
2017-02-22 17:46:47 +01:00
|
|
|
register_hooks(Host),
|
2014-09-25 18:28:20 +02:00
|
|
|
if QueuePresence ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_filter_send, Host, ?MODULE,
|
2014-09-25 18:28:20 +02:00
|
|
|
filter_presence, 50);
|
|
|
|
true -> ok
|
|
|
|
end,
|
2016-05-17 20:55:45 +02:00
|
|
|
if QueueChatStates ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_filter_send, Host, ?MODULE,
|
2014-09-25 18:28:20 +02:00
|
|
|
filter_chat_states, 50);
|
|
|
|
true -> ok
|
2016-05-17 19:27:18 +02:00
|
|
|
end,
|
2016-05-17 22:12:04 +02:00
|
|
|
if QueuePEP ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_filter_send, Host, ?MODULE,
|
2016-05-17 22:12:04 +02:00
|
|
|
filter_pep, 50);
|
|
|
|
true -> ok
|
2017-02-22 17:46:47 +01:00
|
|
|
end;
|
2014-09-11 17:44:29 +02:00
|
|
|
true -> ok
|
2016-05-17 19:27:18 +02:00
|
|
|
end.
|
2014-09-11 17:44:29 +02:00
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
-spec stop(binary()) -> ok.
|
2014-09-11 17:44:29 +02:00
|
|
|
stop(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
QueuePresence = mod_client_state_opt:queue_presence(Host),
|
|
|
|
QueueChatStates = mod_client_state_opt:queue_chat_states(Host),
|
|
|
|
QueuePEP = mod_client_state_opt:queue_pep(Host),
|
2016-05-17 22:12:04 +02:00
|
|
|
if QueuePresence; QueueChatStates; QueuePEP ->
|
2017-02-22 17:46:47 +01:00
|
|
|
unregister_hooks(Host),
|
2016-05-08 16:45:31 +02:00
|
|
|
if QueuePresence ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_filter_send, Host, ?MODULE,
|
2016-05-08 16:45:31 +02:00
|
|
|
filter_presence, 50);
|
|
|
|
true -> ok
|
|
|
|
end,
|
2016-05-17 20:55:45 +02:00
|
|
|
if QueueChatStates ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_filter_send, Host, ?MODULE,
|
2016-05-08 16:45:31 +02:00
|
|
|
filter_chat_states, 50);
|
|
|
|
true -> ok
|
2016-05-17 19:27:18 +02:00
|
|
|
end,
|
2016-05-17 22:12:04 +02:00
|
|
|
if QueuePEP ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_filter_send, Host, ?MODULE,
|
2016-05-17 22:12:04 +02:00
|
|
|
filter_pep, 50);
|
|
|
|
true -> ok
|
2017-02-22 17:46:47 +01:00
|
|
|
end;
|
2016-05-08 16:45:31 +02:00
|
|
|
true -> ok
|
2016-05-17 19:27:18 +02:00
|
|
|
end.
|
2014-09-11 17:44:29 +02:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-spec reload(binary(), gen_mod:opts(), gen_mod:opts()) -> ok.
|
|
|
|
reload(Host, NewOpts, _OldOpts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
QueuePresence = mod_client_state_opt:queue_presence(NewOpts),
|
|
|
|
QueueChatStates = mod_client_state_opt:queue_chat_states(NewOpts),
|
|
|
|
QueuePEP = mod_client_state_opt:queue_pep(NewOpts),
|
2017-02-22 17:46:47 +01:00
|
|
|
if QueuePresence; QueueChatStates; QueuePEP ->
|
|
|
|
register_hooks(Host);
|
|
|
|
true ->
|
|
|
|
unregister_hooks(Host)
|
|
|
|
end,
|
|
|
|
if QueuePresence ->
|
|
|
|
ejabberd_hooks:add(c2s_filter_send, Host, ?MODULE,
|
|
|
|
filter_presence, 50);
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:delete(c2s_filter_send, Host, ?MODULE,
|
|
|
|
filter_presence, 50)
|
|
|
|
end,
|
|
|
|
if QueueChatStates ->
|
|
|
|
ejabberd_hooks:add(c2s_filter_send, Host, ?MODULE,
|
|
|
|
filter_chat_states, 50);
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:delete(c2s_filter_send, Host, ?MODULE,
|
|
|
|
filter_chat_states, 50)
|
|
|
|
end,
|
|
|
|
if QueuePEP ->
|
|
|
|
ejabberd_hooks:add(c2s_filter_send, Host, ?MODULE,
|
|
|
|
filter_pep, 50);
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:delete(c2s_filter_send, Host, ?MODULE,
|
|
|
|
filter_pep, 50)
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec mod_opt_type(atom()) -> econf:validator().
|
2016-05-18 21:30:38 +02:00
|
|
|
mod_opt_type(queue_presence) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2016-05-18 21:30:38 +02:00
|
|
|
mod_opt_type(queue_chat_states) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2016-05-18 21:30:38 +02:00
|
|
|
mod_opt_type(queue_pep) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool().
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(_) ->
|
|
|
|
[{queue_presence, true},
|
|
|
|
{queue_chat_states, true},
|
|
|
|
{queue_pep, true}].
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("This module allows for queueing certain types of stanzas "
|
|
|
|
"when a client indicates that the user is not actively using "
|
|
|
|
"the client right now (see https://xmpp.org/extensions/xep-0352.html"
|
|
|
|
"[XEP-0352: Client State Indication]). This can save bandwidth and "
|
|
|
|
"resources."), "",
|
|
|
|
?T("A stanza is dropped from the queue if it's effectively obsoleted "
|
|
|
|
"by a new one (e.g., a new presence stanza would replace an old "
|
|
|
|
"one from the same client). The queue is flushed if a stanza arrives "
|
|
|
|
"that won't be queued, or if the queue size reaches a certain limit "
|
|
|
|
"(currently 100 stanzas), or if the client becomes active again.")],
|
|
|
|
opts =>
|
|
|
|
[{queue_presence,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("While a client is inactive, queue presence stanzas "
|
|
|
|
"that indicate (un)availability. The default value is 'true'.")}},
|
|
|
|
{queue_chat_states,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Queue \"standalone\" chat state notifications (as defined in "
|
|
|
|
"https://xmpp.org/extensions/xep-0085.html"
|
|
|
|
"[XEP-0085: Chat State Notifications]) while a client "
|
|
|
|
"indicates inactivity. The default value is 'true'.")}},
|
|
|
|
{queue_pep,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Queue PEP notifications while a client is inactive. "
|
|
|
|
"When the queue is flushed, only the most recent notification "
|
|
|
|
"of a given PEP node is delivered. The default value is 'true'.")}}]}.
|
|
|
|
|
2016-07-07 11:17:38 +02:00
|
|
|
-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
|
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-spec register_hooks(binary()) -> ok.
|
|
|
|
register_hooks(Host) ->
|
|
|
|
ejabberd_hooks:add(c2s_stream_started, Host, ?MODULE,
|
|
|
|
c2s_stream_started, 50),
|
|
|
|
ejabberd_hooks:add(c2s_post_auth_features, Host, ?MODULE,
|
|
|
|
add_stream_feature, 50),
|
|
|
|
ejabberd_hooks:add(c2s_authenticated_packet, Host, ?MODULE,
|
|
|
|
c2s_authenticated_packet, 50),
|
2018-04-01 17:13:04 +02:00
|
|
|
ejabberd_hooks:add(csi_activity, Host, ?MODULE,
|
|
|
|
csi_activity, 50),
|
2017-02-22 17:46:47 +01:00
|
|
|
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_filter_send, Host, ?MODULE,
|
|
|
|
filter_other, 75).
|
|
|
|
|
|
|
|
-spec unregister_hooks(binary()) -> ok.
|
|
|
|
unregister_hooks(Host) ->
|
|
|
|
ejabberd_hooks:delete(c2s_stream_started, Host, ?MODULE,
|
|
|
|
c2s_stream_started, 50),
|
|
|
|
ejabberd_hooks:delete(c2s_post_auth_features, Host, ?MODULE,
|
|
|
|
add_stream_feature, 50),
|
|
|
|
ejabberd_hooks:delete(c2s_authenticated_packet, Host, ?MODULE,
|
|
|
|
c2s_authenticated_packet, 50),
|
2018-04-01 17:13:04 +02:00
|
|
|
ejabberd_hooks:delete(csi_activity, Host, ?MODULE,
|
|
|
|
csi_activity, 50),
|
2017-02-22 17:46:47 +01:00
|
|
|
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_filter_send, Host, ?MODULE,
|
|
|
|
filter_other, 75).
|
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% ejabberd_hooks callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec c2s_stream_started(c2s_state(), stream_start()) -> c2s_state().
|
|
|
|
c2s_stream_started(State, _) ->
|
2017-10-05 10:03:36 +02:00
|
|
|
init_csi_state(State).
|
2017-01-09 15:02:17 +01:00
|
|
|
|
|
|
|
-spec c2s_authenticated_packet(c2s_state(), xmpp_element()) -> c2s_state().
|
2018-04-01 17:13:04 +02:00
|
|
|
c2s_authenticated_packet(#{lserver := LServer} = C2SState, #csi{type = active}) ->
|
|
|
|
ejabberd_hooks:run_fold(csi_activity, LServer, C2SState, [active]);
|
|
|
|
c2s_authenticated_packet(#{lserver := LServer} = C2SState, #csi{type = inactive}) ->
|
|
|
|
ejabberd_hooks:run_fold(csi_activity, LServer, C2SState, [inactive]);
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_authenticated_packet(C2SState, _) ->
|
|
|
|
C2SState.
|
|
|
|
|
2018-04-01 17:13:04 +02:00
|
|
|
-spec csi_activity(c2s_state(), active | inactive) -> c2s_state().
|
|
|
|
csi_activity(C2SState, active) ->
|
|
|
|
C2SState1 = C2SState#{csi_state => active},
|
|
|
|
flush_queue(C2SState1);
|
|
|
|
csi_activity(C2SState, inactive) ->
|
|
|
|
C2SState#{csi_state => inactive}.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec c2s_copy_session(c2s_state(), c2s_state()) -> c2s_state().
|
2017-06-21 01:05:46 +02:00
|
|
|
c2s_copy_session(C2SState, #{csi_queue := Q}) ->
|
|
|
|
C2SState#{csi_queue => Q};
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_copy_session(C2SState, _) ->
|
|
|
|
C2SState.
|
|
|
|
|
|
|
|
-spec c2s_session_resumed(c2s_state()) -> c2s_state().
|
|
|
|
c2s_session_resumed(C2SState) ->
|
|
|
|
flush_queue(C2SState).
|
|
|
|
|
|
|
|
-spec filter_presence(filter_acc()) -> filter_acc().
|
|
|
|
filter_presence({#presence{meta = #{csi_resend := true}}, _} = Acc) ->
|
|
|
|
Acc;
|
|
|
|
filter_presence({#presence{to = To, type = Type} = Pres,
|
|
|
|
#{csi_state := inactive} = C2SState})
|
|
|
|
when Type == available; Type == unavailable ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Got availability presence stanza for ~ts", [jid:encode(To)]),
|
2017-01-09 15:02:17 +01:00
|
|
|
enqueue_stanza(presence, Pres, C2SState);
|
|
|
|
filter_presence(Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
-spec filter_chat_states(filter_acc()) -> filter_acc().
|
|
|
|
filter_chat_states({#message{meta = #{csi_resend := true}}, _} = Acc) ->
|
|
|
|
Acc;
|
|
|
|
filter_chat_states({#message{from = From, to = To} = Msg,
|
|
|
|
#{csi_state := inactive} = C2SState} = Acc) ->
|
2018-10-25 01:05:45 +02:00
|
|
|
case misc:is_standalone_chat_state(Msg) of
|
2016-07-18 14:01:32 +02:00
|
|
|
true ->
|
|
|
|
case {From, To} of
|
|
|
|
{#jid{luser = U, lserver = S}, #jid{luser = U, lserver = S}} ->
|
|
|
|
%% Don't queue (carbon copies of) chat states from other
|
|
|
|
%% resources, as they might be used to sync the state of
|
|
|
|
%% conversations across clients.
|
|
|
|
Acc;
|
|
|
|
_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Got standalone chat state notification for ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[jid:encode(To)]),
|
2017-01-09 15:02:17 +01:00
|
|
|
enqueue_stanza(chatstate, Msg, C2SState)
|
2016-07-18 14:01:32 +02:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
2014-09-11 17:44:29 +02:00
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
filter_chat_states(Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
-spec filter_pep(filter_acc()) -> filter_acc().
|
|
|
|
filter_pep({#message{meta = #{csi_resend := true}}, _} = Acc) ->
|
|
|
|
Acc;
|
|
|
|
filter_pep({#message{to = To} = Msg,
|
|
|
|
#{csi_state := inactive} = C2SState} = Acc) ->
|
|
|
|
case get_pep_node(Msg) of
|
2016-07-18 14:01:32 +02:00
|
|
|
undefined ->
|
|
|
|
Acc;
|
|
|
|
Node ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Got PEP notification for ~ts", [jid:encode(To)]),
|
2017-01-09 15:02:17 +01:00
|
|
|
enqueue_stanza({pep, Node}, Msg, C2SState)
|
2016-05-17 22:12:04 +02:00
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
filter_pep(Acc) ->
|
|
|
|
Acc.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec filter_other(filter_acc()) -> filter_acc().
|
|
|
|
filter_other({Stanza, #{jid := JID} = C2SState} = Acc) when ?is_stanza(Stanza) ->
|
|
|
|
case xmpp:get_meta(Stanza) of
|
|
|
|
#{csi_resend := true} ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Won't add stanza for ~ts to CSI queue", [jid:encode(JID)]),
|
2017-10-06 20:48:54 +02:00
|
|
|
From = case xmpp:get_from(Stanza) of
|
|
|
|
undefined -> JID;
|
|
|
|
F -> F
|
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
C2SState1 = dequeue_sender(From, C2SState),
|
|
|
|
{Stanza, C2SState1}
|
|
|
|
end;
|
|
|
|
filter_other(Acc) ->
|
|
|
|
Acc.
|
2016-05-17 22:12:04 +02:00
|
|
|
|
2017-02-16 12:18:36 +01:00
|
|
|
-spec add_stream_feature([xmpp_element()], binary()) -> [xmpp_element()].
|
2017-01-09 15:02:17 +01:00
|
|
|
add_stream_feature(Features, Host) ->
|
|
|
|
case gen_mod:is_loaded(Host, ?MODULE) of
|
|
|
|
true ->
|
2018-06-04 23:05:11 +02:00
|
|
|
[#feature_csi{} | Features];
|
2017-01-09 15:02:17 +01:00
|
|
|
false ->
|
|
|
|
Features
|
|
|
|
end.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Internal functions.
|
|
|
|
%%--------------------------------------------------------------------
|
2017-10-05 10:03:36 +02:00
|
|
|
-spec init_csi_state(c2s_state()) -> c2s_state().
|
|
|
|
init_csi_state(C2SState) ->
|
|
|
|
C2SState#{csi_state => active, csi_queue => queue_new()}.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec enqueue_stanza(csi_type(), stanza(), c2s_state()) -> filter_acc().
|
|
|
|
enqueue_stanza(Type, Stanza, #{csi_state := inactive,
|
|
|
|
csi_queue := Q} = C2SState) ->
|
|
|
|
case queue_len(Q) >= ?CSI_QUEUE_MAX of
|
|
|
|
true ->
|
2016-05-17 19:27:18 +02:00
|
|
|
?DEBUG("CSI queue too large, going to flush it", []),
|
2017-01-09 15:02:17 +01:00
|
|
|
C2SState1 = flush_queue(C2SState),
|
|
|
|
enqueue_stanza(Type, Stanza, C2SState1);
|
|
|
|
false ->
|
2017-04-06 22:19:00 +02:00
|
|
|
From = jid:tolower(xmpp:get_from(Stanza)),
|
|
|
|
Q1 = queue_in({From, Type}, Stanza, Q),
|
2017-01-09 15:02:17 +01:00
|
|
|
{stop, {drop, C2SState#{csi_queue => Q1}}}
|
|
|
|
end;
|
|
|
|
enqueue_stanza(_Type, Stanza, State) ->
|
|
|
|
{Stanza, State}.
|
|
|
|
|
|
|
|
-spec dequeue_sender(jid(), c2s_state()) -> c2s_state().
|
2017-04-06 22:19:00 +02:00
|
|
|
dequeue_sender(#jid{luser = U, lserver = S} = Sender,
|
2017-10-05 10:03:36 +02:00
|
|
|
#{jid := JID} = C2SState) ->
|
|
|
|
case maps:get(csi_queue, C2SState, undefined) of
|
|
|
|
undefined ->
|
|
|
|
%% This may happen when the module is (re)loaded in runtime
|
|
|
|
init_csi_state(C2SState);
|
|
|
|
Q ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Flushing packets of ~ts@~ts from CSI queue of ~ts",
|
2017-10-05 10:03:36 +02:00
|
|
|
[U, S, jid:encode(JID)]),
|
|
|
|
{Elems, Q1} = queue_take(Sender, Q),
|
|
|
|
C2SState1 = flush_stanzas(C2SState, Elems),
|
|
|
|
C2SState1#{csi_queue => Q1}
|
|
|
|
end.
|
2016-05-17 19:27:18 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec flush_queue(c2s_state()) -> c2s_state().
|
|
|
|
flush_queue(#{csi_queue := Q, jid := JID} = C2SState) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Flushing CSI queue of ~ts", [jid:encode(JID)]),
|
2017-01-09 15:02:17 +01:00
|
|
|
C2SState1 = flush_stanzas(C2SState, queue_to_list(Q)),
|
|
|
|
C2SState1#{csi_queue => queue_new()}.
|
|
|
|
|
|
|
|
-spec flush_stanzas(c2s_state(),
|
|
|
|
[{csi_type(), csi_timestamp(), stanza()}]) -> c2s_state().
|
|
|
|
flush_stanzas(#{lserver := LServer} = C2SState, Elems) ->
|
|
|
|
lists:foldl(
|
2017-04-06 22:19:00 +02:00
|
|
|
fun({Time, Stanza}, AccState) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Stanza1 = add_delay_info(Stanza, LServer, Time),
|
|
|
|
ejabberd_c2s:send(AccState, Stanza1)
|
|
|
|
end, C2SState, Elems).
|
|
|
|
|
|
|
|
-spec add_delay_info(stanza(), binary(), csi_timestamp()) -> stanza().
|
|
|
|
add_delay_info(Stanza, LServer, {_Seq, TimeStamp}) ->
|
2018-10-25 01:05:45 +02:00
|
|
|
Stanza1 = misc:add_delay_info(
|
2017-01-09 15:02:17 +01:00
|
|
|
Stanza, jid:make(LServer), TimeStamp,
|
|
|
|
<<"Client Inactive">>),
|
|
|
|
xmpp:put_meta(Stanza1, csi_resend, true).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_pep_node(message()) -> binary() | undefined.
|
|
|
|
get_pep_node(#message{from = #jid{luser = <<>>}}) ->
|
|
|
|
%% It's not PEP.
|
|
|
|
undefined;
|
|
|
|
get_pep_node(#message{} = Msg) ->
|
2016-08-30 08:48:08 +02:00
|
|
|
case xmpp:get_subtag(Msg, #ps_event{}) of
|
|
|
|
#ps_event{items = #ps_items{node = Node}} ->
|
2016-07-18 14:01:32 +02:00
|
|
|
Node;
|
|
|
|
_ ->
|
|
|
|
undefined
|
2016-05-18 21:30:38 +02:00
|
|
|
end.
|
2017-01-09 15:02:17 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Queue interface
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec queue_new() -> csi_queue().
|
|
|
|
queue_new() ->
|
2017-04-06 21:01:26 +02:00
|
|
|
{0, #{}}.
|
2017-01-09 15:02:17 +01:00
|
|
|
|
2017-04-06 23:00:53 +02:00
|
|
|
-spec queue_in(csi_key(), stanza(), csi_queue()) -> csi_queue().
|
|
|
|
queue_in(Key, Stanza, {Seq, Q}) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Seq1 = Seq + 1,
|
2019-02-27 09:56:20 +01:00
|
|
|
Time = {Seq1, erlang:timestamp()},
|
2017-04-06 23:00:53 +02:00
|
|
|
Q1 = maps:put(Key, {Time, Stanza}, Q),
|
2017-04-06 22:19:00 +02:00
|
|
|
{Seq1, Q1}.
|
|
|
|
|
|
|
|
-spec queue_take(jid(), csi_queue()) -> {[csi_element()], csi_queue()}.
|
|
|
|
queue_take(#jid{luser = LUser, lserver = LServer}, {Seq, Q}) ->
|
|
|
|
{Vals, Q1} = maps:fold(fun({{U, S, _}, _} = Key, Val, {AccVals, AccQ})
|
|
|
|
when U == LUser, S == LServer ->
|
|
|
|
{[Val | AccVals], maps:remove(Key, AccQ)};
|
|
|
|
(_, _, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, {[], Q}, Q),
|
|
|
|
{lists:keysort(1, Vals), {Seq, Q1}}.
|
2017-01-09 15:02:17 +01:00
|
|
|
|
|
|
|
-spec queue_len(csi_queue()) -> non_neg_integer().
|
2017-04-06 21:01:26 +02:00
|
|
|
queue_len({_, Q}) ->
|
|
|
|
maps:size(Q).
|
2017-01-09 15:02:17 +01:00
|
|
|
|
2017-04-06 22:19:00 +02:00
|
|
|
-spec queue_to_list(csi_queue()) -> [csi_element()].
|
|
|
|
queue_to_list({_, Q}) ->
|
|
|
|
lists:keysort(1, maps:values(Q)).
|