Comparison

plugins/time.lua @ 480:a48a4723aa2a

Add time query plugin
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Mar 2023 12:30:49 +0000
child 490:6b2f31da9610
comparison
equal deleted inserted replaced
479:2bb12fb6fcbd 480:a48a4723aa2a
1 local verse = require "verse";
2 local dt = require "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