plugins/uptime.lua

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

mercurial