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 core.chatcommands[cmd] = def
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",
22 description = "save mod profiling data to logfile " ..
23 "(depends on default loglevel)",
25 privs = { server=true }
29 core.register_on_chat_message(function(name, message)
30 local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
34 local cmd_def = core.chatcommands[cmd]
38 local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
40 local success, message = cmd_def.func(name, param)
42 core.chat_send_player(name, message)
45 core.chat_send_player(name, "You don't have permission"
46 .. " to run this command (missing privileges: "
47 .. table.concat(missing_privs, ", ") .. ")")
49 return true -- Handled chat message
55 core.register_chatcommand("me", {
57 description = "chat action (eg. /me orders a pizza)",
59 func = function(name, param)
60 core.chat_send_all("* " .. name .. " " .. param)
64 core.register_chatcommand("help", {
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)
71 if def.params and def.params ~= "" then
72 msg = msg .. " " .. def.params
74 if def.description and def.description ~= "" then
75 msg = msg .. ": " .. def.description
82 for cmd, def in pairs(core.chatcommands) do
83 if core.check_player_privs(name, def.privs) then
84 table.insert(cmds, cmd)
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
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))
99 return true, "Available commands:\n"..table.concat(cmds, "\n")
100 elseif param == "privs" then
102 for priv, def in pairs(core.registered_privileges) do
103 table.insert(privs, priv .. ": " .. def.description)
106 return true, "Available privileges:\n"..table.concat(privs, "\n")
109 local def = core.chatcommands[cmd]
111 return false, "Command not available: "..cmd
113 return true, format_help_line(cmd, def)
119 core.register_chatcommand("privs", {
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), ' ')
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."
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."
143 local grantprivs = core.string_to_privs(grantprivstr)
144 if grantprivstr == "all" then
145 grantprivs = core.registered_privileges
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."
154 if not core.registered_privileges[priv] then
155 privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
159 if privs_unknown ~= "" then
160 return false, privs_unknown
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, ' '))
169 return true, "Privileges of " .. grantname .. ": "
170 .. core.privs_to_string(
171 core.get_player_privs(grantname), ' ')
174 core.register_chatcommand("revoke", {
175 params = "<name> <privilege>|all",
176 description = "Remove privilege from player",
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."
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."
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
193 not core.check_player_privs(name, {privs=true}) then
194 return false, "Your privileges are insufficient."
197 if revoke_priv_str == "all" then
200 for priv, _ in pairs(revoke_privs) do
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, ' '))
213 return true, "Privileges of " .. revoke_name .. ": "
214 .. core.privs_to_string(
215 core.get_player_privs(revoke_name), ' ')
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, "^([^ ]+) +(.+)$")
226 toname = param:match("^([^ ]+) *$")
230 return false, "Name field required"
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"
239 core.set_player_password(toname,
240 core.get_password_hash(toname,
243 act_str_pres = "sets"
245 if toname ~= name then
246 core.chat_send_player(toname, "Your password was "
247 .. act_str_past .. " by " .. name)
250 core.log("action", name .. " " .. act_str_pres
251 .. " password of " .. toname .. ".")
253 return true, "Password of player \"" .. toname .. "\" " .. act_str_past
257 core.register_chatcommand("clearpassword", {
259 description = "set empty password",
260 privs = {password=true},
261 func = function(name, param)
264 return false, "Name field required"
266 core.set_player_password(toname, '')
268 core.log("action", name .. " clears password of " .. toname .. ".")
270 return true, "Password of player \"" .. toname .. "\" cleared"
274 core.register_chatcommand("auth_reload", {
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.")
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)
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)
301 local def = core.registered_nodes[n.name]
302 if def and not def.walkable then
310 local teleportee = nil
312 p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
316 teleportee = core.get_player_by_name(name)
317 if teleportee and p.x and p.y and p.z then
319 return true, "Teleporting to "..core.pos_to_string(p)
322 local teleportee = nil
324 local target_name = nil
325 target_name = param:match("^([^ ]+)$")
326 teleportee = core.get_player_by_name(name)
328 local target = core.get_player_by_name(target_name)
333 if teleportee and p then
334 p = find_free_position_near(p)
336 return true, "Teleporting to " .. target_name
337 .. " at "..core.pos_to_string(p)
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)"
344 local teleportee = nil
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)
353 if teleportee and p.x and p.y and p.z then
355 return true, "Teleporting " .. teleportee_name
356 .. " to " .. core.pos_to_string(p)
359 local teleportee = 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)
368 local target = core.get_player_by_name(target_name)
373 if teleportee and p then
374 p = find_free_position_near(p)
376 return true, "Teleporting " .. teleportee_name
377 .. " to " .. target_name
378 .. " at " .. core.pos_to_string(p)
381 return false, 'Invalid parameters ("' .. param
382 .. '") or player not found (see /help teleport)'
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
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."
401 core.setting_set(setname, setvalue)
402 return true, setname .. " = " .. setvalue
404 local setname = string.match(param, "([^ ]+)")
406 local setvalue = core.setting_get(setname)
408 setvalue = "<not set>"
410 return true, setname .. " = " .. setvalue
412 return false, "Invalid parameters (see /help set)."
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)
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"
434 local radius = tonumber(args[2]) or 0
435 p1 = vector.add(p1, radius)
436 p2 = vector.subtract(p2, radius)
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)"
444 p1 = core.string_to_pos(pos1 .. ")")
445 p2 = core.string_to_pos("(" .. pos2)
447 if p1 == nil or p2 == nil then
448 return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
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)
456 return false, "Failed to clear one or more blocks in area"
461 core.register_chatcommand("mods", {
463 description = "List mods installed on the server",
465 func = function(name, param)
466 return true, table.concat(core.get_modnames(), ", ")
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"
479 local receiverref = core.get_player_by_name(receiver)
480 if receiverref == nil then
481 return false, receiver .. " is not a known player"
483 local leftover = receiverref:get_inventory():add_item("main", itemstack)
485 if leftover:is_empty() then
487 elseif leftover:get_count() == itemstack:get_count() then
488 partiality = "could not be "
490 partiality = "partially "
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)
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)
506 core.register_chatcommand("give", {
507 params = "<name> <ItemString>",
508 description = "give item to player",
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"
515 return handle_give_command("/give", name, toname, itemstring)
519 core.register_chatcommand("giveme", {
520 params = "<ItemString>",
521 description = "give item to yourself",
523 func = function(name, param)
524 local itemstring = string.match(param, "(.+)$")
525 if not itemstring then
526 return false, "ItemString required"
528 return handle_give_command("/giveme", name, name, itemstring)
532 core.register_chatcommand("spawnentity", {
533 params = "<EntityName> [<X>,<Y>,<Z>]",
534 description = "Spawn entity at given (or your) position",
535 privs = {give=true, interact=true},
536 func = function(name, param)
537 local entityname, p = string.match(param, "^([^ ]+) *(.*)$")
538 if not entityname then
539 return false, "EntityName required"
541 core.log("action", ("%s invokes /spawnentity, entityname=%q")
542 :format(name, 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"
551 p = core.string_to_pos(p)
553 return false, "Invalid parameters ('" .. param .. "')"
557 core.add_entity(p, entityname)
558 return true, ("%q spawned."):format(entityname)
562 core.register_chatcommand("pulverize", {
564 description = "Destroy item in hand",
565 func = function(name, param)
566 local player = core.get_player_by_name(name)
568 core.log("error", "Unable to pulverize, no player.")
569 return false, "Unable to pulverize, no player."
571 if player:get_wielded_item():is_empty() then
572 return false, "Unable to pulverize, no item in hand."
574 player:set_wielded_item(nil)
575 return true, "An item was pulverized."
580 core.rollback_punch_callbacks = {}
582 core.register_on_punchnode(function(pos, node, puncher)
583 local name = puncher:get_player_name()
584 if core.rollback_punch_callbacks[name] then
585 core.rollback_punch_callbacks[name](pos, node, puncher)
586 core.rollback_punch_callbacks[name] = nil
590 core.register_chatcommand("rollback_check", {
591 params = "[<range>] [<seconds>] [limit]",
592 description = "Check who has last touched a node or near it,"
593 .. " max. <seconds> ago (default range=0,"
594 .. " seconds=86400=24h, limit=5)",
595 privs = {rollback=true},
596 func = function(name, param)
597 if not core.setting_getbool("enable_rollback_recording") then
598 return false, "Rollback functions are disabled."
600 local range, seconds, limit =
601 param:match("(%d+) *(%d*) *(%d*)")
602 range = tonumber(range) or 0
603 seconds = tonumber(seconds) or 86400
604 limit = tonumber(limit) or 5
606 return false, "That limit is too high!"
609 core.rollback_punch_callbacks[name] = function(pos, node, puncher)
610 local name = puncher:get_player_name()
611 core.chat_send_player(name, "Checking " .. core.pos_to_string(pos) .. "...")
612 local actions = core.rollback_get_node_actions(pos, range, seconds, limit)
614 core.chat_send_player(name, "Rollback functions are disabled")
617 local num_actions = #actions
618 if num_actions == 0 then
619 core.chat_send_player(name, "Nobody has touched"
620 .. " the specified location in "
621 .. seconds .. " seconds")
624 local time = os.time()
625 for i = num_actions, 1, -1 do
626 local action = actions[i]
627 core.chat_send_player(name,
628 ("%s %s %s -> %s %d seconds ago.")
630 core.pos_to_string(action.pos),
638 return true, "Punch a node (range=" .. range .. ", seconds="
639 .. seconds .. "s, limit=" .. limit .. ")"
643 core.register_chatcommand("rollback", {
644 params = "<player name> [<seconds>] | :<actor> [<seconds>]",
645 description = "revert actions of a player; default for <seconds> is 60",
646 privs = {rollback=true},
647 func = function(name, param)
648 if not core.setting_getbool("enable_rollback_recording") then
649 return false, "Rollback functions are disabled."
651 local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
652 if not target_name then
653 local player_name = nil
654 player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
655 if not player_name then
656 return false, "Invalid parameters. See /help rollback"
657 .. " and /help rollback_check."
659 target_name = "player:"..player_name
661 seconds = tonumber(seconds) or 60
662 core.chat_send_player(name, "Reverting actions of "
663 .. target_name .. " since "
664 .. seconds .. " seconds.")
665 local success, log = core.rollback_revert_actions_by(
666 target_name, seconds)
669 response = "(log is too long to show)\n"
671 for _, line in pairs(log) do
672 response = response .. line .. "\n"
675 response = response .. "Reverting actions "
676 .. (success and "succeeded." or "FAILED.")
677 return success, response
681 core.register_chatcommand("status", {
682 description = "Print server status",
683 func = function(name, param)
684 return true, core.get_server_status()
688 core.register_chatcommand("time", {
689 params = "<0..23>:<0..59> | <0..24000>",
690 description = "set time of day",
692 func = function(name, param)
694 local current_time = math.floor(core.get_timeofday() * 1440)
695 local minutes = current_time % 60
696 local hour = (current_time - minutes) / 60
697 return true, ("Current time is %d:%02d"):format(hour, minutes)
699 local player_privs = minetest.get_player_privs(name)
700 if not player_privs.settime then
701 return false, "You don't have permission to run this command " ..
702 "(missing privilege: settime)."
704 local hour, minute = param:match("^(%d+):(%d+)$")
706 local new_time = tonumber(param)
708 return false, "Invalid time."
710 -- Backward compatibility.
711 core.set_timeofday((new_time % 24000) / 24000)
712 core.log("action", name .. " sets time to " .. new_time)
713 return true, "Time of day changed."
715 hour = tonumber(hour)
716 minute = tonumber(minute)
717 if hour < 0 or hour > 23 then
718 return false, "Invalid hour (must be between 0 and 23 inclusive)."
719 elseif minute < 0 or minute > 59 then
720 return false, "Invalid minute (must be between 0 and 59 inclusive)."
722 core.set_timeofday((hour * 60 + minute) / 1440)
723 core.log("action", ("%s sets time to %d:%02d"):format(name, hour, minute))
724 return true, "Time of day changed."
728 core.register_chatcommand("shutdown", {
729 description = "shutdown server",
730 privs = {server=true},
731 func = function(name, param)
732 core.log("action", name .. " shuts down server")
733 core.request_shutdown()
734 core.chat_send_all("*** Server shutting down (operator request).")
738 core.register_chatcommand("ban", {
740 description = "Ban IP of player",
742 func = function(name, param)
744 return true, "Ban list: " .. core.get_ban_list()
746 if not core.get_player_by_name(param) then
747 return false, "No such player."
749 if not core.ban_player(param) then
750 return false, "Failed to ban player."
752 local desc = core.get_ban_description(param)
753 core.log("action", name .. " bans " .. desc .. ".")
754 return true, "Banned " .. desc .. "."
758 core.register_chatcommand("unban", {
759 params = "<name/ip>",
760 description = "remove IP ban",
762 func = function(name, param)
763 if not core.unban_player_or_ip(param) then
764 return false, "Failed to unban player/IP."
766 core.log("action", name .. " unbans " .. param)
767 return true, "Unbanned " .. param
771 core.register_chatcommand("kick", {
772 params = "<name> [reason]",
773 description = "kick a player",
775 func = function(name, param)
776 local tokick, reason = param:match("([^ ]+) (.+)")
777 tokick = tokick or param
778 if not core.kick_player(tokick, reason) then
779 return false, "Failed to kick player " .. tokick
781 local log_reason = ""
783 log_reason = " with reason \"" .. reason .. "\""
785 core.log("action", name .. " kicks " .. tokick .. log_reason)
786 return true, "Kicked " .. tokick
790 core.register_chatcommand("clearobjects", {
791 description = "clear all objects in world",
792 privs = {server=true},
793 func = function(name, param)
794 core.log("action", name .. " clears all objects.")
795 core.chat_send_all("Clearing all objects. This may take long."
796 .. " You may experience a timeout. (by "
799 core.log("action", "Object clearing done.")
800 core.chat_send_all("*** Cleared all objects.")
804 core.register_chatcommand("msg", {
805 params = "<name> <message>",
806 description = "Send a private message",
807 privs = {shout=true},
808 func = function(name, param)
809 local sendto, message = param:match("^(%S+)%s(.+)$")
811 return false, "Invalid usage, see /help msg."
813 if not core.get_player_by_name(sendto) then
814 return false, "The player " .. sendto
817 core.log("action", "PM from " .. name .. " to " .. sendto
819 core.chat_send_player(sendto, "PM from " .. name .. ": "
821 return true, "Message sent."
825 core.register_chatcommand("last-login", {
827 description = "Get the last login time of a player",
828 func = function(name, param)
832 local pauth = core.get_auth_handler().get_auth(param)
833 if pauth and pauth.last_login then
834 -- Time in UTC, ISO 8601 format
835 return true, "Last login time was " ..
836 os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login)
838 return false, "Last login time is unknown"