./ct_report/coverage/mod_time.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% @author Ludwik Bukowski <ludwik.bukowski@erlang-solutions.com>
3 %%% @copyright (C) 2015, Ludwik Bukowski
4 %%% @doc XEP-0202: Entity Time
5 %%% @end
6 %%%-------------------------------------------------------------------
7 -module(mod_time).
8 -author('ludwik.bukowski@erlang-solutions.com').
9
10 -behaviour(gen_mod).
11 -behaviour(mongoose_module_metrics).
12
13 -export([start/2]).
14 -export([stop/1]).
15 -export([supported_features/0]).
16 -export([config_spec/0]).
17 -export([process_local_iq/5]).
18
19 -ignore_xref([process_local_iq/5]).
20
21 -include("jlib.hrl").
22 -include("mongoose_config_spec.hrl").
23
24 -xep([{xep, 202}, {version, "2.0"}]).
25 -xep([{xep, 82}, {version, "1.1"}]).
26
27 -spec start(HostType :: mongooseim:host_type(), gen_mod:module_opts()) -> ok | {error, atom()}.
28 start(HostType, #{iqdisc := IQDisc}) ->
29 1 gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_TIME, ejabberd_local,
30 fun ?MODULE:process_local_iq/5, #{}, IQDisc).
31
32 -spec stop(HostType :: mongooseim:host_type()) -> ok | {error, not_registered}.
33 stop(HostType) ->
34 1 gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_TIME, ejabberd_local).
35
36 -spec supported_features() -> [atom()].
37 supported_features() ->
38
:-(
[dynamic_domains].
39
40 -spec config_spec() -> mongoose_config_spec:config_section().
41 config_spec() ->
42 164 #section{
43 items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc()},
44 defaults = #{<<"iqdisc">> => one_queue},
45 format_items = map
46 }.
47
48 process_local_iq(Acc, _From, _To, #iq{type = set, sub_el = SubEl} = IQ, _Extra) ->
49
:-(
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:not_allowed()]}};
50
51 process_local_iq(Acc, _From, _To, #iq{type = get} = IQ, _Extra) ->
52 1 {UTC, TZODiff} = calculate_time(),
53 1 R = IQ#iq{type = result,
54 sub_el =
55 [#xmlel{name = <<"time">>,
56 attrs = [{<<"xmlns">>, ?NS_TIME}],
57 children =
58 [#xmlel{name = <<"tzo">>, attrs = [],
59 children = [#xmlcdata{content = list_to_binary(TZODiff)}]},
60 #xmlel{name = <<"utc">>, attrs = [],
61 children =
62 [#xmlcdata{content = list_to_binary(UTC)}]}]}]},
63 1 {Acc, R}.
64
65 %% Internals
66 calculate_time() ->
67 1 SystemTime = erlang:system_time(second),
68 1 UTCString = calendar:system_time_to_rfc3339(SystemTime, [{offset, "Z"}]),
69 1 LocalString = calendar:system_time_to_rfc3339(SystemTime),
70 1 DateTimeLength = length(UTCString) - 1,
71 1 {UTCString, string:slice(LocalString, DateTimeLength)}.
Line Hits Source