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
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2014-2017 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
|
|
|
|
|
|
|
-behavior(gen_mod).
|
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
%% gen_mod callbacks.
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, mod_opt_type/1, depends/2]).
|
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,
|
|
|
|
c2s_copy_session/2, c2s_authenticated_packet/2,
|
|
|
|
c2s_session_resumed/1]).
|
2014-09-11 17:44:29 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("logger.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.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()}.
|
2017-04-06 21:01:26 +02:00
|
|
|
-type csi_queue() :: {non_neg_integer(), map()}.
|
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) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
QueuePresence = gen_mod:get_opt(queue_presence, Opts, true),
|
|
|
|
QueueChatStates = gen_mod:get_opt(queue_chat_states, Opts, true),
|
|
|
|
QueuePEP = gen_mod:get_opt(queue_pep, Opts, true),
|
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) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
QueuePresence = gen_mod:get_module_opt(Host, ?MODULE, queue_presence, true),
|
|
|
|
QueueChatStates = gen_mod:get_module_opt(Host, ?MODULE, queue_chat_states, true),
|
|
|
|
QueuePEP = gen_mod:get_module_opt(Host, ?MODULE, queue_pep, true),
|
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) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
QueuePresence = gen_mod:get_opt(queue_presence, NewOpts, true),
|
|
|
|
QueueChatStates = gen_mod:get_opt(queue_chat_states, NewOpts, true),
|
|
|
|
QueuePEP = gen_mod:get_opt(queue_pep, NewOpts, true),
|
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.
|
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
-spec mod_opt_type(atom()) -> fun((term()) -> term()) | [atom()].
|
|
|
|
mod_opt_type(queue_presence) ->
|
|
|
|
fun(B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(queue_chat_states) ->
|
|
|
|
fun(B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(queue_pep) ->
|
|
|
|
fun(B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(_) -> [queue_presence, queue_chat_states, queue_pep].
|
|
|
|
|
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),
|
|
|
|
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),
|
|
|
|
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().
|
|
|
|
c2s_authenticated_packet(C2SState, #csi{type = active}) ->
|
|
|
|
C2SState1 = C2SState#{csi_state => active},
|
|
|
|
flush_queue(C2SState1);
|
|
|
|
c2s_authenticated_packet(C2SState, #csi{type = inactive}) ->
|
|
|
|
C2SState#{csi_state => inactive};
|
|
|
|
c2s_authenticated_packet(C2SState, _) ->
|
|
|
|
C2SState.
|
|
|
|
|
|
|
|
-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 ->
|
2017-02-26 08:07:12 +01:00
|
|
|
?DEBUG("Got availability presence stanza for ~s", [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) ->
|
|
|
|
case xmpp_util: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;
|
|
|
|
_ ->
|
2016-08-05 23:47:18 +02:00
|
|
|
?DEBUG("Got standalone chat state notification for ~s",
|
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 ->
|
2017-02-26 08:07:12 +01:00
|
|
|
?DEBUG("Got PEP notification for ~s", [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;
|
|
|
|
_ ->
|
2017-02-26 08:07:12 +01:00
|
|
|
?DEBUG("Won't add stanza for ~s 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 ->
|
|
|
|
[#feature_csi{xmlns = <<"urn:xmpp:csi:0">>} | Features];
|
|
|
|
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 ->
|
|
|
|
?DEBUG("Flushing packets of ~s@~s from CSI queue of ~s",
|
|
|
|
[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) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
?DEBUG("Flushing CSI queue of ~s", [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}) ->
|
|
|
|
Stanza1 = xmpp_util:add_delay_info(
|
|
|
|
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,
|
|
|
|
Time = {Seq1, p1_time_compat: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)).
|