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
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2014-2016 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.
|
2016-07-07 11:17:38 +02:00
|
|
|
-export([start/2, stop/1, mod_opt_type/1, depends/2]).
|
2016-05-18 21:30:38 +02:00
|
|
|
|
|
|
|
%% ejabberd_hooks callbacks.
|
2016-08-05 23:47:18 +02:00
|
|
|
-export([filter_presence/4, filter_chat_states/4, filter_pep/4, filter_other/4,
|
|
|
|
flush_queue/3, add_stream_feature/2]).
|
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()}.
|
2016-05-18 21:30:38 +02:00
|
|
|
-type csi_key() :: {ljid(), csi_type()}.
|
|
|
|
-type csi_stanza() :: {csi_key(), erlang:timestamp(), xmlel()}.
|
|
|
|
-type csi_queue() :: [csi_stanza()].
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% gen_mod callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec start(binary(), gen_mod:opts()) -> ok.
|
|
|
|
|
2014-09-11 17:44:29 +02:00
|
|
|
start(Host, Opts) ->
|
2016-05-17 22:12:04 +02:00
|
|
|
QueuePresence =
|
|
|
|
gen_mod:get_opt(queue_presence, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true),
|
|
|
|
QueueChatStates =
|
|
|
|
gen_mod:get_opt(queue_chat_states, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true),
|
|
|
|
QueuePEP =
|
|
|
|
gen_mod:get_opt(queue_pep, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
2016-06-29 22:22:49 +02:00
|
|
|
true),
|
2016-05-17 22:12:04 +02:00
|
|
|
if QueuePresence; QueueChatStates; QueuePEP ->
|
2014-09-25 18:28:20 +02:00
|
|
|
ejabberd_hooks:add(c2s_post_auth_features, Host, ?MODULE,
|
|
|
|
add_stream_feature, 50),
|
|
|
|
if QueuePresence ->
|
|
|
|
ejabberd_hooks:add(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
filter_presence, 50);
|
|
|
|
true -> ok
|
|
|
|
end,
|
2016-05-17 20:55:45 +02:00
|
|
|
if QueueChatStates ->
|
2014-09-25 18:28:20 +02:00
|
|
|
ejabberd_hooks:add(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
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 ->
|
|
|
|
ejabberd_hooks:add(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
filter_pep, 50);
|
|
|
|
true -> ok
|
|
|
|
end,
|
2016-05-17 19:27:18 +02:00
|
|
|
ejabberd_hooks:add(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
filter_other, 100),
|
|
|
|
ejabberd_hooks:add(csi_flush_queue, Host, ?MODULE,
|
|
|
|
flush_queue, 50);
|
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) ->
|
2016-05-17 22:12:04 +02:00
|
|
|
QueuePresence =
|
|
|
|
gen_mod:get_module_opt(Host, ?MODULE, queue_presence,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true),
|
|
|
|
QueueChatStates =
|
|
|
|
gen_mod:get_module_opt(Host, ?MODULE, queue_chat_states,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
true),
|
|
|
|
QueuePEP =
|
|
|
|
gen_mod:get_module_opt(Host, ?MODULE, queue_pep,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
2016-06-29 22:22:49 +02:00
|
|
|
true),
|
2016-05-17 22:12:04 +02:00
|
|
|
if QueuePresence; QueueChatStates; QueuePEP ->
|
2016-05-08 16:45:31 +02:00
|
|
|
ejabberd_hooks:delete(c2s_post_auth_features, Host, ?MODULE,
|
|
|
|
add_stream_feature, 50),
|
|
|
|
if QueuePresence ->
|
|
|
|
ejabberd_hooks:delete(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
filter_presence, 50);
|
|
|
|
true -> ok
|
|
|
|
end,
|
2016-05-17 20:55:45 +02:00
|
|
|
if QueueChatStates ->
|
2016-05-08 16:45:31 +02:00
|
|
|
ejabberd_hooks:delete(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
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 ->
|
|
|
|
ejabberd_hooks:delete(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
filter_pep, 50);
|
|
|
|
true -> ok
|
|
|
|
end,
|
2016-05-17 19:27:18 +02:00
|
|
|
ejabberd_hooks:delete(csi_filter_stanza, Host, ?MODULE,
|
|
|
|
filter_other, 100),
|
|
|
|
ejabberd_hooks:delete(csi_flush_queue, Host, ?MODULE,
|
|
|
|
flush_queue, 50);
|
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
|
|
|
|
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) ->
|
|
|
|
[].
|
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% ejabberd_hooks callbacks.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec filter_presence({ejabberd_c2s:state(), [stanza()]}, binary(), jid(), stanza())
|
2016-08-09 09:56:32 +02:00
|
|
|
-> {ejabberd_c2s:state(), [stanza()]} |
|
|
|
|
{stop, {ejabberd_c2s:state(), [stanza()]}}.
|
2014-09-25 18:28:20 +02:00
|
|
|
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_presence({C2SState, _OutStanzas} = Acc, Host, To,
|
2016-07-18 14:01:32 +02:00
|
|
|
#presence{type = Type} = Stanza) ->
|
2016-08-09 09:56:32 +02:00
|
|
|
if Type == available; Type == unavailable ->
|
2016-11-12 11:27:15 +01:00
|
|
|
?DEBUG("Got availability presence stanza for ~s",
|
|
|
|
[jid:to_string(To)]),
|
2016-07-18 14:01:32 +02:00
|
|
|
queue_add(presence, Stanza, Host, C2SState);
|
|
|
|
true ->
|
|
|
|
Acc
|
2014-09-11 17:44:29 +02:00
|
|
|
end;
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_presence(Acc, _Host, _To, _Stanza) -> Acc.
|
2014-09-11 17:44:29 +02:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec filter_chat_states({ejabberd_c2s:state(), [stanza()]}, binary(), jid(), stanza())
|
2016-08-09 09:56:32 +02:00
|
|
|
-> {ejabberd_c2s:state(), [stanza()]} |
|
|
|
|
{stop, {ejabberd_c2s:state(), [stanza()]}}.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_chat_states({C2SState, _OutStanzas} = Acc, Host, To,
|
2016-11-12 11:27:15 +01:00
|
|
|
#message{from = From} = Stanza) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
case xmpp_util:is_standalone_chat_state(Stanza) of
|
|
|
|
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",
|
|
|
|
[jid:to_string(To)]),
|
2016-07-18 14:01:32 +02:00
|
|
|
queue_add(chatstate, Stanza, Host, C2SState)
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
2014-09-11 17:44:29 +02:00
|
|
|
end;
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_chat_states(Acc, _Host, _To, _Stanza) -> Acc.
|
2016-05-17 19:27:18 +02:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec filter_pep({ejabberd_c2s:state(), [stanza()]}, binary(), jid(), stanza())
|
2016-08-09 09:56:32 +02:00
|
|
|
-> {ejabberd_c2s:state(), [stanza()]} |
|
|
|
|
{stop, {ejabberd_c2s:state(), [stanza()]}}.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
filter_pep({C2SState, _OutStanzas} = Acc, Host, To, #message{} = Stanza) ->
|
2016-06-05 21:48:03 +02:00
|
|
|
case get_pep_node(Stanza) of
|
2016-07-18 14:01:32 +02:00
|
|
|
undefined ->
|
|
|
|
Acc;
|
|
|
|
Node ->
|
2016-11-12 11:27:15 +01:00
|
|
|
?DEBUG("Got PEP notification for ~s", [jid:to_string(To)]),
|
2016-07-18 14:01:32 +02:00
|
|
|
queue_add({pep, Node}, Stanza, Host, C2SState)
|
2016-05-17 22:12:04 +02:00
|
|
|
end;
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_pep(Acc, _Host, _To, _Stanza) -> Acc.
|
2016-05-17 22:12:04 +02:00
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec filter_other({ejabberd_c2s:state(), [stanza()]}, binary(), jid(), stanza())
|
|
|
|
-> {ejabberd_c2s:state(), [stanza()]}.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_other({C2SState, _OutStanzas}, Host, To, Stanza) ->
|
|
|
|
?DEBUG("Won't add stanza for ~s to CSI queue", [jid:to_string(To)]),
|
2016-05-17 19:27:18 +02:00
|
|
|
queue_take(Stanza, Host, C2SState).
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec flush_queue({ejabberd_c2s:state(), [stanza()]}, binary(), jid())
|
2016-08-09 09:56:32 +02:00
|
|
|
-> {ejabberd_c2s:state(), [stanza()]}.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-08-05 23:47:18 +02:00
|
|
|
flush_queue({C2SState, _OutStanzas}, Host, JID) ->
|
|
|
|
?DEBUG("Going to flush CSI queue of ~s", [jid:to_string(JID)]),
|
2016-05-17 19:27:18 +02:00
|
|
|
Queue = get_queue(C2SState),
|
|
|
|
NewState = set_queue([], C2SState),
|
2016-05-18 21:30:38 +02:00
|
|
|
{NewState, get_stanzas(Queue, Host)}.
|
2016-05-17 19:27:18 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec add_stream_feature([stanza()], binary) -> [stanza()].
|
2016-05-17 22:12:04 +02:00
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
add_stream_feature(Features, _Host) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
[#feature_csi{xmlns = <<"urn:xmpp:csi:0">>} | Features].
|
2016-05-18 21:30:38 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Internal functions.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec queue_add(csi_type(), stanza(), binary(), term())
|
|
|
|
-> {stop, {term(), [stanza()]}}.
|
2016-05-17 22:12:04 +02:00
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
queue_add(Type, Stanza, Host, C2SState) ->
|
|
|
|
case get_queue(C2SState) of
|
|
|
|
Queue when length(Queue) >= ?CSI_QUEUE_MAX ->
|
|
|
|
?DEBUG("CSI queue too large, going to flush it", []),
|
|
|
|
NewState = set_queue([], C2SState),
|
|
|
|
{stop, {NewState, get_stanzas(Queue, Host) ++ [Stanza]}};
|
|
|
|
Queue ->
|
|
|
|
?DEBUG("Adding stanza to CSI queue", []),
|
2016-07-18 14:01:32 +02:00
|
|
|
From = xmpp:get_from(Stanza),
|
|
|
|
Key = {jid:tolower(From), Type},
|
2016-05-17 19:27:18 +02:00
|
|
|
Entry = {Key, p1_time_compat:timestamp(), Stanza},
|
|
|
|
NewQueue = lists:keystore(Key, 1, Queue, Entry),
|
|
|
|
NewState = set_queue(NewQueue, C2SState),
|
|
|
|
{stop, {NewState, []}}
|
|
|
|
end.
|
|
|
|
|
2016-11-12 11:27:15 +01:00
|
|
|
-spec queue_take(stanza(), binary(), term()) -> {term(), [stanza()]}.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
queue_take(Stanza, Host, C2SState) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
From = xmpp:get_from(Stanza),
|
|
|
|
{LUser, LServer, _LResource} = jid:tolower(From),
|
2016-05-17 19:27:18 +02:00
|
|
|
{Selected, Rest} = lists:partition(
|
|
|
|
fun({{{U, S, _R}, _Type}, _Time, _Stanza}) ->
|
|
|
|
U == LUser andalso S == LServer
|
|
|
|
end, get_queue(C2SState)),
|
|
|
|
NewState = set_queue(Rest, C2SState),
|
2016-08-06 13:36:27 +02:00
|
|
|
{NewState, get_stanzas(Selected, Host) ++ [Stanza]}.
|
2016-05-17 19:27:18 +02:00
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
-spec set_queue(csi_queue(), term()) -> term().
|
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
set_queue(Queue, C2SState) ->
|
|
|
|
ejabberd_c2s:set_aux_field(csi_queue, Queue, C2SState).
|
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
-spec get_queue(term()) -> csi_queue().
|
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
get_queue(C2SState) ->
|
|
|
|
case ejabberd_c2s:get_aux_field(csi_queue, C2SState) of
|
|
|
|
{ok, Queue} ->
|
|
|
|
Queue;
|
|
|
|
error ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_stanzas(csi_queue(), binary()) -> [stanza()].
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
get_stanzas(Queue, Host) ->
|
|
|
|
lists:map(fun({_Key, Time, Stanza}) ->
|
2016-09-13 11:30:05 +02:00
|
|
|
xmpp_util:add_delay_info(Stanza, jid:make(Host), Time,
|
2016-07-18 14:01:32 +02:00
|
|
|
<<"Client Inactive">>)
|
2016-05-17 19:27:18 +02:00
|
|
|
end, Queue).
|
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.
|