1 local st = require "util.stanza"; |
1 local st = require "util.stanza"; |
2 |
|
3 local xmlns_last = "jabber:iq:last"; |
|
4 |
2 |
5 function riddim.plugins.uptime(bot) |
3 function riddim.plugins.uptime(bot) |
6 bot.stream:add_plugin("uptime"); |
4 bot.stream:add_plugin("uptime"); |
7 bot.stream.uptime:set{ |
5 bot.stream.uptime:set{ |
8 starttime = os.time(); |
6 starttime = os.time(); |
9 }; |
7 }; |
10 |
8 |
11 bot:hook("commands/uptime", function (command) |
9 bot:hook("commands/uptime", function (command) |
12 local who, param = bot.stream.jid, command.param; |
10 local who, param = bot.stream.jid, command.param; |
|
11 local reply_prefix = "I have been running for "; |
13 if param then |
12 if param then |
14 if command.room and command.room.occupants[param] then |
13 if command.room and command.room.occupants[param] then |
15 who = command.room.occupants[param].jid; |
14 who = command.room.occupants[param].jid; |
|
15 reply_prefix = param.." has been idle for "; |
16 elseif command.room and command.room.occupants[param:gsub("%s$", "")] then |
16 elseif command.room and command.room.occupants[param:gsub("%s$", "")] then |
17 who = command.room.occupants[param:gsub("%s$", "")].jid; |
17 who = command.room.occupants[param:gsub("%s$", "")].jid; |
|
18 reply_prefix = param.." has been idle for "; |
18 else |
19 else |
19 who = param; |
20 who = param; |
|
21 reply_prefix = param.." has been running for "; |
20 end |
22 end |
21 end |
23 end |
22 |
24 |
23 bot.stream:query_uptime(who, function (reply) |
25 bot.stream:query_uptime(who, function (reply) |
24 if not reply.error then |
26 if not reply.error then |
25 local saywho = (who == command.sender.jid and "You are") or (param and param.." is" or "I am"); |
27 command:reply(reply_prefix..convert_time(reply.seconds)); |
26 command:reply(saywho..convert_time(reply.seconds)); |
|
27 else |
28 else |
28 local type, condition, text = reply.type, reply.condition, reply.text; |
29 local type, condition, text = reply.type, reply.condition, reply.text; |
29 local r = "There was an error requesting "..param.."'s version"; |
30 local r = "There was an error requesting "..param.."'s version"; |
30 if condition == "service-unavailable" then |
31 if condition == "service-unavailable" then |
31 r = param.." doesn't reply to uptime/last activity requests"; |
32 r = param.." doesn't reply to uptime/last activity requests"; |
52 local minutes = t%60; |
53 local minutes = t%60; |
53 t = (t - minutes)/60; |
54 t = (t - minutes)/60; |
54 local hours = t%24; |
55 local hours = t%24; |
55 t = (t - hours)/24; |
56 t = (t - hours)/24; |
56 local days = t; |
57 local days = t; |
57 return string.format(" up from %d day%s, %d hour%s and %d minute%s", |
58 return string.format("%d day%s, %d hour%s and %d minute%s", |
58 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "", |
59 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "", |
59 minutes, (minutes ~= 1 and "s") or ""); |
60 minutes, (minutes ~= 1 and "s") or ""); |
60 end |
61 end |
61 end |
62 end |