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"
233 if not raw_password then
234 core.set_player_password(toname, "")
237 core.set_player_password(toname,
238 core.get_password_hash(toname,
242 if toname ~= name then
243 core.chat_send_player(toname, "Your password was "
244 .. actstr .. " by " .. name)
246 return true, "Password of player \"" .. toname .. "\" " .. actstr
250 core.register_chatcommand("clearpassword", {
252 description = "set empty password",
253 privs = {password=true},
254 func = function(name, param)
257 return false, "Name field required"
259 core.set_player_password(toname, '')
260 return true, "Password of player \"" .. toname .. "\" cleared"
264 core.register_chatcommand("auth_reload", {
266 description = "reload authentication data",
267 privs = {server=true},
268 func = function(name, param)
269 local done = core.auth_reload()
270 return done, (done and "Done." or "Failed.")
274 core.register_chatcommand("teleport", {
275 params = "<X>,<Y>,<Z> | <to_name> | <name> <X>,<Y>,<Z> | <name> <to_name>",
276 description = "teleport to given position",
277 privs = {teleport=true},
278 func = function(name, param)
279 -- Returns (pos, true) if found, otherwise (pos, false)
280 local function find_free_position_near(pos)
287 for _, d in ipairs(tries) do
288 local p = {x = pos.x+d.x, y = pos.y+d.y, z = pos.z+d.z}
289 local n = core.get_node_or_nil(p)
291 local def = core.registered_nodes[n.name]
292 if def and not def.walkable then
300 local teleportee = nil
302 p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
306 teleportee = core.get_player_by_name(name)
307 if teleportee and p.x and p.y and p.z then
309 return true, "Teleporting to "..core.pos_to_string(p)
312 local teleportee = nil
314 local target_name = nil
315 target_name = param:match("^([^ ]+)$")
316 teleportee = core.get_player_by_name(name)
318 local target = core.get_player_by_name(target_name)
323 if teleportee and p then
324 p = find_free_position_near(p)
326 return true, "Teleporting to " .. target_name
327 .. " at "..core.pos_to_string(p)
330 if core.check_player_privs(name, {bring=true}) then
331 local teleportee = nil
333 local teleportee_name = nil
334 teleportee_name, p.x, p.y, p.z = param:match(
335 "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
336 p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
337 if teleportee_name then
338 teleportee = core.get_player_by_name(teleportee_name)
340 if teleportee and p.x and p.y and p.z then
342 return true, "Teleporting " .. teleportee_name
343 .. " to " .. core.pos_to_string(p)
346 local teleportee = nil
348 local teleportee_name = nil
349 local target_name = nil
350 teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
351 if teleportee_name then
352 teleportee = core.get_player_by_name(teleportee_name)
355 local target = core.get_player_by_name(target_name)
360 if teleportee and p then
361 p = find_free_position_near(p)
363 return true, "Teleporting " .. teleportee_name
364 .. " to " .. target_name
365 .. " at " .. core.pos_to_string(p)
369 return false, 'Invalid parameters ("' .. param
370 .. '") or player not found (see /help teleport)'
374 core.register_chatcommand("set", {
375 params = "[-n] <name> <value> | <name>",
376 description = "set or read server configuration setting",
377 privs = {server=true},
378 func = function(name, param)
379 local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
380 if arg and arg == "-n" and setname and setvalue then
381 core.setting_set(setname, setvalue)
382 return true, setname .. " = " .. setvalue
384 local setname, setvalue = string.match(param, "([^ ]+) (.+)")
385 if setname and setvalue then
386 if not core.setting_get(setname) then
387 return false, "Failed. Use '/set -n <name> <value>' to create a new setting."
389 core.setting_set(setname, setvalue)
390 return true, setname .. " = " .. setvalue
392 local setname = string.match(param, "([^ ]+)")
394 local setvalue = core.setting_get(setname)
396 setvalue = "<not set>"
398 return true, setname .. " = " .. setvalue
400 return false, "Invalid parameters (see /help set)."
404 core.register_chatcommand("mods", {
406 description = "List mods installed on the server",
408 func = function(name, param)
409 return true, table.concat(core.get_modnames(), ", ")
413 local function handle_give_command(cmd, giver, receiver, stackstring)
414 core.log("action", giver .. " invoked " .. cmd
415 .. ', stackstring="' .. stackstring .. '"')
416 local itemstack = ItemStack(stackstring)
417 if itemstack:is_empty() then
418 return false, "Cannot give an empty item"
419 elseif not itemstack:is_known() then
420 return false, "Cannot give an unknown item"
422 local receiverref = core.get_player_by_name(receiver)
423 if receiverref == nil then
424 return false, receiver .. " is not a known player"
426 local leftover = receiverref:get_inventory():add_item("main", itemstack)
427 if leftover:is_empty() then
429 elseif leftover:get_count() == itemstack:get_count() then
430 partiality = "could not be "
432 partiality = "partially "
434 -- The actual item stack string may be different from what the "giver"
435 -- entered (e.g. big numbers are always interpreted as 2^16-1).
436 stackstring = itemstack:to_string()
437 if giver == receiver then
438 return true, ("%q %sadded to inventory.")
439 :format(stackstring, partiality)
441 core.chat_send_player(receiver, ("%q %sadded to inventory.")
442 :format(stackstring, partiality))
443 return true, ("%q %sadded to %s's inventory.")
444 :format(stackstring, partiality, receiver)
448 core.register_chatcommand("give", {
449 params = "<name> <ItemString>",
450 description = "give item to player",
452 func = function(name, param)
453 local toname, itemstring = string.match(param, "^([^ ]+) +(.+)$")
454 if not toname or not itemstring then
455 return false, "Name and ItemString required"
457 return handle_give_command("/give", name, toname, itemstring)
461 core.register_chatcommand("giveme", {
462 params = "<ItemString>",
463 description = "give item to yourself",
465 func = function(name, param)
466 local itemstring = string.match(param, "(.+)$")
467 if not itemstring then
468 return false, "ItemString required"
470 return handle_give_command("/giveme", name, name, itemstring)
474 core.register_chatcommand("spawnentity", {
475 params = "<EntityName>",
476 description = "Spawn entity at your position",
477 privs = {give=true, interact=true},
478 func = function(name, param)
479 local entityname = string.match(param, "(.+)$")
480 if not entityname then
481 return false, "EntityName required"
483 core.log("action", ("/spawnentity invoked, entityname=%q")
485 local player = core.get_player_by_name(name)
486 if player == nil then
487 core.log("error", "Unable to spawn entity, player is nil")
488 return false, "Unable to spawn entity, player is nil"
490 local p = player:getpos()
492 core.add_entity(p, entityname)
493 return true, ("%q spawned."):format(entityname)
497 core.register_chatcommand("pulverize", {
499 description = "Destroy item in hand",
500 func = function(name, param)
501 local player = core.get_player_by_name(name)
503 core.log("error", "Unable to pulverize, no player.")
504 return false, "Unable to pulverize, no player."
506 if player:get_wielded_item():is_empty() then
507 return false, "Unable to pulverize, no item in hand."
509 player:set_wielded_item(nil)
510 return true, "An item was pulverized."
515 core.rollback_punch_callbacks = {}
517 core.register_on_punchnode(function(pos, node, puncher)
518 local name = puncher:get_player_name()
519 if core.rollback_punch_callbacks[name] then
520 core.rollback_punch_callbacks[name](pos, node, puncher)
521 core.rollback_punch_callbacks[name] = nil
525 core.register_chatcommand("rollback_check", {
526 params = "[<range>] [<seconds>] [limit]",
527 description = "Check who has last touched a node or near it,"
528 .. " max. <seconds> ago (default range=0,"
529 .. " seconds=86400=24h, limit=5)",
530 privs = {rollback=true},
531 func = function(name, param)
532 local range, seconds, limit =
533 param:match("(%d+) *(%d*) *(%d*)")
534 range = tonumber(range) or 0
535 seconds = tonumber(seconds) or 86400
536 limit = tonumber(limit) or 5
538 return false, "That limit is too high!"
541 core.rollback_punch_callbacks[name] = function(pos, node, puncher)
542 local name = puncher:get_player_name()
543 core.chat_send_player(name, "Checking " .. core.pos_to_string(pos) .. "...")
544 local actions = core.rollback_get_node_actions(pos, range, seconds, limit)
545 local num_actions = #actions
546 if num_actions == 0 then
547 core.chat_send_player(name, "Nobody has touched"
548 .. " the specified location in "
549 .. seconds .. " seconds")
552 local time = os.time()
553 for i = num_actions, 1, -1 do
554 local action = actions[i]
555 core.chat_send_player(name,
556 ("%s %s %s -> %s %d seconds ago.")
558 core.pos_to_string(action.pos),
566 return true, "Punch a node (range=" .. range .. ", seconds="
567 .. seconds .. "s, limit=" .. limit .. ")"
571 core.register_chatcommand("rollback", {
572 params = "<player name> [<seconds>] | :<actor> [<seconds>]",
573 description = "revert actions of a player; default for <seconds> is 60",
574 privs = {rollback=true},
575 func = function(name, param)
576 local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
577 if not target_name then
578 local player_name = nil
579 player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
580 if not player_name then
581 return false, "Invalid parameters. See /help rollback"
582 .. " and /help rollback_check."
584 target_name = "player:"..player_name
586 seconds = tonumber(seconds) or 60
587 core.chat_send_player(name, "Reverting actions of "
588 .. target_name .. " since "
589 .. seconds .. " seconds.")
590 local success, log = core.rollback_revert_actions_by(
591 target_name, seconds)
594 response = "(log is too long to show)\n"
596 for _, line in pairs(log) do
597 response = response .. line .. "\n"
600 response = response .. "Reverting actions "
601 .. (success and "succeeded." or "FAILED.")
602 return success, response
606 core.register_chatcommand("status", {
607 description = "Print server status",
608 func = function(name, param)
609 return true, core.get_server_status()
613 core.register_chatcommand("time", {
614 params = "<0...24000>",
615 description = "set time of day",
616 privs = {settime=true},
617 func = function(name, param)
619 return false, "Missing time."
621 local newtime = tonumber(param)
622 if newtime == nil then
623 return false, "Invalid time."
625 core.set_timeofday((newtime % 24000) / 24000)
626 core.log("action", name .. " sets time " .. newtime)
627 return true, "Time of day changed."
631 core.register_chatcommand("shutdown", {
632 description = "shutdown server",
633 privs = {server=true},
634 func = function(name, param)
635 core.log("action", name .. " shuts down server")
636 core.request_shutdown()
637 core.chat_send_all("*** Server shutting down (operator request).")
641 core.register_chatcommand("ban", {
643 description = "Ban IP of player",
645 func = function(name, param)
647 return true, "Ban list: " .. core.get_ban_list()
649 if not core.get_player_by_name(param) then
650 return false, "No such player."
652 if not core.ban_player(param) then
653 return false, "Failed to ban player."
655 local desc = core.get_ban_description(param)
656 core.log("action", name .. " bans " .. desc .. ".")
657 return true, "Banned " .. desc .. "."
661 core.register_chatcommand("unban", {
662 params = "<name/ip>",
663 description = "remove IP ban",
665 func = function(name, param)
666 if not core.unban_player_or_ip(param) then
667 return false, "Failed to unban player/IP."
669 core.log("action", name .. " unbans " .. param)
670 return true, "Unbanned " .. param
674 core.register_chatcommand("kick", {
675 params = "<name> [reason]",
676 description = "kick a player",
678 func = function(name, param)
679 local tokick, reason = param:match("([^ ]+) (.+)")
680 tokick = tokick or param
681 if not core.kick_player(tokick, reason) then
682 return false, "Failed to kick player " .. tokick
684 core.log("action", name .. " kicked " .. tokick)
685 return true, "Kicked " .. tokick
689 core.register_chatcommand("clearobjects", {
690 description = "clear all objects in world",
691 privs = {server=true},
692 func = function(name, param)
693 core.log("action", name .. " clears all objects.")
694 core.chat_send_all("Clearing all objects. This may take long."
695 .. " You may experience a timeout. (by "
698 core.log("action", "Object clearing done.")
699 core.chat_send_all("*** Cleared all objects.")
703 core.register_chatcommand("msg", {
704 params = "<name> <message>",
705 description = "Send a private message",
706 privs = {shout=true},
707 func = function(name, param)
708 local sendto, message = param:match("^(%S+)%s(.+)$")
710 return false, "Invalid usage, see /help msg."
712 if not core.get_player_by_name(sendto) then
713 return false, "The player " .. sendto
716 core.log("action", "PM from " .. name .. " to " .. sendto
718 core.chat_send_player(sendto, "PM from " .. name .. ": "
720 return true, "Message sent."