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