]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/game/chatcommands.lua
Improve LuaVoxelManip documentation
[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("help", {
88         privs = {},
89         params = "[all/privs/<cmd>]",
90         description = "Get help for commands or list privileges",
91         func = function(name, param)
92                 local function format_help_line(cmd, def)
93                         local msg = "/"..cmd
94                         if def.params and def.params ~= "" then
95                                 msg = msg .. " " .. def.params
96                         end
97                         if def.description and def.description ~= "" then
98                                 msg = msg .. ": " .. def.description
99                         end
100                         return msg
101                 end
102                 if param == "" then
103                         local msg = ""
104                         local cmds = {}
105                         for cmd, def in pairs(core.chatcommands) do
106                                 if core.check_player_privs(name, def.privs) then
107                                         table.insert(cmds, cmd)
108                                 end
109                         end
110                         table.sort(cmds)
111                         return true, "Available commands: " .. table.concat(cmds, " ") .. "\n"
112                                         .. "Use '/help <cmd>' to get more information,"
113                                         .. " or '/help all' to list everything."
114                 elseif param == "all" then
115                         local cmds = {}
116                         for cmd, def in pairs(core.chatcommands) do
117                                 if core.check_player_privs(name, def.privs) then
118                                         table.insert(cmds, format_help_line(cmd, def))
119                                 end
120                         end
121                         table.sort(cmds)
122                         return true, "Available commands:\n"..table.concat(cmds, "\n")
123                 elseif param == "privs" then
124                         local privs = {}
125                         for priv, def in pairs(core.registered_privileges) do
126                                 table.insert(privs, priv .. ": " .. def.description)
127                         end
128                         table.sort(privs)
129                         return true, "Available privileges:\n"..table.concat(privs, "\n")
130                 else
131                         local cmd = param
132                         local def = core.chatcommands[cmd]
133                         if not def then
134                                 return false, "Command not available: "..cmd
135                         else
136                                 return true, format_help_line(cmd, def)
137                         end
138                 end
139         end,
140 })
141
142 core.register_chatcommand("privs", {
143         params = "<name>",
144         description = "print out privileges of player",
145         func = function(name, param)
146                 param = (param ~= "" and param or name)
147                 return true, "Privileges of " .. param .. ": "
148                         .. core.privs_to_string(
149                                 core.get_player_privs(param), ' ')
150         end,
151 })
152 core.register_chatcommand("grant", {
153         params = "<name> <privilege>|all",
154         description = "Give privilege to player",
155         func = function(name, param)
156                 if not core.check_player_privs(name, {privs=true}) and
157                                 not core.check_player_privs(name, {basic_privs=true}) then
158                         return false, "Your privileges are insufficient."
159                 end
160                 local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
161                 if not grantname or not grantprivstr then
162                         return false, "Invalid parameters (see /help grant)"
163                 elseif not core.auth_table[grantname] then
164                         return false, "Player " .. grantname .. " does not exist."
165                 end
166                 local grantprivs = core.string_to_privs(grantprivstr)
167                 if grantprivstr == "all" then
168                         grantprivs = core.registered_privileges
169                 end
170                 local privs = core.get_player_privs(grantname)
171                 local privs_unknown = ""
172                 for priv, _ in pairs(grantprivs) do
173                         if priv ~= "interact" and priv ~= "shout" and
174                                         not core.check_player_privs(name, {privs=true}) then
175                                 return false, "Your privileges are insufficient."
176                         end
177                         if not core.registered_privileges[priv] then
178                                 privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
179                         end
180                         privs[priv] = true
181                 end
182                 if privs_unknown ~= "" then
183                         return false, privs_unknown
184                 end
185                 core.set_player_privs(grantname, privs)
186                 core.log("action", name..' granted ('..core.privs_to_string(grantprivs, ', ')..') privileges to '..grantname)
187                 if grantname ~= name then
188                         core.chat_send_player(grantname, name
189                                         .. " granted you privileges: "
190                                         .. core.privs_to_string(grantprivs, ' '))
191                 end
192                 return true, "Privileges of " .. grantname .. ": "
193                         .. core.privs_to_string(
194                                 core.get_player_privs(grantname), ' ')
195         end,
196 })
197 core.register_chatcommand("revoke", {
198         params = "<name> <privilege>|all",
199         description = "Remove privilege from player",
200         privs = {},
201         func = function(name, param)
202                 if not core.check_player_privs(name, {privs=true}) and
203                                 not core.check_player_privs(name, {basic_privs=true}) then
204                         return false, "Your privileges are insufficient."
205                 end
206                 local revoke_name, revoke_priv_str = string.match(param, "([^ ]+) (.+)")
207                 if not revoke_name or not revoke_priv_str then
208                         return false, "Invalid parameters (see /help revoke)"
209                 elseif not core.auth_table[revoke_name] then
210                         return false, "Player " .. revoke_name .. " does not exist."
211                 end
212                 local revoke_privs = core.string_to_privs(revoke_priv_str)
213                 local privs = core.get_player_privs(revoke_name)
214                 for priv, _ in pairs(revoke_privs) do
215                         if priv ~= "interact" and priv ~= "shout" and
216                                         not core.check_player_privs(name, {privs=true}) then
217                                 return false, "Your privileges are insufficient."
218                         end
219                 end
220                 if revoke_priv_str == "all" then
221                         privs = {}
222                 else
223                         for priv, _ in pairs(revoke_privs) do
224                                 privs[priv] = nil
225                         end
226                 end
227                 core.set_player_privs(revoke_name, privs)
228                 core.log("action", name..' revoked ('
229                                 ..core.privs_to_string(revoke_privs, ', ')
230                                 ..') privileges from '..revoke_name)
231                 if revoke_name ~= name then
232                         core.chat_send_player(revoke_name, name
233                                         .. " revoked privileges from you: "
234                                         .. core.privs_to_string(revoke_privs, ' '))
235                 end
236                 return true, "Privileges of " .. revoke_name .. ": "
237                         .. core.privs_to_string(
238                                 core.get_player_privs(revoke_name), ' ')
239         end,
240 })
241
242 core.register_chatcommand("setpassword", {
243         params = "<name> <password>",
244         description = "set given password",
245         privs = {password=true},
246         func = function(name, param)
247                 local toname, raw_password = string.match(param, "^([^ ]+) +(.+)$")
248                 if not toname then
249                         toname = param:match("^([^ ]+) *$")
250                         raw_password = nil
251                 end
252                 if not toname then
253                         return false, "Name field required"
254                 end
255                 local act_str_past = "?"
256                 local act_str_pres = "?"
257                 if not raw_password then
258                         core.set_player_password(toname, "")
259                         act_str_past = "cleared"
260                         act_str_pres = "clears"
261                 else
262                         core.set_player_password(toname,
263                                         core.get_password_hash(toname,
264                                                         raw_password))
265                         act_str_past = "set"
266                         act_str_pres = "sets"
267                 end
268                 if toname ~= name then
269                         core.chat_send_player(toname, "Your password was "
270                                         .. act_str_past .. " by " .. name)
271                 end
272
273                 core.log("action", name .. " " .. act_str_pres
274                 .. " password of " .. toname .. ".")
275
276                 return true, "Password of player \"" .. toname .. "\" " .. act_str_past
277         end,
278 })
279
280 core.register_chatcommand("clearpassword", {
281         params = "<name>",
282         description = "set empty password",
283         privs = {password=true},
284         func = function(name, param)
285                 local toname = param
286                 if toname == "" then
287                         return false, "Name field required"
288                 end
289                 core.set_player_password(toname, '')
290
291                 core.log("action", name .. " clears password of " .. toname .. ".")
292
293                 return true, "Password of player \"" .. toname .. "\" cleared"
294         end,
295 })
296
297 core.register_chatcommand("auth_reload", {
298         params = "",
299         description = "reload authentication data",
300         privs = {server=true},
301         func = function(name, param)
302                 local done = core.auth_reload()
303                 return done, (done and "Done." or "Failed.")
304         end,
305 })
306
307 core.register_chatcommand("teleport", {
308         params = "<X>,<Y>,<Z> | <to_name> | <name> <X>,<Y>,<Z> | <name> <to_name>",
309         description = "teleport to given position",
310         privs = {teleport=true},
311         func = function(name, param)
312                 -- Returns (pos, true) if found, otherwise (pos, false)
313                 local function find_free_position_near(pos)
314                         local tries = {
315                                 {x=1,y=0,z=0},
316                                 {x=-1,y=0,z=0},
317                                 {x=0,y=0,z=1},
318                                 {x=0,y=0,z=-1},
319                         }
320                         for _, d in ipairs(tries) do
321                                 local p = {x = pos.x+d.x, y = pos.y+d.y, z = pos.z+d.z}
322                                 local n = core.get_node_or_nil(p)
323                                 if n and n.name then
324                                         local def = core.registered_nodes[n.name]
325                                         if def and not def.walkable then
326                                                 return p, true
327                                         end
328                                 end
329                         end
330                         return pos, false
331                 end
332
333                 local teleportee = nil
334                 local p = {}
335                 p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
336                 p.x = tonumber(p.x)
337                 p.y = tonumber(p.y)
338                 p.z = tonumber(p.z)
339                 teleportee = core.get_player_by_name(name)
340                 if teleportee and p.x and p.y and p.z then
341                         teleportee:setpos(p)
342                         return true, "Teleporting to "..core.pos_to_string(p)
343                 end
344
345                 local teleportee = nil
346                 local p = nil
347                 local target_name = nil
348                 target_name = param:match("^([^ ]+)$")
349                 teleportee = core.get_player_by_name(name)
350                 if target_name then
351                         local target = core.get_player_by_name(target_name)
352                         if target then
353                                 p = target:getpos()
354                         end
355                 end
356                 if teleportee and p then
357                         p = find_free_position_near(p)
358                         teleportee:setpos(p)
359                         return true, "Teleporting to " .. target_name
360                                         .. " at "..core.pos_to_string(p)
361                 end
362
363                 if not core.check_player_privs(name, {bring=true}) then
364                         return false, "You don't have permission to teleport other players (missing bring privilege)"
365                 end
366
367                 local teleportee = nil
368                 local p = {}
369                 local teleportee_name = nil
370                 teleportee_name, p.x, p.y, p.z = param:match(
371                                 "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
372                 p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
373                 if teleportee_name then
374                         teleportee = core.get_player_by_name(teleportee_name)
375                 end
376                 if teleportee and p.x and p.y and p.z then
377                         teleportee:setpos(p)
378                         return true, "Teleporting " .. teleportee_name
379                                         .. " to " .. core.pos_to_string(p)
380                 end
381
382                 local teleportee = nil
383                 local p = nil
384                 local teleportee_name = nil
385                 local target_name = nil
386                 teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
387                 if teleportee_name then
388                         teleportee = core.get_player_by_name(teleportee_name)
389                 end
390                 if target_name then
391                         local target = core.get_player_by_name(target_name)
392                         if target then
393                                 p = target:getpos()
394                         end
395                 end
396                 if teleportee and p then
397                         p = find_free_position_near(p)
398                         teleportee:setpos(p)
399                         return true, "Teleporting " .. teleportee_name
400                                         .. " to " .. target_name
401                                         .. " at " .. core.pos_to_string(p)
402                 end
403
404                 return false, 'Invalid parameters ("' .. param
405                                 .. '") or player not found (see /help teleport)'
406         end,
407 })
408
409 core.register_chatcommand("set", {
410         params = "[-n] <name> <value> | <name>",
411         description = "set or read server configuration setting",
412         privs = {server=true},
413         func = function(name, param)
414                 local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
415                 if arg and arg == "-n" and setname and setvalue then
416                         core.setting_set(setname, setvalue)
417                         return true, setname .. " = " .. setvalue
418                 end
419                 local setname, setvalue = string.match(param, "([^ ]+) (.+)")
420                 if setname and setvalue then
421                         if not core.setting_get(setname) then
422                                 return false, "Failed. Use '/set -n <name> <value>' to create a new setting."
423                         end
424                         core.setting_set(setname, setvalue)
425                         return true, setname .. " = " .. setvalue
426                 end
427                 local setname = string.match(param, "([^ ]+)")
428                 if setname then
429                         local setvalue = core.setting_get(setname)
430                         if not setvalue then
431                                 setvalue = "<not set>"
432                         end
433                         return true, setname .. " = " .. setvalue
434                 end
435                 return false, "Invalid parameters (see /help set)."
436         end,
437 })
438
439 local function emergeblocks_callback(pos, action, num_calls_remaining, ctx)
440         if ctx.total_blocks == 0 then
441                 ctx.total_blocks   = num_calls_remaining + 1
442                 ctx.current_blocks = 0
443         end
444         ctx.current_blocks = ctx.current_blocks + 1
445
446         if ctx.current_blocks == ctx.total_blocks then
447                 core.chat_send_player(ctx.requestor_name,
448                         string.format("Finished emerging %d blocks in %.2fms.",
449                         ctx.total_blocks, (os.clock() - ctx.start_time) * 1000))
450         end
451 end
452
453 local function emergeblocks_progress_update(ctx)
454         if ctx.current_blocks ~= ctx.total_blocks then
455                 core.chat_send_player(ctx.requestor_name,
456                         string.format("emergeblocks update: %d/%d blocks emerged (%.1f%%)",
457                         ctx.current_blocks, ctx.total_blocks,
458                         (ctx.current_blocks / ctx.total_blocks) * 100))
459
460                 core.after(2, emergeblocks_progress_update, ctx)
461         end
462 end
463
464 core.register_chatcommand("emergeblocks", {
465         params = "(here [radius]) | (<pos1> <pos2>)",
466         description = "starts loading (or generating, if inexistent) map blocks "
467                 .. "contained in area pos1 to pos2",
468         privs = {server=true},
469         func = function(name, param)
470                 local p1, p2 = parse_range_str(name, param)
471                 if p1 == false then
472                         return false, p2
473                 end
474
475                 local context = {
476                         current_blocks = 0,
477                         total_blocks   = 0,
478                         start_time     = os.clock(),
479                         requestor_name = name
480                 }
481
482                 core.emerge_area(p1, p2, emergeblocks_callback, context)
483                 core.after(2, emergeblocks_progress_update, context)
484
485                 return true, "Started emerge of area ranging from " ..
486                         core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
487         end,
488 })
489
490 core.register_chatcommand("deleteblocks", {
491         params = "(here [radius]) | (<pos1> <pos2>)",
492         description = "delete map blocks contained in area pos1 to 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.delete_area(p1, p2) then
501                         return true, "Successfully cleared area ranging from " ..
502                                 core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
503                 else
504                         return false, "Failed to clear 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 has last touched a node or near it,"
641                         .. " max. <seconds> ago (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("shutdown", {
777         description = "shutdown server",
778         privs = {server=true},
779         func = function(name, param)
780                 core.log("action", name .. " shuts down server")
781                 core.request_shutdown()
782                 core.chat_send_all("*** Server shutting down (operator request).")
783         end,
784 })
785
786 core.register_chatcommand("ban", {
787         params = "<name>",
788         description = "Ban IP of player",
789         privs = {ban=true},
790         func = function(name, param)
791                 if param == "" then
792                         return true, "Ban list: " .. core.get_ban_list()
793                 end
794                 if not core.get_player_by_name(param) then
795                         return false, "No such player."
796                 end
797                 if not core.ban_player(param) then
798                         return false, "Failed to ban player."
799                 end
800                 local desc = core.get_ban_description(param)
801                 core.log("action", name .. " bans " .. desc .. ".")
802                 return true, "Banned " .. desc .. "."
803         end,
804 })
805
806 core.register_chatcommand("unban", {
807         params = "<name/ip>",
808         description = "remove IP ban",
809         privs = {ban=true},
810         func = function(name, param)
811                 if not core.unban_player_or_ip(param) then
812                         return false, "Failed to unban player/IP."
813                 end
814                 core.log("action", name .. " unbans " .. param)
815                 return true, "Unbanned " .. param
816         end,
817 })
818
819 core.register_chatcommand("kick", {
820         params = "<name> [reason]",
821         description = "kick a player",
822         privs = {kick=true},
823         func = function(name, param)
824                 local tokick, reason = param:match("([^ ]+) (.+)")
825                 tokick = tokick or param
826                 if not core.kick_player(tokick, reason) then
827                         return false, "Failed to kick player " .. tokick
828                 end
829                 local log_reason = ""
830                 if reason then
831                         log_reason = " with reason \"" .. reason .. "\""
832                 end
833                 core.log("action", name .. " kicks " .. tokick .. log_reason)
834                 return true, "Kicked " .. tokick
835         end,
836 })
837
838 core.register_chatcommand("clearobjects", {
839         description = "clear all objects in world",
840         privs = {server=true},
841         func = function(name, param)
842                 core.log("action", name .. " clears all objects.")
843                 core.chat_send_all("Clearing all objects.  This may take long."
844                                 .. "  You may experience a timeout.  (by "
845                                 .. name .. ")")
846                 core.clear_objects()
847                 core.log("action", "Object clearing done.")
848                 core.chat_send_all("*** Cleared all objects.")
849         end,
850 })
851
852 core.register_chatcommand("msg", {
853         params = "<name> <message>",
854         description = "Send a private message",
855         privs = {shout=true},
856         func = function(name, param)
857                 local sendto, message = param:match("^(%S+)%s(.+)$")
858                 if not sendto then
859                         return false, "Invalid usage, see /help msg."
860                 end
861                 if not core.get_player_by_name(sendto) then
862                         return false, "The player " .. sendto
863                                         .. " is not online."
864                 end
865                 core.log("action", "PM from " .. name .. " to " .. sendto
866                                 .. ": " .. message)
867                 core.chat_send_player(sendto, "PM from " .. name .. ": "
868                                 .. message)
869                 return true, "Message sent."
870         end,
871 })
872
873 core.register_chatcommand("last-login", {
874         params = "[name]",
875         description = "Get the last login time of a player",
876         func = function(name, param)
877                 if param == "" then
878                         param = name
879                 end
880                 local pauth = core.get_auth_handler().get_auth(param)
881                 if pauth and pauth.last_login then
882                         -- Time in UTC, ISO 8601 format
883                         return true, "Last login time was " ..
884                                 os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login)
885                 end
886                 return false, "Last login time is unknown"
887         end,
888 })