]> git.lizzy.rs Git - minetest.git/blob - builtin/game/chatcommands.lua
Add /fixlight chat command
[minetest.git] / builtin / game / chatcommands.lua
1 -- Minetest: builtin/game/chatcommands.lua
2
3 --
4 -- Chat command handler
5 --
6
7 core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY
8
9 core.register_on_chat_message(function(name, message)
10         if message:sub(1,1) ~= "/" then
11                 return
12         end
13
14         local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
15         if not cmd then
16                 core.chat_send_player(name, "-!- Empty command")
17                 return true
18         end
19
20         param = param or ""
21
22         local cmd_def = core.registered_chatcommands[cmd]
23         if not cmd_def then
24                 core.chat_send_player(name, "-!- Invalid command: " .. cmd)
25                 return true
26         end
27         local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
28         if has_privs then
29                 core.set_last_run_mod(cmd_def.mod_origin)
30                 local success, message = cmd_def.func(name, param)
31                 if message then
32                         core.chat_send_player(name, message)
33                 end
34         else
35                 core.chat_send_player(name, "You don't have permission"
36                                 .. " to run this command (missing privileges: "
37                                 .. table.concat(missing_privs, ", ") .. ")")
38         end
39         return true  -- Handled chat message
40 end)
41
42 if core.setting_getbool("profiler.load") then
43         -- Run after register_chatcommand and its register_on_chat_message
44         -- Before any chattcommands that should be profiled
45         profiler.init_chatcommand()
46 end
47
48 -- Parses a "range" string in the format of "here (number)" or
49 -- "(x1, y1, z1) (x2, y2, z2)", returning two position vectors
50 local function parse_range_str(player_name, str)
51         local p1, p2
52         local args = str:split(" ")
53
54         if args[1] == "here" then
55                 p1, p2 = core.get_player_radius_area(player_name, tonumber(args[2]))
56                 if p1 == nil then
57                         return false, "Unable to get player " .. player_name .. " position"
58                 end
59         else
60                 p1, p2 = core.string_to_area(str)
61                 if p1 == nil then
62                         return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
63                 end
64         end
65
66         return p1, p2
67 end
68
69 --
70 -- Chat commands
71 --
72 core.register_chatcommand("me", {
73         params = "<action>",
74         description = "Display chat action (e.g., '/me orders a pizza' displays"
75                         .. " '<player name> orders a pizza')",
76         privs = {shout=true},
77         func = function(name, param)
78                 core.chat_send_all("* " .. name .. " " .. param)
79         end,
80 })
81
82 core.register_chatcommand("admin", {
83         description = "Show the name of the server owner",
84         func = function(name)
85                 local admin = minetest.setting_get("name")
86                 if admin then
87                         return true, "The administrator of this server is "..admin.."."
88                 else
89                         return false, "There's no administrator named in the config file."
90                 end
91         end,
92 })
93
94 core.register_chatcommand("privs", {
95         params = "<name>",
96         description = "Print privileges of player",
97         func = function(caller, param)
98                 param = param:trim()
99                 local name = (param ~= "" and param or caller)
100                 return true, "Privileges of " .. name .. ": "
101                         .. core.privs_to_string(
102                                 core.get_player_privs(name), ' ')
103         end,
104 })
105
106 local function handle_grant_command(caller, grantname, grantprivstr)
107         local caller_privs = minetest.get_player_privs(caller)
108         if not (caller_privs.privs or caller_privs.basic_privs) then
109                 return false, "Your privileges are insufficient."
110         end
111
112         if not core.get_auth_handler().get_auth(grantname) then
113                 return false, "Player " .. grantname .. " does not exist."
114         end
115         local grantprivs = core.string_to_privs(grantprivstr)
116         if grantprivstr == "all" then
117                 grantprivs = core.registered_privileges
118         end
119         local privs = core.get_player_privs(grantname)
120         local privs_unknown = ""
121         local basic_privs =
122                 core.string_to_privs(core.setting_get("basic_privs") or "interact,shout")
123         for priv, _ in pairs(grantprivs) do
124                 if not basic_privs[priv] and not caller_privs.privs then
125                         return false, "Your privileges are insufficient."
126                 end
127                 if not core.registered_privileges[priv] then
128                         privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
129                 end
130                 privs[priv] = true
131         end
132         if privs_unknown ~= "" then
133                 return false, privs_unknown
134         end
135         core.set_player_privs(grantname, privs)
136         core.log("action", caller..' granted ('..core.privs_to_string(grantprivs, ', ')..') privileges to '..grantname)
137         if grantname ~= caller then
138                 core.chat_send_player(grantname, caller
139                                 .. " granted you privileges: "
140                                 .. core.privs_to_string(grantprivs, ' '))
141         end
142         return true, "Privileges of " .. grantname .. ": "
143                 .. core.privs_to_string(
144                         core.get_player_privs(grantname), ' ')
145 end
146
147 core.register_chatcommand("grant", {
148         params = "<name> <privilege>|all",
149         description = "Give privilege to player",
150         func = function(name, param)
151                 local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
152                 if not grantname or not grantprivstr then
153                         return false, "Invalid parameters (see /help grant)"
154                 end
155                 return handle_grant_command(name, grantname, grantprivstr)
156         end,
157 })
158
159 core.register_chatcommand("grantme", {
160         params = "<privilege>|all",
161         description = "Grant privileges to yourself",
162         func = function(name, param)
163                 if param == "" then
164                         return false, "Invalid parameters (see /help grantme)"
165                 end
166                 return handle_grant_command(name, name, param)
167         end,
168 })
169
170 core.register_chatcommand("revoke", {
171         params = "<name> <privilege>|all",
172         description = "Remove privilege from player",
173         privs = {},
174         func = function(name, param)
175                 if not core.check_player_privs(name, {privs=true}) and
176                                 not core.check_player_privs(name, {basic_privs=true}) then
177                         return false, "Your privileges are insufficient."
178                 end
179                 local revoke_name, revoke_priv_str = string.match(param, "([^ ]+) (.+)")
180                 if not revoke_name or not revoke_priv_str then
181                         return false, "Invalid parameters (see /help revoke)"
182                 elseif not core.get_auth_handler().get_auth(revoke_name) then
183                         return false, "Player " .. revoke_name .. " does not exist."
184                 end
185                 local revoke_privs = core.string_to_privs(revoke_priv_str)
186                 local privs = core.get_player_privs(revoke_name)
187                 local basic_privs =
188                         core.string_to_privs(core.setting_get("basic_privs") or "interact,shout")
189                 for priv, _ in pairs(revoke_privs) do
190                         if not basic_privs[priv] and
191                                         not core.check_player_privs(name, {privs=true}) then
192                                 return false, "Your privileges are insufficient."
193                         end
194                 end
195                 if revoke_priv_str == "all" then
196                         privs = {}
197                 else
198                         for priv, _ in pairs(revoke_privs) do
199                                 privs[priv] = nil
200                         end
201                 end
202                 core.set_player_privs(revoke_name, privs)
203                 core.log("action", name..' revoked ('
204                                 ..core.privs_to_string(revoke_privs, ', ')
205                                 ..') privileges from '..revoke_name)
206                 if revoke_name ~= name then
207                         core.chat_send_player(revoke_name, name
208                                         .. " revoked privileges from you: "
209                                         .. core.privs_to_string(revoke_privs, ' '))
210                 end
211                 return true, "Privileges of " .. revoke_name .. ": "
212                         .. core.privs_to_string(
213                                 core.get_player_privs(revoke_name), ' ')
214         end,
215 })
216
217 core.register_chatcommand("setpassword", {
218         params = "<name> <password>",
219         description = "Set player's password",
220         privs = {password=true},
221         func = function(name, param)
222                 local toname, raw_password = string.match(param, "^([^ ]+) +(.+)$")
223                 if not toname then
224                         toname = param:match("^([^ ]+) *$")
225                         raw_password = nil
226                 end
227                 if not toname then
228                         return false, "Name field required"
229                 end
230                 local act_str_past = "?"
231                 local act_str_pres = "?"
232                 if not raw_password then
233                         core.set_player_password(toname, "")
234                         act_str_past = "cleared"
235                         act_str_pres = "clears"
236                 else
237                         core.set_player_password(toname,
238                                         core.get_password_hash(toname,
239                                                         raw_password))
240                         act_str_past = "set"
241                         act_str_pres = "sets"
242                 end
243                 if toname ~= name then
244                         core.chat_send_player(toname, "Your password was "
245                                         .. act_str_past .. " by " .. name)
246                 end
247
248                 core.log("action", name .. " " .. act_str_pres
249                 .. " password of " .. toname .. ".")
250
251                 return true, "Password of player \"" .. toname .. "\" " .. act_str_past
252         end,
253 })
254
255 core.register_chatcommand("clearpassword", {
256         params = "<name>",
257         description = "Set empty password",
258         privs = {password=true},
259         func = function(name, param)
260                 local toname = param
261                 if toname == "" then
262                         return false, "Name field required"
263                 end
264                 core.set_player_password(toname, '')
265
266                 core.log("action", name .. " clears password of " .. toname .. ".")
267
268                 return true, "Password of player \"" .. toname .. "\" cleared"
269         end,
270 })
271
272 core.register_chatcommand("auth_reload", {
273         params = "",
274         description = "Reload authentication data",
275         privs = {server=true},
276         func = function(name, param)
277                 local done = core.auth_reload()
278                 return done, (done and "Done." or "Failed.")
279         end,
280 })
281
282 core.register_chatcommand("teleport", {
283         params = "<X>,<Y>,<Z> | <to_name> | <name> <X>,<Y>,<Z> | <name> <to_name>",
284         description = "Teleport to player or position",
285         privs = {teleport=true},
286         func = function(name, param)
287                 -- Returns (pos, true) if found, otherwise (pos, false)
288                 local function find_free_position_near(pos)
289                         local tries = {
290                                 {x=1,y=0,z=0},
291                                 {x=-1,y=0,z=0},
292                                 {x=0,y=0,z=1},
293                                 {x=0,y=0,z=-1},
294                         }
295                         for _, d in ipairs(tries) do
296                                 local p = {x = pos.x+d.x, y = pos.y+d.y, z = pos.z+d.z}
297                                 local n = core.get_node_or_nil(p)
298                                 if n and n.name then
299                                         local def = core.registered_nodes[n.name]
300                                         if def and not def.walkable then
301                                                 return p, true
302                                         end
303                                 end
304                         end
305                         return pos, false
306                 end
307
308                 local teleportee = nil
309                 local p = {}
310                 p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
311                 p.x = tonumber(p.x)
312                 p.y = tonumber(p.y)
313                 p.z = tonumber(p.z)
314                 if p.x and p.y and p.z then
315                         local lm = 31000
316                         if p.x < -lm or p.x > lm or p.y < -lm or p.y > lm or p.z < -lm or p.z > lm then
317                                 return false, "Cannot teleport out of map bounds!"
318                         end
319                         teleportee = core.get_player_by_name(name)
320                         if teleportee then
321                                 teleportee:setpos(p)
322                                 return true, "Teleporting to "..core.pos_to_string(p)
323                         end
324                 end
325
326                 local teleportee = nil
327                 local p = nil
328                 local target_name = nil
329                 target_name = param:match("^([^ ]+)$")
330                 teleportee = core.get_player_by_name(name)
331                 if target_name then
332                         local target = core.get_player_by_name(target_name)
333                         if target then
334                                 p = target:getpos()
335                         end
336                 end
337                 if teleportee and p then
338                         p = find_free_position_near(p)
339                         teleportee:setpos(p)
340                         return true, "Teleporting to " .. target_name
341                                         .. " at "..core.pos_to_string(p)
342                 end
343
344                 if not core.check_player_privs(name, {bring=true}) then
345                         return false, "You don't have permission to teleport other players (missing bring privilege)"
346                 end
347
348                 local teleportee = nil
349                 local p = {}
350                 local teleportee_name = nil
351                 teleportee_name, p.x, p.y, p.z = param:match(
352                                 "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
353                 p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
354                 if teleportee_name then
355                         teleportee = core.get_player_by_name(teleportee_name)
356                 end
357                 if teleportee and p.x and p.y and p.z then
358                         teleportee:setpos(p)
359                         return true, "Teleporting " .. teleportee_name
360                                         .. " to " .. core.pos_to_string(p)
361                 end
362
363                 local teleportee = nil
364                 local p = nil
365                 local teleportee_name = nil
366                 local target_name = nil
367                 teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
368                 if teleportee_name then
369                         teleportee = core.get_player_by_name(teleportee_name)
370                 end
371                 if target_name then
372                         local target = core.get_player_by_name(target_name)
373                         if target then
374                                 p = target:getpos()
375                         end
376                 end
377                 if teleportee and p then
378                         p = find_free_position_near(p)
379                         teleportee:setpos(p)
380                         return true, "Teleporting " .. teleportee_name
381                                         .. " to " .. target_name
382                                         .. " at " .. core.pos_to_string(p)
383                 end
384
385                 return false, 'Invalid parameters ("' .. param
386                                 .. '") or player not found (see /help teleport)'
387         end,
388 })
389
390 core.register_chatcommand("set", {
391         params = "[-n] <name> <value> | <name>",
392         description = "Set or read server configuration setting",
393         privs = {server=true},
394         func = function(name, param)
395                 local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
396                 if arg and arg == "-n" and setname and setvalue then
397                         core.setting_set(setname, setvalue)
398                         return true, setname .. " = " .. setvalue
399                 end
400                 local setname, setvalue = string.match(param, "([^ ]+) (.+)")
401                 if setname and setvalue then
402                         if not core.setting_get(setname) then
403                                 return false, "Failed. Use '/set -n <name> <value>' to create a new setting."
404                         end
405                         core.setting_set(setname, setvalue)
406                         return true, setname .. " = " .. setvalue
407                 end
408                 local setname = string.match(param, "([^ ]+)")
409                 if setname then
410                         local setvalue = core.setting_get(setname)
411                         if not setvalue then
412                                 setvalue = "<not set>"
413                         end
414                         return true, setname .. " = " .. setvalue
415                 end
416                 return false, "Invalid parameters (see /help set)."
417         end,
418 })
419
420 local function emergeblocks_callback(pos, action, num_calls_remaining, ctx)
421         if ctx.total_blocks == 0 then
422                 ctx.total_blocks   = num_calls_remaining + 1
423                 ctx.current_blocks = 0
424         end
425         ctx.current_blocks = ctx.current_blocks + 1
426
427         if ctx.current_blocks == ctx.total_blocks then
428                 core.chat_send_player(ctx.requestor_name,
429                         string.format("Finished emerging %d blocks in %.2fms.",
430                         ctx.total_blocks, (os.clock() - ctx.start_time) * 1000))
431         end
432 end
433
434 local function emergeblocks_progress_update(ctx)
435         if ctx.current_blocks ~= ctx.total_blocks then
436                 core.chat_send_player(ctx.requestor_name,
437                         string.format("emergeblocks update: %d/%d blocks emerged (%.1f%%)",
438                         ctx.current_blocks, ctx.total_blocks,
439                         (ctx.current_blocks / ctx.total_blocks) * 100))
440
441                 core.after(2, emergeblocks_progress_update, ctx)
442         end
443 end
444
445 core.register_chatcommand("emergeblocks", {
446         params = "(here [radius]) | (<pos1> <pos2>)",
447         description = "Load (or, if nonexistent, generate) map blocks "
448                 .. "contained in area pos1 to pos2",
449         privs = {server=true},
450         func = function(name, param)
451                 local p1, p2 = parse_range_str(name, param)
452                 if p1 == false then
453                         return false, p2
454                 end
455
456                 local context = {
457                         current_blocks = 0,
458                         total_blocks   = 0,
459                         start_time     = os.clock(),
460                         requestor_name = name
461                 }
462
463                 core.emerge_area(p1, p2, emergeblocks_callback, context)
464                 core.after(2, emergeblocks_progress_update, context)
465
466                 return true, "Started emerge of area ranging from " ..
467                         core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
468         end,
469 })
470
471 core.register_chatcommand("deleteblocks", {
472         params = "(here [radius]) | (<pos1> <pos2>)",
473         description = "Delete map blocks contained in area pos1 to pos2",
474         privs = {server=true},
475         func = function(name, param)
476                 local p1, p2 = parse_range_str(name, param)
477                 if p1 == false then
478                         return false, p2
479                 end
480
481                 if core.delete_area(p1, p2) then
482                         return true, "Successfully cleared area ranging from " ..
483                                 core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
484                 else
485                         return false, "Failed to clear one or more blocks in area"
486                 end
487         end,
488 })
489
490 core.register_chatcommand("fixlight", {
491         params = "(here [radius]) | (<pos1> <pos2>)",
492         description = "Resets lighting in the area between pos1 and pos2",
493         privs = {server = true},
494         func = function(name, param)
495                 local p1, p2 = parse_range_str(name, param)
496                 if p1 == false then
497                         return false, p2
498                 end
499
500                 if core.fix_light(p1, p2) then
501                         return true, "Successfully reset light in the area ranging from " ..
502                                 core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
503                 else
504                         return false, "Failed to load one or more blocks in area"
505                 end
506         end,
507 })
508
509 core.register_chatcommand("mods", {
510         params = "",
511         description = "List mods installed on the server",
512         privs = {},
513         func = function(name, param)
514                 return true, table.concat(core.get_modnames(), ", ")
515         end,
516 })
517
518 local function handle_give_command(cmd, giver, receiver, stackstring)
519         core.log("action", giver .. " invoked " .. cmd
520                         .. ', stackstring="' .. stackstring .. '"')
521         local itemstack = ItemStack(stackstring)
522         if itemstack:is_empty() then
523                 return false, "Cannot give an empty item"
524         elseif not itemstack:is_known() then
525                 return false, "Cannot give an unknown item"
526         end
527         local receiverref = core.get_player_by_name(receiver)
528         if receiverref == nil then
529                 return false, receiver .. " is not a known player"
530         end
531         local leftover = receiverref:get_inventory():add_item("main", itemstack)
532         local partiality
533         if leftover:is_empty() then
534                 partiality = ""
535         elseif leftover:get_count() == itemstack:get_count() then
536                 partiality = "could not be "
537         else
538                 partiality = "partially "
539         end
540         -- The actual item stack string may be different from what the "giver"
541         -- entered (e.g. big numbers are always interpreted as 2^16-1).
542         stackstring = itemstack:to_string()
543         if giver == receiver then
544                 return true, ("%q %sadded to inventory.")
545                                 :format(stackstring, partiality)
546         else
547                 core.chat_send_player(receiver, ("%q %sadded to inventory.")
548                                 :format(stackstring, partiality))
549                 return true, ("%q %sadded to %s's inventory.")
550                                 :format(stackstring, partiality, receiver)
551         end
552 end
553
554 core.register_chatcommand("give", {
555         params = "<name> <ItemString>",
556         description = "Give item to player",
557         privs = {give=true},
558         func = function(name, param)
559                 local toname, itemstring = string.match(param, "^([^ ]+) +(.+)$")
560                 if not toname or not itemstring then
561                         return false, "Name and ItemString required"
562                 end
563                 return handle_give_command("/give", name, toname, itemstring)
564         end,
565 })
566
567 core.register_chatcommand("giveme", {
568         params = "<ItemString>",
569         description = "Give item to yourself",
570         privs = {give=true},
571         func = function(name, param)
572                 local itemstring = string.match(param, "(.+)$")
573                 if not itemstring then
574                         return false, "ItemString required"
575                 end
576                 return handle_give_command("/giveme", name, name, itemstring)
577         end,
578 })
579
580 core.register_chatcommand("spawnentity", {
581         params = "<EntityName> [<X>,<Y>,<Z>]",
582         description = "Spawn entity at given (or your) position",
583         privs = {give=true, interact=true},
584         func = function(name, param)
585                 local entityname, p = string.match(param, "^([^ ]+) *(.*)$")
586                 if not entityname then
587                         return false, "EntityName required"
588                 end
589                 core.log("action", ("%s invokes /spawnentity, entityname=%q")
590                                 :format(name, entityname))
591                 local player = core.get_player_by_name(name)
592                 if player == nil then
593                         core.log("error", "Unable to spawn entity, player is nil")
594                         return false, "Unable to spawn entity, player is nil"
595                 end
596                 if p == "" then
597                         p = player:getpos()
598                 else
599                         p = core.string_to_pos(p)
600                         if p == nil then
601                                 return false, "Invalid parameters ('" .. param .. "')"
602                         end
603                 end
604                 p.y = p.y + 1
605                 core.add_entity(p, entityname)
606                 return true, ("%q spawned."):format(entityname)
607         end,
608 })
609
610 core.register_chatcommand("pulverize", {
611         params = "",
612         description = "Destroy item in hand",
613         func = function(name, param)
614                 local player = core.get_player_by_name(name)
615                 if not player then
616                         core.log("error", "Unable to pulverize, no player.")
617                         return false, "Unable to pulverize, no player."
618                 end
619                 if player:get_wielded_item():is_empty() then
620                         return false, "Unable to pulverize, no item in hand."
621                 end
622                 player:set_wielded_item(nil)
623                 return true, "An item was pulverized."
624         end,
625 })
626
627 -- Key = player name
628 core.rollback_punch_callbacks = {}
629
630 core.register_on_punchnode(function(pos, node, puncher)
631         local name = puncher:get_player_name()
632         if core.rollback_punch_callbacks[name] then
633                 core.rollback_punch_callbacks[name](pos, node, puncher)
634                 core.rollback_punch_callbacks[name] = nil
635         end
636 end)
637
638 core.register_chatcommand("rollback_check", {
639         params = "[<range>] [<seconds>] [limit]",
640         description = "Check who last touched a node or a node near it"
641                         .. " within the time specified by <seconds>. Default: range = 0,"
642                         .. " seconds = 86400 = 24h, limit = 5",
643         privs = {rollback=true},
644         func = function(name, param)
645                 if not core.setting_getbool("enable_rollback_recording") then
646                         return false, "Rollback functions are disabled."
647                 end
648                 local range, seconds, limit =
649                         param:match("(%d+) *(%d*) *(%d*)")
650                 range = tonumber(range) or 0
651                 seconds = tonumber(seconds) or 86400
652                 limit = tonumber(limit) or 5
653                 if limit > 100 then
654                         return false, "That limit is too high!"
655                 end
656
657                 core.rollback_punch_callbacks[name] = function(pos, node, puncher)
658                         local name = puncher:get_player_name()
659                         core.chat_send_player(name, "Checking " .. core.pos_to_string(pos) .. "...")
660                         local actions = core.rollback_get_node_actions(pos, range, seconds, limit)
661                         if not actions then
662                                 core.chat_send_player(name, "Rollback functions are disabled")
663                                 return
664                         end
665                         local num_actions = #actions
666                         if num_actions == 0 then
667                                 core.chat_send_player(name, "Nobody has touched"
668                                                 .. " the specified location in "
669                                                 .. seconds .. " seconds")
670                                 return
671                         end
672                         local time = os.time()
673                         for i = num_actions, 1, -1 do
674                                 local action = actions[i]
675                                 core.chat_send_player(name,
676                                         ("%s %s %s -> %s %d seconds ago.")
677                                                 :format(
678                                                         core.pos_to_string(action.pos),
679                                                         action.actor,
680                                                         action.oldnode.name,
681                                                         action.newnode.name,
682                                                         time - action.time))
683                         end
684                 end
685
686                 return true, "Punch a node (range=" .. range .. ", seconds="
687                                 .. seconds .. "s, limit=" .. limit .. ")"
688         end,
689 })
690
691 core.register_chatcommand("rollback", {
692         params = "<player name> [<seconds>] | :<actor> [<seconds>]",
693         description = "Revert actions of a player. Default for <seconds> is 60",
694         privs = {rollback=true},
695         func = function(name, param)
696                 if not core.setting_getbool("enable_rollback_recording") then
697                         return false, "Rollback functions are disabled."
698                 end
699                 local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
700                 if not target_name then
701                         local player_name = nil
702                         player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
703                         if not player_name then
704                                 return false, "Invalid parameters. See /help rollback"
705                                                 .. " and /help rollback_check."
706                         end
707                         target_name = "player:"..player_name
708                 end
709                 seconds = tonumber(seconds) or 60
710                 core.chat_send_player(name, "Reverting actions of "
711                                 .. target_name .. " since "
712                                 .. seconds .. " seconds.")
713                 local success, log = core.rollback_revert_actions_by(
714                                 target_name, seconds)
715                 local response = ""
716                 if #log > 100 then
717                         response = "(log is too long to show)\n"
718                 else
719                         for _, line in pairs(log) do
720                                 response = response .. line .. "\n"
721                         end
722                 end
723                 response = response .. "Reverting actions "
724                                 .. (success and "succeeded." or "FAILED.")
725                 return success, response
726         end,
727 })
728
729 core.register_chatcommand("status", {
730         description = "Print server status",
731         func = function(name, param)
732                 return true, core.get_server_status()
733         end,
734 })
735
736 core.register_chatcommand("time", {
737         params = "<0..23>:<0..59> | <0..24000>",
738         description = "Set time of day",
739         privs = {},
740         func = function(name, param)
741                 if param == "" then
742                         local current_time = math.floor(core.get_timeofday() * 1440)
743                         local minutes = current_time % 60
744                         local hour = (current_time - minutes) / 60
745                         return true, ("Current time is %d:%02d"):format(hour, minutes)
746                 end
747                 local player_privs = core.get_player_privs(name)
748                 if not player_privs.settime then
749                         return false, "You don't have permission to run this command " ..
750                                 "(missing privilege: settime)."
751                 end
752                 local hour, minute = param:match("^(%d+):(%d+)$")
753                 if not hour then
754                         local new_time = tonumber(param)
755                         if not new_time then
756                                 return false, "Invalid time."
757                         end
758                         -- Backward compatibility.
759                         core.set_timeofday((new_time % 24000) / 24000)
760                         core.log("action", name .. " sets time to " .. new_time)
761                         return true, "Time of day changed."
762                 end
763                 hour = tonumber(hour)
764                 minute = tonumber(minute)
765                 if hour < 0 or hour > 23 then
766                         return false, "Invalid hour (must be between 0 and 23 inclusive)."
767                 elseif minute < 0 or minute > 59 then
768                         return false, "Invalid minute (must be between 0 and 59 inclusive)."
769                 end
770                 core.set_timeofday((hour * 60 + minute) / 1440)
771                 core.log("action", ("%s sets time to %d:%02d"):format(name, hour, minute))
772                 return true, "Time of day changed."
773         end,
774 })
775
776 core.register_chatcommand("days", {
777         description = "Display day count",
778         func = function(name, param)
779                 return true, "Current day is " .. core.get_day_count()
780         end
781 })
782
783 core.register_chatcommand("shutdown", {
784         description = "Shutdown server",
785         params = "[delay_in_seconds(0..inf) or -1 for cancel] [reconnect] [message]",
786         privs = {server=true},
787         func = function(name, param)
788                 local delay, reconnect, message = param:match("([^ ][-]?[0-9]+)([^ ]+)(.*)")
789                 message = message or ""
790
791                 if delay ~= "" then
792                         delay = tonumber(param) or 0
793                 else
794                         delay = 0
795                         core.log("action", name .. " shuts down server")
796                         core.chat_send_all("*** Server shutting down (operator request).")
797                 end
798                 core.request_shutdown(message:trim(), core.is_yes(reconnect), delay)
799         end,
800 })
801
802 core.register_chatcommand("ban", {
803         params = "<name>",
804         description = "Ban IP of player",
805         privs = {ban=true},
806         func = function(name, param)
807                 if param == "" then
808                         return true, "Ban list: " .. core.get_ban_list()
809                 end
810                 if not core.get_player_by_name(param) then
811                         return false, "No such player."
812                 end
813                 if not core.ban_player(param) then
814                         return false, "Failed to ban player."
815                 end
816                 local desc = core.get_ban_description(param)
817                 core.log("action", name .. " bans " .. desc .. ".")
818                 return true, "Banned " .. desc .. "."
819         end,
820 })
821
822 core.register_chatcommand("unban", {
823         params = "<name/ip>",
824         description = "Remove IP ban",
825         privs = {ban=true},
826         func = function(name, param)
827                 if not core.unban_player_or_ip(param) then
828                         return false, "Failed to unban player/IP."
829                 end
830                 core.log("action", name .. " unbans " .. param)
831                 return true, "Unbanned " .. param
832         end,
833 })
834
835 core.register_chatcommand("kick", {
836         params = "<name> [reason]",
837         description = "Kick a player",
838         privs = {kick=true},
839         func = function(name, param)
840                 local tokick, reason = param:match("([^ ]+) (.+)")
841                 tokick = tokick or param
842                 if not core.kick_player(tokick, reason) then
843                         return false, "Failed to kick player " .. tokick
844                 end
845                 local log_reason = ""
846                 if reason then
847                         log_reason = " with reason \"" .. reason .. "\""
848                 end
849                 core.log("action", name .. " kicks " .. tokick .. log_reason)
850                 return true, "Kicked " .. tokick
851         end,
852 })
853
854 core.register_chatcommand("clearobjects", {
855         params = "[full|quick]",
856         description = "Clear all objects in world",
857         privs = {server=true},
858         func = function(name, param)
859                 local options = {}
860                 if param == "" or param == "full" then
861                         options.mode = "full"
862                 elseif param == "quick" then
863                         options.mode = "quick"
864                 else
865                         return false, "Invalid usage, see /help clearobjects."
866                 end
867
868                 core.log("action", name .. " clears all objects ("
869                                 .. options.mode .. " mode).")
870                 core.chat_send_all("Clearing all objects.  This may take long."
871                                 .. "  You may experience a timeout.  (by "
872                                 .. name .. ")")
873                 core.clear_objects(options)
874                 core.log("action", "Object clearing done.")
875                 core.chat_send_all("*** Cleared all objects.")
876         end,
877 })
878
879 core.register_chatcommand("msg", {
880         params = "<name> <message>",
881         description = "Send a private message",
882         privs = {shout=true},
883         func = function(name, param)
884                 local sendto, message = param:match("^(%S+)%s(.+)$")
885                 if not sendto then
886                         return false, "Invalid usage, see /help msg."
887                 end
888                 if not core.get_player_by_name(sendto) then
889                         return false, "The player " .. sendto
890                                         .. " is not online."
891                 end
892                 core.log("action", "PM from " .. name .. " to " .. sendto
893                                 .. ": " .. message)
894                 core.chat_send_player(sendto, "PM from " .. name .. ": "
895                                 .. message)
896                 return true, "Message sent."
897         end,
898 })
899
900 core.register_chatcommand("last-login", {
901         params = "[name]",
902         description = "Get the last login time of a player",
903         func = function(name, param)
904                 if param == "" then
905                         param = name
906                 end
907                 local pauth = core.get_auth_handler().get_auth(param)
908                 if pauth and pauth.last_login then
909                         -- Time in UTC, ISO 8601 format
910                         return true, "Last login time was " ..
911                                 os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login)
912                 end
913                 return false, "Last login time is unknown"
914         end,
915 })