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 priv ~= "interact_extra" 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>",
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"
541 core.log("action", ("/spawnentity invoked, entityname=%q")
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"
548 local p = player:getpos()
550 core.add_entity(p, entityname)
551 return true, ("%q spawned."):format(entityname)
555 core.register_chatcommand("pulverize", {
557 description = "Destroy item in hand",
558 func = function(name, param)
559 local player = core.get_player_by_name(name)
561 core.log("error", "Unable to pulverize, no player.")
562 return false, "Unable to pulverize, no player."
564 if player:get_wielded_item():is_empty() then
565 return false, "Unable to pulverize, no item in hand."
567 player:set_wielded_item(nil)
568 return true, "An item was pulverized."
573 core.rollback_punch_callbacks = {}
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
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."
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
599 return false, "That limit is too high!"
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)
607 core.chat_send_player(name, "Rollback functions are disabled")
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")
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.")
623 core.pos_to_string(action.pos),
631 return true, "Punch a node (range=" .. range .. ", seconds="
632 .. seconds .. "s, limit=" .. limit .. ")"
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."
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."
652 target_name = "player:"..player_name
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)
662 response = "(log is too long to show)\n"
664 for _, line in pairs(log) do
665 response = response .. line .. "\n"
668 response = response .. "Reverting actions "
669 .. (success and "succeeded." or "FAILED.")
670 return success, response
674 core.register_chatcommand("status", {
675 description = "Print server status",
676 func = function(name, param)
677 return true, core.get_server_status()
681 core.register_chatcommand("time", {
682 params = "<0..23>:<0..59> | <0..24000>",
683 description = "set time of day",
685 func = function(name, param)
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)
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)."
697 local hour, minute = param:match("^(%d+):(%d+)$")
699 local new_time = tonumber(param)
701 return false, "Invalid time."
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."
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)."
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."
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).")
731 core.register_chatcommand("ban", {
733 description = "Ban IP of player",
735 func = function(name, param)
737 return true, "Ban list: " .. core.get_ban_list()
739 if not core.get_player_by_name(param) then
740 return false, "No such player."
742 if not core.ban_player(param) then
743 return false, "Failed to ban player."
745 local desc = core.get_ban_description(param)
746 core.log("action", name .. " bans " .. desc .. ".")
747 return true, "Banned " .. desc .. "."
751 core.register_chatcommand("unban", {
752 params = "<name/ip>",
753 description = "remove IP ban",
755 func = function(name, param)
756 if not core.unban_player_or_ip(param) then
757 return false, "Failed to unban player/IP."
759 core.log("action", name .. " unbans " .. param)
760 return true, "Unbanned " .. param
764 core.register_chatcommand("kick", {
765 params = "<name> [reason]",
766 description = "kick a player",
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
774 local log_reason = ""
776 log_reason = " with reason \"" .. reason .. "\""
778 core.log("action", name .. " kicks " .. tokick .. log_reason)
779 return true, "Kicked " .. tokick
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 "
792 core.log("action", "Object clearing done.")
793 core.chat_send_all("*** Cleared all objects.")
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(.+)$")
804 return false, "Invalid usage, see /help msg."
806 if not core.get_player_by_name(sendto) then
807 return false, "The player " .. sendto
810 core.log("action", "PM from " .. name .. " to " .. sendto
812 core.chat_send_player(sendto, "PM from " .. name .. ": "
814 return true, "Message sent."
818 core.register_chatcommand("last-login", {
820 description = "Get the last login time of a player",
821 func = function(name, param)
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)
831 return false, "Last login time is unknown"