plugins/time.lua

1
local verse = require "verse";
2
local dt = require "prosody.util.datetime";
3
 
4
local xmlns_time = "urn:xmpp:time";
5
 
6
function verse.plugins.time(stream)
7
	function stream:query_time(target_jid, callback)
8
		callback = callback or function (time) return self:event("time/response", time); end
9
		self:send_iq(verse.iq({ type = "get", to = target_jid })
10
			:tag("time", { xmlns = xmlns_time }),
11
			function (reply)
12
				if reply.attr.type == "result" then
13
					local query = reply:get_child("time", xmlns_time);
14
					local resp = {
15
						tzo = query:get_child_text("tzo");
16
						utc = query:get_child_text("utc");
17
					};
18
					if resp.utc then
19
						resp.timestamp = dt.parse(resp.utc);
20
					end
21
					callback(resp);
22
				else
23
					local type, condition, text = reply:get_error();
24
					callback({
25
						error = true;
26
						condition = condition;
27
						text = text;
28
						type = type;
29
					});
30
				end
31
			end);
32
	end
33
	return true;
34
end