1 -- Minetest: builtin/chatcommands.lua
4 -- Chat command handler
8 function core.register_chatcommand(cmd, def)
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
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",
23 description = "save mod profiling data to logfile " ..
24 "(depends on default loglevel)",
26 privs = { server=true }
30 core.register_on_chat_message(function(name, message)
31 local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
35 local cmd_def = core.chatcommands[cmd]
39 local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
41 core.set_last_run_mod(cmd_def.mod_origin)
42 local success, message = cmd_def.func(name, param)
44 core.chat_send_player(name, message)
47 core.chat_send_player(name, "You don't have permission"
48 .. " to run this command (missing privileges: "
49 .. table.concat(missing_privs, ", ") .. ")")
51 return true -- Handled chat message
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)
58 local args = str:split(" ")
60 if args[1] == "here" then
61 p1, p2 = core.get_player_radius_area(player_name, tonumber(args[2]))
63 return false, "Unable to get player " .. player_name .. " position"
66 p1, p2 = core.string_to_area(str)
68 return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
78 core.register_chatcommand("me", {
80 description = "chat action (eg. /me orders a pizza)",
82 func = function(name, param)
83 core.chat_send_all("* " .. name .. " " .. param)
87 core.register_chatcommand("help", {
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)
94 if def.params and def.params ~= "" then
95 msg = msg .. " " .. def.params
97 if def.description and def.description ~= "" then
98 msg = msg .. ": " .. def.description
105 for cmd, def in pairs(core.chatcommands) do
106 if core.check_player_privs(name, def.privs) then
107 table.insert(cmds, cmd)
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
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))
122 return true, "Available commands:\n"..table.concat(cmds, "\n")
123 elseif param == "privs" then
125 for priv, def in pairs(core.registered_privileges) do
126 table.insert(privs, priv .. ": " .. def.description)
129 return true, "Available privileges:\n"..table.concat(privs, "\n")
132 local def = core.chatcommands[cmd]
134 return false, "Command not available: "..cmd
136 return true, format_help_line(cmd, def)
142 core.register_chatcommand("privs", {
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), ' ')
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."
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."
166 local grantprivs = core.string_to_privs(grantprivstr)
167 if grantprivstr == "all" then
168 grantprivs = core.registered_privileges
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."
177 if not core.registered_privileges[priv] then
178 privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
182 if privs_unknown ~= "" then
183 return false, privs_unknown
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, ' '))
192 return true, "Privileges of " .. grantname .. ": "
193 .. core.privs_to_string(
194 core.get_player_privs(grantname), ' ')
197 core.register_chatcommand("revoke", {
198 params = "<name> <privilege>|all",
199 description = "Remove privilege from player",
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."
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."
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."
220 if revoke_priv_str == "all" then
223 for priv, _ in pairs(revoke_privs) do
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, ' '))
236 return true, "Privileges of " .. revoke_name .. ": "
237 .. core.privs_to_string(
238 core.get_player_privs(revoke_name), ' ')
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, "^([^ ]+) +(.+)$")
249 toname = param:match("^([^ ]+) *$")
253 return false, "Name field required"
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"
262 core.set_player_password(toname,
263 core.get_password_hash(toname,
266 act_str_pres = "sets"
268 if toname ~= name then
269 core.chat_send_player(toname, "Your password was "
270 .. act_str_past .. " by " .. name)
273 core.log("action", name .. " " .. act_str_pres
274 .. " password of " .. toname .. ".")
276 return true, "Password of player \"" .. toname .. "\" " .. act_str_past
280 core.register_chatcommand("clearpassword", {
282 description = "set empty password",
283 privs = {password=true},
284 func = function(name, param)
287 return false, "Name field required"
289 core.set_player_password(toname, '')
291 core.log("action", name .. " clears password of " .. toname .. ".")
293 return true, "Password of player \"" .. toname .. "\" cleared"
297 core.register_chatcommand("auth_reload", {
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.")
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)
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)
324 local def = core.registered_nodes[n.name]
325 if def and not def.walkable then
333 local teleportee = nil
335 p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
339 teleportee = core.get_player_by_name(name)
340 if teleportee and p.x and p.y and p.z then
342 return true, "Teleporting to "..core.pos_to_string(p)
345 local teleportee = nil
347 local target_name = nil
348 target_name = param:match("^([^ ]+)$")
349 teleportee = core.get_player_by_name(name)
351 local target = core.get_player_by_name(target_name)
356 if teleportee and p then
357 p = find_free_position_near(p)
359 return true, "Teleporting to " .. target_name
360 .. " at "..core.pos_to_string(p)
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)"
367 local teleportee = nil
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)
376 if teleportee and p.x and p.y and p.z then
378 return true, "Teleporting " .. teleportee_name
379 .. " to " .. core.pos_to_string(p)
382 local teleportee = 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)
391 local target = core.get_player_by_name(target_name)
396 if teleportee and p then
397 p = find_free_position_near(p)
399 return true, "Teleporting " .. teleportee_name
400 .. " to " .. target_name
401 .. " at " .. core.pos_to_string(p)
404 return false, 'Invalid parameters ("' .. param
405 .. '") or player not found (see /help teleport)'
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
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."
424 core.setting_set(setname, setvalue)
425 return true, setname .. " = " .. setvalue
427 local setname = string.match(param, "([^ ]+)")
429 local setvalue = core.setting_get(setname)
431 setvalue = "<not set>"
433 return true, setname .. " = " .. setvalue
435 return false, "Invalid parameters (see /help set)."
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)
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)
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)
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)
470 return false, "Failed to clear one or more blocks in area"
475 core.register_chatcommand("mods", {
477 description = "List mods installed on the server",
479 func = function(name, param)
480 return true, table.concat(core.get_modnames(), ", ")
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"
493 local receiverref = core.get_player_by_name(receiver)
494 if receiverref == nil then
495 return false, receiver .. " is not a known player"
497 local leftover = receiverref:get_inventory():add_item("main", itemstack)
499 if leftover:is_empty() then
501 elseif leftover:get_count() == itemstack:get_count() then
502 partiality = "could not be "
504 partiality = "partially "
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)
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)
520 core.register_chatcommand("give", {
521 params = "<name> <ItemString>",
522 description = "give item to player",
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"
529 return handle_give_command("/give", name, toname, itemstring)
533 core.register_chatcommand("giveme", {
534 params = "<ItemString>",
535 description = "give item to yourself",
537 func = function(name, param)
538 local itemstring = string.match(param, "(.+)$")
539 if not itemstring then
540 return false, "ItemString required"
542 return handle_give_command("/giveme", name, name, itemstring)
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"
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"
565 p = core.string_to_pos(p)
567 return false, "Invalid parameters ('" .. param .. "')"
571 core.add_entity(p, entityname)
572 return true, ("%q spawned."):format(entityname)
576 core.register_chatcommand("pulverize", {
578 description = "Destroy item in hand",
579 func = function(name, param)
580 local player = core.get_player_by_name(name)
582 core.log("error", "Unable to pulverize, no player.")
583 return false, "Unable to pulverize, no player."
585 if player:get_wielded_item():is_empty() then
586 return false, "Unable to pulverize, no item in hand."
588 player:set_wielded_item(nil)
589 return true, "An item was pulverized."
594 core.rollback_punch_callbacks = {}
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
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."
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
620 return false, "That limit is too high!"
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)
628 core.chat_send_player(name, "Rollback functions are disabled")
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")
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.")
644 core.pos_to_string(action.pos),
652 return true, "Punch a node (range=" .. range .. ", seconds="
653 .. seconds .. "s, limit=" .. limit .. ")"
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."
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."
673 target_name = "player:"..player_name
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)
683 response = "(log is too long to show)\n"
685 for _, line in pairs(log) do
686 response = response .. line .. "\n"
689 response = response .. "Reverting actions "
690 .. (success and "succeeded." or "FAILED.")
691 return success, response
695 core.register_chatcommand("status", {
696 description = "Print server status",
697 func = function(name, param)
698 return true, core.get_server_status()
702 core.register_chatcommand("time", {
703 params = "<0..23>:<0..59> | <0..24000>",
704 description = "set time of day",
706 func = function(name, param)
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)
713 local player_privs = minetest.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)."
718 local hour, minute = param:match("^(%d+):(%d+)$")
720 local new_time = tonumber(param)
722 return false, "Invalid time."
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."
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)."
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."
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).")
752 core.register_chatcommand("ban", {
754 description = "Ban IP of player",
756 func = function(name, param)
758 return true, "Ban list: " .. core.get_ban_list()
760 if not core.get_player_by_name(param) then
761 return false, "No such player."
763 if not core.ban_player(param) then
764 return false, "Failed to ban player."
766 local desc = core.get_ban_description(param)
767 core.log("action", name .. " bans " .. desc .. ".")
768 return true, "Banned " .. desc .. "."
772 core.register_chatcommand("unban", {
773 params = "<name/ip>",
774 description = "remove IP ban",
776 func = function(name, param)
777 if not core.unban_player_or_ip(param) then
778 return false, "Failed to unban player/IP."
780 core.log("action", name .. " unbans " .. param)
781 return true, "Unbanned " .. param
785 core.register_chatcommand("kick", {
786 params = "<name> [reason]",
787 description = "kick a player",
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
795 local log_reason = ""
797 log_reason = " with reason \"" .. reason .. "\""
799 core.log("action", name .. " kicks " .. tokick .. log_reason)
800 return true, "Kicked " .. tokick
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 "
813 core.log("action", "Object clearing done.")
814 core.chat_send_all("*** Cleared all objects.")
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(.+)$")
825 return false, "Invalid usage, see /help msg."
827 if not core.get_player_by_name(sendto) then
828 return false, "The player " .. sendto
831 core.log("action", "PM from " .. name .. " to " .. sendto
833 core.chat_send_player(sendto, "PM from " .. name .. ": "
835 return true, "Message sent."
839 core.register_chatcommand("last-login", {
841 description = "Get the last login time of a player",
842 func = function(name, param)
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)
852 return false, "Last login time is unknown"