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