plugins/uptime.lua

Mon, 14 Mar 2011 14:21:30 +0000

author
Marco Cirillo <maranda@lightwitch.org>
date
Mon, 14 Mar 2011 14:21:30 +0000
changeset 194
651c696e0b21
child 250
a5ac643a7fd6
permissions
-rw-r--r--

Added uptime plugin, included also an entry for it into the squishy file.

194
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 local xmlns_last = "jabber:iq:last";
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
2
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3 local function set_uptime(self, uptime_info)
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4 self.starttime = uptime_info.starttime;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5 end
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
7 function verse.plugins.uptime(stream)
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
8 stream.uptime = { set = set_uptime };
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
9 stream:hook("iq/"..xmlns_last, function (stanza)
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10 if stanza.attr.type ~= "get" then return; end
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
11 local reply = verse.reply(stanza)
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
12 :tag("query", { seconds = tostring(os.difftime(os.time(), stream.uptime.starttime)), xmlns = xmlns_last });
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
13 stream:send(reply);
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14 return true;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15 end);
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
17 function stream:query_uptime(target_jid, callback)
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
18 callback = callback or function (uptime) return stream:event("uptime/response", uptime); end
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
19 stream:send_iq(verse.iq({ type = "get", to = target_jid })
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
20 :tag("query", { xmlns = xmlns_last }),
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
21 function (reply)
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
22 local query = reply:get_child("query", xmlns_last);
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
23 if reply.attr.type == "result" then
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
24 local seconds = query.attr.seconds;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
25 callback({
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
26 seconds = seconds or nil;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
27 });
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
28 else
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 local type, condition, text = reply:get_error();
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 callback({
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 error = true;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 condition = condition;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
33 text = text;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 type = type;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35 });
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36 end
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37 end);
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 end
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
39 return true;
651c696e0b21 Added uptime plugin, included also an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 end

mercurial