plugins/uptime.lua

Mon, 14 Mar 2011 14:18:16 +0000

author
Marco Cirillo <maranda@lightwitch.org>
date
Mon, 14 Mar 2011 14:18:16 +0000
changeset 72
a1c73a366ee0
child 73
b9d319376f6d
permissions
-rw-r--r--

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

72
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 local st = require "util.stanza";
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
2
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3 local xmlns_last = "jabber:iq:last";
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5 function riddim.plugins.uptime(bot)
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6 bot.stream:add_plugin("uptime");
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
7 bot.stream.uptime:set{
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
8 starttime = os.time();
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
9 };
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
11 bot:hook("commands/uptime", function (command)
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
12 local who, param = bot.stream.jid, command.param;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
13 if param then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14 if command.room and command.room.occupants[param] then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15 who = command.room.occupants[param].jid;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16 elseif command.room and command.room.occupants[param:gsub("%s$", "")] then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
17 who = command.room.occupants[param:gsub("%s$", "")].jid;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
18 else
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
19 who = param;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
20 end
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
21 end
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
22
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
23 bot.stream:query_uptime(who, function (reply)
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
24 if not reply.error then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
25 local saywho = (who == command.sender.jid and "You are") or (param and param.." is" or "I am");
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
26 command:reply(saywho..convert_time(reply.seconds));
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
27 else
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
28 local type, condition, text = reply.type, reply.condition, reply.text;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 local r = "There was an error requesting "..param.."'s version";
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 if condition == "service-unavailable" then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 r = param.." doesn't reply to uptime/last activity requests";
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 elseif condition == "feature-not-implemented" then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
33 r = param.." doesn't support feature requests";
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 elseif condition == "remote-server-not-found" then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35 r = param.." can't be reached via XMPP";
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36 elseif condition and not text then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37 r = r..": "..condition;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 end
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
39 if text then
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 r = r .. " ("..text..")";
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
41 end
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
42 command:reply(r);
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
43 end
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44 end);
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
45 return true;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
46 end);
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
47
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
48 function convert_time(value)
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
49 local t = value;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
50 local seconds = t%60;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
51 t = (t - seconds)/60;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
52 local minutes = t%60;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
53 t = (t - minutes)/60;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
54 local hours = t%24;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
55 t = (t - hours)/24;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
56 local days = t;
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
57 return string.format(" up from %d day%s, %d hour%s and %d minute%s",
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
58 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "",
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
59 minutes, (minutes ~= 1 and "s") or "");
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
60 end
a1c73a366ee0 Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
61 end

mercurial