plugins/mod_time.lua

1
-- Prosody IM
2
-- Copyright (C) 2008-2010 Matthew Wild
3
-- Copyright (C) 2008-2010 Waqas Hussain
4
-- 
5
-- This project is MIT/X11 licensed. Please see the
6
-- COPYING file in the source package for more information.
7
--
8
 
9
local st = require "util.stanza";
10
local datetime = require "util.datetime".datetime;
11
local legacy = require "util.datetime".legacy;
12
 
13
-- XEP-0202: Entity Time
14
 
15
module:add_feature("urn:xmpp:time");
16
 
17
local function time_handler(event)
18
	local origin, stanza = event.origin, event.stanza;
19
	if stanza.attr.type == "get" then
20
		origin.send(st.reply(stanza):tag("time", {xmlns="urn:xmpp:time"})
21
			:tag("tzo"):text("+00:00"):up() -- TODO get the timezone in a platform independent fashion
22
			:tag("utc"):text(datetime()));
23
		return true;
24
	end
25
end
26
 
27
module:hook("iq/bare/urn:xmpp:time:time", time_handler);
28
module:hook("iq/host/urn:xmpp:time:time", time_handler);
29
 
30
-- XEP-0090: Entity Time (deprecated)
31
 
32
module:add_feature("jabber:iq:time");
33
 
34
local function legacy_time_handler(event)
35
	local origin, stanza = event.origin, event.stanza;
36
	if stanza.attr.type == "get" then
37
		origin.send(st.reply(stanza):tag("query", {xmlns="jabber:iq:time"})
38
			:tag("utc"):text(legacy()));
39
		return true;
40
	end
41
end
42
 
43
module:hook("iq/bare/jabber:iq:time:query", legacy_time_handler);
44
module:hook("iq/host/jabber:iq:time:query", legacy_time_handler);