5f9fcfc7bb4f61cae1a8d9c9701a55c17aae35ad
[oweals/minetest.git] / builtin / game / chatcommands.lua
1 -- Minetest: builtin/chatcommands.lua
2
3 --
4 -- Chat command handler
5 --
6
7 core.chatcommands = {}
8 function core.register_chatcommand(cmd, def)
9         def = def or {}
10         def.params = def.params or ""
11         def.description = def.description or ""
12         def.privs = def.privs or {}
13         def.mod_origin = core.get_current_modname() or "??"
14         core.chatcommands[cmd] = def
15 end
16
17 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 core.register_chatcommand("emergeblocks", {
440         params = "(here [radius]) | (<pos1> <pos2>)",
441         description = "starts loading (or generating, if inexistent) map blocks "
442                 .. "contained in area pos1 to pos2",
443         privs = {server=true},
444         func = function(name, param)
445                 local p1, p2 = parse_range_str(name, param)
446                 if p1 == false then
447                         return false, p2
448                 end
449
450                 core.emerge_area(p1, p2)
451                 return true, "Started emerge of area ranging from " ..
452                         core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
453         end,
454 })
455
456 core.register_chatcommand("deleteblocks", {
457         params = "(here [radius]) | (<pos1> <pos2>)",
458         description = "delete map blocks contained in area pos1 to pos2",
459         privs = {server=true},
460         func = function(name, param)
461                 local p1, p2 = parse_range_str(name, param)
462                 if p1 == false then
463                         return false, p2
464                 end
465
466                 if core.delete_area(p1, p2) then
467                         return true, "Successfully cleared area ranging from " ..
468                                 core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
469                 else
470                         return false, "Failed to clear one or more blocks in area"
471                 end
472         end,
473 })
474
475 core.register_chatcommand("mods", {
476         params = "",
477         description = "List mods installed on the server",
478         privs = {},
479         func = function(name, param)
480                 return true, table.concat(core.get_modnames(), ", ")
481         end,
482 })
483
484 local function handle_give_command(cmd, giver, receiver, stackstring)
485         core.log("action", giver .. " invoked " .. cmd
486                         .. ', stackstring="' .. stackstring .. '"')
487         local itemstack = ItemStack(stackstring)
488         if itemstack:is_empty() then
489                 return false, "Cannot give an empty item"
490         elseif not itemstack:is_known() then
491                 return false, "Cannot give an unknown item"
492         end
493         local receiverref = core.get_player_by_name(receiver)
494         if receiverref == nil then
495                 return false, receiver .. " is not a known player"
496         end
497         local leftover = receiverref:get_inventory():add_item("main", itemstack)
498         local partiality
499         if leftover:is_empty() then
500                 partiality = ""
501         elseif leftover:get_count() == itemstack:get_count() then
502                 partiality = "could not be "
503         else
504                 partiality = "partially "
505         end
506         -- The actual item stack string may be different from what the "giver"
507         -- entered (e.g. big numbers are always interpreted as 2^16-1).
508         stackstring = itemstack:to_string()
509         if giver == receiver then
510                 return true, ("%q %sadded to inventory.")
511                                 :format(stackstring, partiality)
512         else
513                 core.chat_send_player(receiver, ("%q %sadded to inventory.")
514                                 :format(stackstring, partiality))
515                 return true, ("%q %sadded to %s's inventory.")
516                                 :format(stackstring, partiality, receiver)
517         end
518 end
519
520 core.register_chatcommand("give", {
521         params = "<name> <ItemString>",
522         description = "give item to player",
523         privs = {give=true},
524         func = function(name, param)
525                 local toname, itemstring = string.match(param, "^([^ ]+) +(.+)$")
526                 if not toname or not itemstring then
527                         return false, "Name and ItemString required"
528                 end
529                 return handle_give_command("/give", name, toname, itemstring)
530         end,
531 })
532
533 core.register_chatcommand("giveme", {
534         params = "<ItemString>",
535         description = "give item to yourself",
536         privs = {give=true},
537         func = function(name, param)
538                 local itemstring = string.match(param, "(.+)$")
539                 if not itemstring then
540                         return false, "ItemString required"
541                 end
542                 return handle_give_command("/giveme", name, name, itemstring)
543         end,
544 })
545
546 core.register_chatcommand("spawnentity", {
547         params = "<EntityName> [<X>,<Y>,<Z>]",
548         description = "Spawn entity at given (or your) position",
549         privs = {give=true, interact=true},
550         func = function(name, param)
551                 local entityname, p = string.match(param, "^([^ ]+) *(.*)$")
552                 if not entityname then
553                         return false, "EntityName required"
554                 end
555                 core.log("action", ("%s invokes /spawnentity, entityname=%q")
556                                 :format(name, entityname))
557                 local player = core.get_player_by_name(name)
558                 if player == nil then
559                         core.log("error", "Unable to spawn entity, player is nil")
560                         return false, "Unable to spawn entity, player is nil"
561                 end
562                 if p == "" then
563                         p = player:getpos()
564                 else
565                         p = core.string_to_pos(p)
566                         if p == nil then
567                                 return false, "Invalid parameters ('" .. param .. "')"
568                         end
569                 end
570                 p.y = p.y + 1
571                 core.add_entity(p, entityname)
572                 return true, ("%q spawned."):format(entityname)
573         end,
574 })
575
576 core.register_chatcommand("pulverize", {
577         params = "",
578         description = "Destroy item in hand",
579         func = function(name, param)
580                 local player = core.get_player_by_name(name)
581                 if not player then
582                         core.log("error", "Unable to pulverize, no player.")
583                         return false, "Unable to pulverize, no player."
584                 end
585                 if player:get_wielded_item():is_empty() then
586                         return false, "Unable to pulverize, no item in hand."
587                 end
588                 player:set_wielded_item(nil)
589                 return true, "An item was pulverized."
590         end,
591 })
592
593 -- Key = player name
594 core.rollback_punch_callbacks = {}
595
596 core.register_on_punchnode(function(pos, node, puncher)
597         local name = puncher:get_player_name()
598         if core.rollback_punch_callbacks[name] then
599                 core.rollback_punch_callbacks[name](pos, node, puncher)
600                 core.rollback_punch_callbacks[name] = nil
601         end
602 end)
603
604 core.register_chatcommand("rollback_check", {
605         params = "[<range>] [<seconds>] [limit]",
606         description = "Check who has last touched a node or near it,"
607                         .. " max. <seconds> ago (default range=0,"
608                         .. " seconds=86400=24h, limit=5)",
609         privs = {rollback=true},
610         func = function(name, param)
611                 if not core.setting_getbool("enable_rollback_recording") then
612                         return false, "Rollback functions are disabled."
613                 end
614                 local range, seconds, limit =
615                         param:match("(%d+) *(%d*) *(%d*)")
616                 range = tonumber(range) or 0
617                 seconds = tonumber(seconds) or 86400
618                 limit = tonumber(limit) or 5
619                 if limit > 100 then
620                         return false, "That limit is too high!"
621                 end
622
623                 core.rollback_punch_callbacks[name] = function(pos, node, puncher)
624                         local name = puncher:get_player_name()
625                         core.chat_send_player(name, "Checking " .. core.pos_to_string(pos) .. "...")
626                         local actions = core.rollback_get_node_actions(pos, range, seconds, limit)
627                         if not actions then
628                                 core.chat_send_player(name, "Rollback functions are disabled")
629                                 return
630                         end
631                         local num_actions = #actions
632                         if num_actions == 0 then
633                                 core.chat_send_player(name, "Nobody has touched"
634                                                 .. " the specified location in "
635                                                 .. seconds .. " seconds")
636                                 return
637                         end
638                         local time = os.time()
639                         for i = num_actions, 1, -1 do
640                                 local action = actions[i]
641                                 core.chat_send_player(name,
642                                         ("%s %s %s -> %s %d seconds ago.")
643                                                 :format(
644                                                         core.pos_to_string(action.pos),
645                                                         action.actor,
646                                                         action.oldnode.name,
647                                                         action.newnode.name,
648                                                         time - action.time))
649                         end
650                 end
651
652                 return true, "Punch a node (range=" .. range .. ", seconds="
653                                 .. seconds .. "s, limit=" .. limit .. ")"
654         end,
655 })
656
657 core.register_chatcommand("rollback", {
658         params = "<player name> [<seconds>] | :<actor> [<seconds>]",
659         description = "revert actions of a player; default for <seconds> is 60",
660         privs = {rollback=true},
661         func = function(name, param)
662                 if not core.setting_getbool("enable_rollback_recording") then
663                         return false, "Rollback functions are disabled."
664                 end
665                 local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
666                 if not target_name then
667                         local player_name = nil
668                         player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
669                         if not player_name then
670                                 return false, "Invalid parameters. See /help rollback"
671                                                 .. " and /help rollback_check."
672                         end
673                         target_name = "player:"..player_name
674                 end
675                 seconds = tonumber(seconds) or 60
676                 core.chat_send_player(name, "Reverting actions of "
677                                 .. target_name .. " since "
678                                 .. seconds .. " seconds.")
679                 local success, log = core.rollback_revert_actions_by(
680                                 target_name, seconds)
681                 local response = ""
682                 if #log > 100 then
683                         response = "(log is too long to show)\n"
684                 else
685                         for _, line in pairs(log) do
686                                 response = response .. line .. "\n"
687                         end
688                 end
689                 response = response .. "Reverting actions "
690                                 .. (success and "succeeded." or "FAILED.")
691                 return success, response
692         end,
693 })
694
695 core.register_chatcommand("status", {
696         description = "Print server status",
697         func = function(name, param)
698                 return true, core.get_server_status()
699         end,
700 })
701
702 core.register_chatcommand("time", {
703         params = "<0..23>:<0..59> | <0..24000>",
704         description = "set time of day",
705         privs = {},
706         func = function(name, param)
707                 if param == "" then
708                         local current_time = math.floor(core.get_timeofday() * 1440)
709                         local minutes = current_time % 60
710                         local hour = (current_time - minutes) / 60
711                         return true, ("Current time is %d:%02d"):format(hour, minutes)
712                 end
713                 local player_privs = core.get_player_privs(name)
714                 if not player_privs.settime then
715                         return false, "You don't have permission to run this command " ..
716                                 "(missing privilege: settime)."
717                 end
718                 local hour, minute = param:match("^(%d+):(%d+)$")
719                 if not hour then
720                         local new_time = tonumber(param)
721                         if not new_time then
722                                 return false, "Invalid time."
723                         end
724                         -- Backward compatibility.
725                         core.set_timeofday((new_time % 24000) / 24000)
726                         core.log("action", name .. " sets time to " .. new_time)
727                         return true, "Time of day changed."
728                 end
729                 hour = tonumber(hour)
730                 minute = tonumber(minute)
731                 if hour < 0 or hour > 23 then
732                         return false, "Invalid hour (must be between 0 and 23 inclusive)."
733                 elseif minute < 0 or minute > 59 then
734                         return false, "Invalid minute (must be between 0 and 59 inclusive)."
735                 end
736                 core.set_timeofday((hour * 60 + minute) / 1440)
737                 core.log("action", ("%s sets time to %d:%02d"):format(name, hour, minute))
738                 return true, "Time of day changed."
739         end,
740 })
741
742 core.register_chatcommand("shutdown", {
743         description = "shutdown server",
744         privs = {server=true},
745         func = function(name, param)
746                 core.log("action", name .. " shuts down server")
747                 core.request_shutdown()
748                 core.chat_send_all("*** Server shutting down (operator request).")
749         end,
750 })
751
752 core.register_chatcommand("ban", {
753         params = "<name>",
754         description = "Ban IP of player",
755         privs = {ban=true},
756         func = function(name, param)
757                 if param == "" then
758                         return true, "Ban list: " .. core.get_ban_list()
759                 end
760                 if not core.get_player_by_name(param) then
761                         return false, "No such player."
762                 end
763                 if not core.ban_player(param) then
764                         return false, "Failed to ban player."
765                 end
766                 local desc = core.get_ban_description(param)
767                 core.log("action", name .. " bans " .. desc .. ".")
768                 return true, "Banned " .. desc .. "."
769         end,
770 })
771
772 core.register_chatcommand("unban", {
773         params = "<name/ip>",
774         description = "remove IP ban",
775         privs = {ban=true},
776         func = function(name, param)
777                 if not core.unban_player_or_ip(param) then
778                         return false, "Failed to unban player/IP."
779                 end
780                 core.log("action", name .. " unbans " .. param)
781                 return true, "Unbanned " .. param
782         end,
783 })
784
785 core.register_chatcommand("kick", {
786         params = "<name> [reason]",
787         description = "kick a player",
788         privs = {kick=true},
789         func = function(name, param)
790                 local tokick, reason = param:match("([^ ]+) (.+)")
791                 tokick = tokick or param
792                 if not core.kick_player(tokick, reason) then
793                         return false, "Failed to kick player " .. tokick
794                 end
795                 local log_reason = ""
796                 if reason then
797                         log_reason = " with reason \"" .. reason .. "\""
798                 end
799                 core.log("action", name .. " kicks " .. tokick .. log_reason)
800                 return true, "Kicked " .. tokick
801         end,
802 })
803
804 core.register_chatcommand("clearobjects", {
805         description = "clear all objects in world",
806         privs = {server=true},
807         func = function(name, param)
808                 core.log("action", name .. " clears all objects.")
809                 core.chat_send_all("Clearing all objects.  This may take long."
810                                 .. "  You may experience a timeout.  (by "
811                                 .. name .. ")")
812                 core.clear_objects()
813                 core.log("action", "Object clearing done.")
814                 core.chat_send_all("*** Cleared all objects.")
815         end,
816 })
817
818 core.register_chatcommand("msg", {
819         params = "<name> <message>",
820         description = "Send a private message",
821         privs = {shout=true},
822         func = function(name, param)
823                 local sendto, message = param:match("^(%S+)%s(.+)$")
824                 if not sendto then
825                         return false, "Invalid usage, see /help msg."
826                 end
827                 if not core.get_player_by_name(sendto) then
828                         return false, "The player " .. sendto
829                                         .. " is not online."
830                 end
831                 core.log("action", "PM from " .. name .. " to " .. sendto
832                                 .. ": " .. message)
833                 core.chat_send_player(sendto, "PM from " .. name .. ": "
834                                 .. message)
835                 return true, "Message sent."
836         end,
837 })
838
839 core.register_chatcommand("last-login", {
840         params = "[name]",
841         description = "Get the last login time of a player",
842         func = function(name, param)
843                 if param == "" then
844                         param = name
845                 end
846                 local pauth = core.get_auth_handler().get_auth(param)
847                 if pauth and pauth.last_login then
848                         -- Time in UTC, ISO 8601 format
849                         return true, "Last login time was " ..
850                                 os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login)
851                 end
852                 return false, "Last login time is unknown"
853         end,
854 })