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