-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatcmds.lua
279 lines (258 loc) · 8.43 KB
/
chatcmds.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
-- Thanks xban2!
local function parse_time(t) --> secs
local unit_to_secs = {
s = 1, m = 60, h = 3600,
D = 86400, W = 604800, M = 2592000, Y = 31104000,
[""] = 1,
}
local secs = 0
for num, unit in t:gmatch("(%d+)([smhDWMY]?)") do
secs = secs + (tonumber(num) * (unit_to_secs[unit] or 1))
end
return secs
end
local commands_descriptors = {}
commands_descriptors.help = {
description = "print help about a subcommand or list all subcommands",
usage = "[<subcommand>]",
params = 0,
func = function(name, token, cmd)
local function get_string_or_func(str_or_func)
if type(str_or_func) == "string" then
return str_or_func
else
return str_or_func(name)
end
end
if not cmd then
local str = "Available subcommands:\n"
for cmd, dsc in pairs(commands_descriptors) do
str = str .. " " .. cmd .. " - " .. dsc.description .. "\n"
end
str = str .. "Type /umabis help <subcommand> to get help about a subcommand."
return true, str
end
local descriptor = commands_descriptors[cmd]
if not descriptor then
return false, "Subcommand " .. cmd .. " does not exist. Type /umabis help for a list of available subcommands."
end
if descriptor.privs then
for _, priv in ipairs(descriptor.privs) do
if not minetest.check_player_privs(name, priv) then
return false, "You don't have permission to run this command. Missing privilege: "..priv
end
end
end
if descriptor.additional_info then
return true, "/umabis " .. cmd .. " " .. descriptor.usage .. "\n" ..
descriptor.description .. "\n" ..
get_string_or_func(descriptor.additional_info)
else
return true, "/umabis " .. cmd .. " " .. descriptor.usage .. "\n" ..
descriptor.description
end
end
}
commands_descriptors.reload = {
description = "reload Umabis",
usage = "",
additional_info = "using this command will apply any change made in a file of the mod, except in init.lua",
privs = {"ban"},
params = 0,
func = function(name, token)
if umabis.reload() then
return true, "Successfully reloaded Umabis."
else
return false, minetest.colorize("#FF0000", "Error occured while reloading. See debug.txt for more details. Umabis is currently is a very inconsistent state, please reload again as soon as possible.")
end
end
}
commands_descriptors.blacklist = {
description = "globally blacklist a player",
usage = "<nick> <reason> <category> [<time>]",
additional_info = function()
local str = "Format of <time>:\n"..
"* 1s or 1 - one second\n"..
"* 1m - one minute\n"..
"* 1h - one hour\n"..
"* 1D - one day\n"..
"* 1W - one week\n"..
"* 1M - one month (30 days)\n"..
"* 1Y - one year (360 days)\n"..
"Values can be combined. For example \"1D3h3m7s\" will blacklist for 1 day, 3 hours, 3 minutes, and 7 seconds.\n\n"..
"Available categories are:\n"
for category, description in pairs(umabis.serverapi.params.available_blacklist_categories) do
str = str .. "* " .. category .. " - " .. description .. "\n"
end
return str
end,
params = 3,
func = function(name, token, blacklisted_name, reason, category, time)
local ok, err = umabis.serverapi.blacklist_user(name, token, blacklisted_name, reason, category, time and parse_time(time))
if not ok then
return false, minetest.colorize("#FF0000", err)
end
return true, "Blacklisted "..blacklisted_name
end
}
commands_descriptors.unblacklist = {
description = "globally unblacklist a player",
usage = "<nick>",
params = 1,
func = function(name, token, blacklisted_name)
local ok, err = umabis.serverapi.unblacklist_user(name, token, blacklisted_name)
if not ok then
return false, minetest.colorize("#FF0000", err)
end
return true, "Unblacklisted "..blacklisted_name
end
}
commands_descriptors.whitelist = {
description = "globally whitelist a player",
usage = "<nick>",
params = 1,
func = function(name, token, whitelisted_name)
local ok, err = umabis.serverapi.whitelist_user(name, token, whitelisted_name)
if not ok then
return false, minetest.colorize("#FF0000", err)
end
return true, "Whitelisted "..whitelisted_name
end
}
commands_descriptors.unwhitelist = {
description = "globally unwhitelist a player",
usage = "<nick>",
params = 1,
func = function(name, token, whitelisted_name)
local ok, err = umabis.serverapi.unwhitelist_user(name, token, whitelisted_name)
if not ok then
return false, minetest.colorize("#FF0000", err)
end
return true, "Unwhitelisted "..whitelisted_name
end
}
if umabis.settings:get_bool("enable_local_ban") then
commands_descriptors.ban = {
description = "locally ban a player",
usage = "<reason> <nicks and/or IP addresses...> [--time=<time>] [--drastic]",
params = 2,
additional_info = "Format of <time>:\n"..
"* 1s or 1 - one second\n"..
"* 1m - one minute\n"..
"* 1h - one hour\n"..
"* 1D - one day\n"..
"* 1W - one week\n"..
"* 1M - one month (30 days)\n"..
"* 1Y - one year (360 days)\n"..
"Values can be combined. For example \"1D3h3m7s\" will blacklist for 1 day, 3 hours, 3 minutes, and 7 seconds.",
privs = {"ban"},
func = function(name, token, ...)
local params = {...}
local drastic = nil
if params[#params] == "--drastic" then
table.remove(params, #params)
drastic = true
end
local time = nil
local time_param = params[#params]
if time_param:sub(1, 7) == "--time=" then
time = parse_time(time_param:sub(8))
table.remove(params, #params)
end
local reason = params[1]
table.remove(params, 1)
local nicks = {}
local ips = {}
for _, param in ipairs(params) do
if string.match(param, "%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?") then -- If param in an IP address
ips[param] = true
else
nicks[param] = true
end
end
return umabis.ban.ban_players(name, nicks, ips, reason, drastic, time)
end
}
commands_descriptors.unban = {
description = "locally unban a player",
usage = "<nick or IP address>",
params = 1,
privs = {"ban"},
func = function(name, token, nick_or_ip)
return umabis.ban.unban_player(nick_or_ip)
end
}
commands_descriptors.get_ban = {
description = "display the ban entry of a player",
usage = "<nick or IP address>",
params = 1,
privs = (function()
if umabis.settings:get_bool(ban_entries_visible) then
return {}
else
return {"ban"}
end
end)(),
func = function(name, token, nick_or_ip)
local entries = umabis.ban.get_entry(nick_or_ip)
if not entries then
return false, "Nick or IP " .. nick_or_ip .. " is not banned."
end
if #entries > 1 then
local str = "Warning: found more than one (" .. #entries .. ") entries! This is abnormal."
for _, entry in ipairs(entries) do
str = str .. "\n---\n" .. umabis.ban.format_entry(nick_or_ip, entry)
end
return true, str
else
return true, umabis.ban.format_entry(nick_or_ip, entries[1])
end
end
}
end
minetest.register_chatcommand("umabis", {
params = "<subcommand> [<subcommand parameters...>]",
description = "Umabis chatcommand interface",
privs = {},
func = function(name, paramstr)
local params = paramstr:split(" ")
local subcmd = params[1]
if not subcmd then
return false, "Please specify a subcommand. Refer to /help umabis and /umabis help for help"
end
local scmd_dscptr = commands_descriptors[subcmd]
if not scmd_dscptr then
return false, "Invalid subcommand: "..subcmd..". See /umabis help for a list of available subcommands"
end
local scmd_params = {}
local i = 2
while i <= #params do
-- Quotes-enclosed strings are treated as a single parameter
if string.sub(params[i], 1, 1) == "\"" then
local p = params[i]
local j = i
repeat
j = j + 1
if not params[j] then
return false, "Unclosed quote"
end
p = p.." "..params[j]
until string.sub(params[j], string.len(params[j])) == "\""
table.insert(scmd_params, string.sub(p, 2, string.len(p)-1))
i = j + 1
else
table.insert(scmd_params, params[i])
i = i + 1
end
end
if #scmd_params < scmd_dscptr.params then
return false, "Subcommand "..subcmd.." requires at least "..scmd_dscptr.params.." parameters, given "..#scmd_params..". See /umabis help "..subcmd.." for help."
end
return scmd_dscptr.func(name, umabis.session.sessions[name].token, unpack(scmd_params))
end
})
if umabis.register_on_reload then -- If first loading
umabis.register_on_reload(function()
minetest.unregister_chatcommand("umabis")
end)
end