25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-10-31 15:21:38 +01:00

mod_client_state: Add function specifications

Add function specifications and apply cosmetic changes to
mod_client_state.
This commit is contained in:
Holger Weiss 2016-05-18 21:30:38 +02:00
parent 8f72c27b88
commit 420ae65590

View File

@ -30,9 +30,12 @@
-behavior(gen_mod).
-export([start/2, stop/1, add_stream_feature/2,
filter_presence/3, filter_chat_states/3, filter_pep/3, filter_other/3,
flush_queue/2, mod_opt_type/1]).
%% gen_mod callbacks.
-export([start/2, stop/1, mod_opt_type/1]).
%% ejabberd_hooks callbacks.
-export([filter_presence/3, filter_chat_states/3, filter_pep/3, filter_other/3,
flush_queue/2, add_stream_feature/2]).
-include("ejabberd.hrl").
-include("logger.hrl").
@ -40,6 +43,17 @@
-define(CSI_QUEUE_MAX, 100).
-type csi_type() :: presence | chatstate | {pep, binary(), binary()}.
-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.
start(Host, Opts) ->
QueuePresence =
gen_mod:get_opt(queue_presence, Opts,
@ -78,6 +92,8 @@ start(Host, Opts) ->
true -> ok
end.
-spec stop(binary()) -> ok.
stop(Host) ->
QueuePresence =
gen_mod:get_module_opt(Host, ?MODULE, queue_presence,
@ -116,11 +132,22 @@ stop(Host) ->
true -> ok
end.
add_stream_feature(Features, _Host) ->
Feature = #xmlel{name = <<"csi">>,
attrs = [{<<"xmlns">>, ?NS_CLIENT_STATE}],
children = []},
[Feature | Features].
-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].
%%--------------------------------------------------------------------
%% ejabberd_hooks callbacks.
%%--------------------------------------------------------------------
-spec filter_presence({term(), [xmlel()]}, binary(), xmlel())
-> {term(), [xmlel()]} | {stop, {term(), [xmlel()]}}.
filter_presence({C2SState, _OutStanzas} = Acc, Host,
#xmlel{name = <<"presence">>, attrs = Attrs} = Stanza) ->
@ -133,6 +160,9 @@ filter_presence({C2SState, _OutStanzas} = Acc, Host,
end;
filter_presence(Acc, _Host, _Stanza) -> Acc.
-spec filter_chat_states({term(), [xmlel()]}, binary(), xmlel())
-> {term(), [xmlel()]} | {stop, {term(), [xmlel()]}}.
filter_chat_states({C2SState, _OutStanzas} = Acc, Host,
#xmlel{name = <<"message">>} = Stanza) ->
case jlib:is_standalone_chat_state(Stanza) of
@ -144,6 +174,9 @@ filter_chat_states({C2SState, _OutStanzas} = Acc, Host,
end;
filter_chat_states(Acc, _Host, _Stanza) -> Acc.
-spec filter_pep({term(), [xmlel()]}, binary(), xmlel())
-> {term(), [xmlel()]} | {stop, {term(), [xmlel()]}}.
filter_pep({C2SState, _OutStanzas} = Acc, Host,
#xmlel{name = <<"message">>} = Stanza) ->
case find_pep(Stanza) of
@ -155,51 +188,35 @@ filter_pep({C2SState, _OutStanzas} = Acc, Host,
end;
filter_pep(Acc, _Host, _Stanza) -> Acc.
-spec filter_other({term(), [xmlel()]}, binary(), xmlel())
-> {stop, {term(), [xmlel()]}}.
filter_other({C2SState, _OutStanzas}, Host, Stanza) ->
?DEBUG("Won't add stanza to CSI queue", []),
queue_take(Stanza, Host, C2SState).
-spec flush_queue({term(), [xmlel()]}, binary()) -> {term(), [xmlel()]}.
flush_queue({C2SState, _OutStanzas}, Host) ->
?DEBUG("Going to flush CSI queue", []),
Queue = get_queue(C2SState),
NewState = set_queue([], C2SState),
{stop, {NewState, get_stanzas(Queue, Host)}}.
{NewState, get_stanzas(Queue, Host)}.
find_pep(#xmlel{name = <<"message">>} = Stanza) ->
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} ->
get_pep_node_and_xmlns(fxml:remove_cdata(Els));
false ->
false
end
end.
-spec add_stream_feature([xmlel()], binary) -> [xmlel()].
get_pep_node_and_xmlns([#xmlel{name = <<"items">>, attrs = ItemsAttrs,
children = Item}]) ->
case {fxml:get_attr(<<"node">>, ItemsAttrs), fxml:remove_cdata(Item)} of
{{value, Node}, [#xmlel{name = <<"item">>, children = Payload}]} ->
case fxml:remove_cdata(Payload) of
[#xmlel{attrs = PayloadAttrs}] ->
case fxml:get_attr(<<"xmlns">>, PayloadAttrs) of
{value, XMLNS} ->
{value, {Node, XMLNS}};
false ->
false
end;
_ ->
false
end;
_ ->
false
end;
get_pep_node_and_xmlns(_) ->
false.
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()]}}.
queue_add(Type, Stanza, Host, C2SState) ->
case get_queue(C2SState) of
@ -217,6 +234,8 @@ queue_add(Type, Stanza, Host, C2SState) ->
{stop, {NewState, []}}
end.
-spec queue_take(xmlel(), binary(), term()) -> {stop, {term(), [xmlel()]}}.
queue_take(Stanza, Host, C2SState) ->
From = fxml:get_tag_attr_s(<<"from">>, Stanza),
{LUser, LServer, _LResource} = jid:tolower(jid:from_string(From)),
@ -227,9 +246,13 @@ queue_take(Stanza, Host, C2SState) ->
NewState = set_queue(Rest, C2SState),
{stop, {NewState, get_stanzas(Selected, Host) ++ [Stanza]}}.
-spec set_queue(csi_queue(), term()) -> term().
set_queue(Queue, C2SState) ->
ejabberd_c2s:set_aux_field(csi_queue, Queue, C2SState).
-spec get_queue(term()) -> csi_queue().
get_queue(C2SState) ->
case ejabberd_c2s:get_aux_field(csi_queue, C2SState) of
{ok, Queue} ->
@ -238,16 +261,50 @@ get_queue(C2SState) ->
[]
end.
-spec get_stanzas(csi_queue(), binary()) -> [xmlel()].
get_stanzas(Queue, Host) ->
lists:map(fun({_Key, Time, Stanza}) ->
jlib:add_delay_info(Stanza, Host, Time,
<<"Client Inactive">>)
end, Queue).
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].
-spec find_pep(xmlel()) -> {pep, binary(), binary()} | false.
find_pep(#xmlel{name = <<"message">>} = Stanza) ->
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} ->
get_pep_node_and_xmlns(fxml:remove_cdata(Els));
false ->
false
end
end.
-spec get_pep_node_and_xmlns([xmlel()]) -> {pep, binary(), binary()} | false.
get_pep_node_and_xmlns([#xmlel{name = <<"items">>, attrs = ItemsAttrs,
children = Item}]) ->
case {fxml:get_attr(<<"node">>, ItemsAttrs), fxml:remove_cdata(Item)} of
{{value, Node}, [#xmlel{name = <<"item">>, children = Payload}]} ->
case fxml:remove_cdata(Payload) of
[#xmlel{attrs = PayloadAttrs}] ->
case fxml:get_attr(<<"xmlns">>, PayloadAttrs) of
{value, XMLNS} ->
{value, {pep, Node, XMLNS}};
false ->
false
end;
_ ->
false
end;
_ ->
false
end;
get_pep_node_and_xmlns(_) ->
false.