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").
|
|
|
|
-include("jlib.hrl").
|
|
|
|
|
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-08-05 23:47:18 +02:00
|
|
|
-spec filter_presence({term(), [xmlel()]}, binary(), jid(), xmlel())
|
2016-05-18 21:30:38 +02:00
|
|
|
-> {term(), [xmlel()]} | {stop, {term(), [xmlel()]}}.
|
2014-09-25 18:28:20 +02:00
|
|
|
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_presence({C2SState, _OutStanzas} = Acc, Host, To,
|
2016-05-17 19:27:18 +02:00
|
|
|
#xmlel{name = <<"presence">>, attrs = Attrs} = Stanza) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr(<<"type">>, Attrs) of
|
2014-09-11 17:44:29 +02:00
|
|
|
{value, Type} when Type /= <<"unavailable">> ->
|
2016-05-17 19:27:18 +02:00
|
|
|
Acc;
|
2014-09-11 17:44:29 +02:00
|
|
|
_ ->
|
2016-08-05 23:47:18 +02:00
|
|
|
?DEBUG("Got availability presence stanza for ~s",
|
|
|
|
[jid:to_string(To)]),
|
2016-05-17 19:27:18 +02:00
|
|
|
queue_add(presence, Stanza, Host, C2SState)
|
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-08-05 23:47:18 +02:00
|
|
|
-spec filter_chat_states({term(), [xmlel()]}, binary(), jid(), xmlel())
|
2016-05-18 21:30:38 +02:00
|
|
|
-> {term(), [xmlel()]} | {stop, {term(), [xmlel()]}}.
|
|
|
|
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_chat_states({C2SState, _OutStanzas} = Acc, Host, To,
|
2016-05-17 19:27:18 +02:00
|
|
|
#xmlel{name = <<"message">>} = Stanza) ->
|
2016-06-05 22:36:56 +02:00
|
|
|
case jlib:is_standalone_chat_state(Stanza) of
|
2016-05-17 20:55:45 +02:00
|
|
|
true ->
|
2016-06-05 22:04:38 +02:00
|
|
|
From = fxml:get_tag_attr_s(<<"from">>, Stanza),
|
2016-08-05 23:47:18 +02:00
|
|
|
case {jid:from_string(From), To} of
|
2016-06-03 21:52:11 +02:00
|
|
|
{#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-06-03 21:52:11 +02:00
|
|
|
queue_add(chatstate, Stanza, Host, C2SState)
|
|
|
|
end;
|
2015-11-25 08:58:22 +01:00
|
|
|
false ->
|
2016-05-17 19:27:18 +02:00
|
|
|
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-08-05 23:47:18 +02:00
|
|
|
-spec filter_pep({term(), [xmlel()]}, binary(), jid(), xmlel())
|
2016-05-18 21:30:38 +02:00
|
|
|
-> {term(), [xmlel()]} | {stop, {term(), [xmlel()]}}.
|
|
|
|
|
2016-08-05 23:47:18 +02:00
|
|
|
filter_pep({C2SState, _OutStanzas} = Acc, Host, To,
|
2016-05-17 22:12:04 +02:00
|
|
|
#xmlel{name = <<"message">>} = Stanza) ->
|
2016-06-05 21:48:03 +02:00
|
|
|
case get_pep_node(Stanza) of
|
|
|
|
{value, Node} ->
|
2016-08-05 23:47:18 +02:00
|
|
|
?DEBUG("Got PEP notification for ~s", [jid:to_string(To)]),
|
2016-06-05 21:48:03 +02:00
|
|
|
queue_add({pep, Node}, Stanza, Host, C2SState);
|
2016-05-17 22:12:04 +02:00
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
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-08-05 23:47:18 +02:00
|
|
|
-spec filter_other({term(), [xmlel()]}, binary(), jid(), xmlel())
|
2016-08-06 13:36:27 +02:00
|
|
|
-> {term(), [xmlel()]}.
|
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-08-05 23:47:18 +02:00
|
|
|
-spec flush_queue({term(), [xmlel()]}, binary(), jid()) -> {term(), [xmlel()]}.
|
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-05-18 21:30:38 +02:00
|
|
|
-spec add_stream_feature([xmlel()], binary) -> [xmlel()].
|
2016-05-17 22:12:04 +02:00
|
|
|
|
2016-05-18 21:30:38 +02:00
|
|
|
add_stream_feature(Features, _Host) ->
|
|
|
|
Feature = #xmlel{name = <<"csi">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_CLIENT_STATE}],
|
|
|
|
children = []},
|
|
|
|
[Feature | Features].
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Internal functions.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
|
|
-spec queue_add(csi_type(), xmlel(), binary(), term())
|
|
|
|
-> {stop, {term(), [xmlel()]}}.
|
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", []),
|
|
|
|
From = fxml:get_tag_attr_s(<<"from">>, Stanza),
|
|
|
|
Key = {jid:tolower(jid:from_string(From)), Type},
|
|
|
|
Entry = {Key, p1_time_compat:timestamp(), Stanza},
|
|
|
|
NewQueue = lists:keystore(Key, 1, Queue, Entry),
|
|
|
|
NewState = set_queue(NewQueue, C2SState),
|
|
|
|
{stop, {NewState, []}}
|
|
|
|
end.
|
|
|
|
|
2016-08-06 13:36:27 +02:00
|
|
|
-spec queue_take(xmlel(), binary(), term()) -> {term(), [xmlel()]}.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
queue_take(Stanza, Host, C2SState) ->
|
|
|
|
From = fxml:get_tag_attr_s(<<"from">>, Stanza),
|
|
|
|
{LUser, LServer, _LResource} = jid:tolower(jid:from_string(From)),
|
|
|
|
{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-05-18 21:30:38 +02:00
|
|
|
-spec get_stanzas(csi_queue(), binary()) -> [xmlel()].
|
|
|
|
|
2016-05-17 19:27:18 +02:00
|
|
|
get_stanzas(Queue, Host) ->
|
|
|
|
lists:map(fun({_Key, Time, Stanza}) ->
|
|
|
|
jlib:add_delay_info(Stanza, Host, Time,
|
|
|
|
<<"Client Inactive">>)
|
|
|
|
end, Queue).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-06-05 21:48:03 +02:00
|
|
|
-spec get_pep_node(xmlel()) -> {value, binary()} | false.
|
2016-05-18 21:30:38 +02:00
|
|
|
|
2016-06-05 21:48:03 +02:00
|
|
|
get_pep_node(#xmlel{name = <<"message">>} = Stanza) ->
|
2016-05-18 21:30:38 +02:00
|
|
|
From = fxml:get_tag_attr_s(<<"from">>, Stanza),
|
|
|
|
case jid:from_string(From) of
|
|
|
|
#jid{luser = <<>>} -> % It's not PEP.
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
case fxml:get_subtag_with_xmlns(Stanza, <<"event">>,
|
|
|
|
?NS_PUBSUB_EVENT) of
|
|
|
|
#xmlel{children = Els} ->
|
2016-06-05 21:48:03 +02:00
|
|
|
case fxml:remove_cdata(Els) of
|
|
|
|
[#xmlel{name = <<"items">>, attrs = ItemsAttrs}] ->
|
|
|
|
fxml:get_attr(<<"node">>, ItemsAttrs);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
2016-05-18 21:30:38 +02:00
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|