| 1 | local st = require "prosody.util.stanza"; |
| 2 | |
| 3 | function riddim.plugins.uptime(bot) |
| 4 | bot.stream:add_plugin("uptime"); |
| 5 | bot.stream.uptime:set{ |
| 6 | starttime = os.time(); |
| 7 | }; |
| 8 | |
| 9 | bot:hook("commands/uptime", function (command) |
| 10 | local who, param = bot.stream.jid, command.param; |
| 11 | local reply_prefix = "I have been running for "; |
| 12 | if param then |
| 13 | if command.room and command.room.occupants[param] then |
| 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 |
| 17 | who = command.room.occupants[param:gsub("%s$", "")].jid; |
| 18 | reply_prefix = param.." has been idle for "; |
| 19 | else |
| 20 | who = param; |
| 21 | reply_prefix = param.." has been running for "; |
| 22 | end |
| 23 | end |
| 24 | |
| 25 | bot.stream:query_uptime(who, function (reply) |
| 26 | if not reply.error then |
| 27 | command:reply(reply_prefix..convert_time(reply.seconds)); |
| 28 | else |
| 29 | local type, condition, text = reply.type, reply.condition, reply.text; |
| 30 | local r = "There was an error requesting "..param.."'s uptime"; |
| 31 | if condition == "service-unavailable" then |
| 32 | r = param.." doesn't reply to uptime/last activity requests"; |
| 33 | elseif condition == "feature-not-implemented" then |
| 34 | r = param.." doesn't support feature requests"; |
| 35 | elseif condition == "remote-server-not-found" then |
| 36 | r = param.." can't be reached via XMPP"; |
| 37 | elseif condition and not text then |
| 38 | r = r..": "..condition; |
| 39 | end |
| 40 | if text then |
| 41 | r = r .. " ("..text..")"; |
| 42 | end |
| 43 | command:reply(r); |
| 44 | end |
| 45 | end); |
| 46 | return true; |
| 47 | end); |
| 48 | |
| 49 | function convert_time(value) |
| 50 | local t = value; |
| 51 | local seconds = t%60; |
| 52 | t = (t - seconds)/60; |
| 53 | local minutes = t%60; |
| 54 | t = (t - minutes)/60; |
| 55 | local hours = t%24; |
| 56 | t = (t - hours)/24; |
| 57 | local days = t; |
| 58 | return string.format("%d day%s, %d hour%s and %d minute%s", |
| 59 | days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "", |
| 60 | minutes, (minutes ~= 1 and "s") or ""); |
| 61 | end |
| 62 | end |