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