plugins/uptime.lua

changeset 194
651c696e0b21
child 250
a5ac643a7fd6
equal deleted inserted replaced
193:fa6e1e65cb3c 194:651c696e0b21
1 local xmlns_last = "jabber:iq:last";
2
3 local function set_uptime(self, uptime_info)
4 self.starttime = uptime_info.starttime;
5 end
6
7 function verse.plugins.uptime(stream)
8 stream.uptime = { set = set_uptime };
9 stream:hook("iq/"..xmlns_last, function (stanza)
10 if stanza.attr.type ~= "get" then return; end
11 local reply = verse.reply(stanza)
12 :tag("query", { seconds = tostring(os.difftime(os.time(), stream.uptime.starttime)), xmlns = xmlns_last });
13 stream:send(reply);
14 return true;
15 end);
16
17 function stream:query_uptime(target_jid, callback)
18 callback = callback or function (uptime) return stream:event("uptime/response", uptime); end
19 stream:send_iq(verse.iq({ type = "get", to = target_jid })
20 :tag("query", { xmlns = xmlns_last }),
21 function (reply)
22 local query = reply:get_child("query", xmlns_last);
23 if reply.attr.type == "result" then
24 local seconds = query.attr.seconds;
25 callback({
26 seconds = seconds or nil;
27 });
28 else
29 local type, condition, text = reply:get_error();
30 callback({
31 error = true;
32 condition = condition;
33 text = text;
34 type = type;
35 });
36 end
37 end);
38 end
39 return true;
40 end

mercurial