Mon, 06 Dec 2021 11:26:37 +0000
rtbl_admin: Fix reason string (abuse is defined to be the most generic)
Would be nice to support reasons in the future (and text annotations)
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 | 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
|
4 | 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
|
5 | 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
|
6 | 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
|
7 | }; |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
8 | |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
9 | 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
|
10 | local who, param = bot.stream.jid, command.param; |
73
b9d319376f6d
uptime: Refactor reply text generation
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
11 | local reply_prefix = "I have been running for "; |
72
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
12 | 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
|
13 | 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
|
14 | who = command.room.occupants[param].jid; |
73
b9d319376f6d
uptime: Refactor reply text generation
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
15 | reply_prefix = param.." has been idle for "; |
72
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; |
73
b9d319376f6d
uptime: Refactor reply text generation
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
18 | reply_prefix = param.." has been idle for "; |
72
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
19 | else |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
20 | who = param; |
73
b9d319376f6d
uptime: Refactor reply text generation
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
21 | reply_prefix = param.." has been running for "; |
72
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
22 | end |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
23 | end |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
24 | |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
25 | 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
|
26 | if not reply.error then |
73
b9d319376f6d
uptime: Refactor reply text generation
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
27 | command:reply(reply_prefix..convert_time(reply.seconds)); |
72
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
28 | else |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
29 | local type, condition, text = reply.type, reply.condition, reply.text; |
106 | 30 | local r = "There was an error requesting "..param.."'s uptime"; |
72
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
31 | 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
|
32 | 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
|
33 | 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
|
34 | 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
|
35 | 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
|
36 | 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
|
37 | 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
|
38 | 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
|
39 | end |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
40 | 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
|
41 | 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
|
42 | end |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
43 | 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
|
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 | end); |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
46 | return true; |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
47 | end); |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
48 | |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
49 | 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
|
50 | 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
|
51 | 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
|
52 | 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
|
53 | 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
|
54 | 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
|
55 | 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
|
56 | 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
|
57 | local days = t; |
73
b9d319376f6d
uptime: Refactor reply text generation
Matthew Wild <mwild1@gmail.com>
parents:
72
diff
changeset
|
58 | return string.format("%d day%s, %d hour%s and %d minute%s", |
72
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
59 | 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
|
60 | 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
|
61 | end |
a1c73a366ee0
Added uptime plugin, also included an entry for it into the squishy file.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
62 | end |