./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 start(HostType, Opts) ->
28 1 IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
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
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 160 #section{
43 items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc()}}.
44
45 process_local_iq(Acc, _From, _To, #iq{type = set, sub_el = SubEl} = IQ, _Extra) ->
46
:-(
{Acc, IQ#iq{type = error, sub_el = [SubEl, mongoose_xmpp_errors:not_allowed()]}};
47
48 process_local_iq(Acc, _From, _To, #iq{type = get} = IQ, _Extra) ->
49 1 {UTC, TZODiff} = calculate_time(),
50 1 R = IQ#iq{type = result,
51 sub_el =
52 [#xmlel{name = <<"time">>,
53 attrs = [{<<"xmlns">>, ?NS_TIME}],
54 children =
55 [#xmlel{name = <<"tzo">>, attrs = [],
56 children = [#xmlcdata{content = list_to_binary(TZODiff)}]},
57 #xmlel{name = <<"utc">>, attrs = [],
58 children =
59 [#xmlcdata{content = list_to_binary(UTC)}]}]}]},
60 1 {Acc, R}.
61
62 %% Internals
63 calculate_time() ->
64 1 SystemTime = erlang:system_time(second),
65 1 UTCString = calendar:system_time_to_rfc3339(SystemTime, [{offset, "Z"}]),
66 1 LocalString = calendar:system_time_to_rfc3339(SystemTime),
67 1 DateTimeLength = length(UTCString) - 1,
68 1 {UTCString, string:slice(LocalString, DateTimeLength)}.
Line Hits Source