2017-01-22 09:05:09 +01:00
|
|
|
-- Minetest: builtin/common/chatcommands.lua
|
|
|
|
|
2021-03-05 16:27:33 +01:00
|
|
|
-- For server-side translations (if INIT == "game")
|
|
|
|
-- Otherwise, use core.gettext
|
|
|
|
local S = core.get_translator("__builtin")
|
|
|
|
|
2017-01-22 09:05:09 +01:00
|
|
|
core.registered_chatcommands = {}
|
|
|
|
|
2021-04-03 22:51:09 +02:00
|
|
|
-- Interpret the parameters of a command, separating options and arguments.
|
2021-12-01 20:22:46 +01:00
|
|
|
-- Input: command, param
|
|
|
|
-- command: name of command
|
|
|
|
-- param: parameters of command
|
2021-04-03 22:51:09 +02:00
|
|
|
-- Returns: opts, args
|
|
|
|
-- opts is a string of option letters, or false on error
|
|
|
|
-- args is an array with the non-option arguments in order, or an error message
|
|
|
|
-- Example: for this command line:
|
|
|
|
-- /command a b -cd e f -g
|
|
|
|
-- the function would receive:
|
|
|
|
-- a b -cd e f -g
|
|
|
|
-- and it would return:
|
|
|
|
-- "cdg", {"a", "b", "e", "f"}
|
|
|
|
-- Negative numbers are taken as arguments. Long options (--option) are
|
|
|
|
-- currently rejected as reserved.
|
2021-12-01 20:22:46 +01:00
|
|
|
local function getopts(command, param)
|
2021-04-03 22:51:09 +02:00
|
|
|
local opts = ""
|
|
|
|
local args = {}
|
|
|
|
for match in param:gmatch("%S+") do
|
|
|
|
if match:byte(1) == 45 then -- 45 = '-'
|
|
|
|
local second = match:byte(2)
|
|
|
|
if second == 45 then
|
2021-12-01 20:22:46 +01:00
|
|
|
return false, S("Invalid parameters (see /help @1).", command)
|
2021-04-03 22:51:09 +02:00
|
|
|
elseif second and (second < 48 or second > 57) then -- 48 = '0', 57 = '9'
|
|
|
|
opts = opts .. match:sub(2)
|
|
|
|
else
|
|
|
|
-- numeric, add it to args
|
|
|
|
args[#args + 1] = match
|
|
|
|
end
|
|
|
|
else
|
|
|
|
args[#args + 1] = match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return opts, args
|
|
|
|
end
|
|
|
|
|
2017-01-22 09:05:09 +01:00
|
|
|
function core.register_chatcommand(cmd, def)
|
|
|
|
def = def or {}
|
|
|
|
def.params = def.params or ""
|
|
|
|
def.description = def.description or ""
|
|
|
|
def.privs = def.privs or {}
|
|
|
|
def.mod_origin = core.get_current_modname() or "??"
|
|
|
|
core.registered_chatcommands[cmd] = def
|
|
|
|
end
|
|
|
|
|
|
|
|
function core.unregister_chatcommand(name)
|
|
|
|
if core.registered_chatcommands[name] then
|
|
|
|
core.registered_chatcommands[name] = nil
|
|
|
|
else
|
|
|
|
core.log("warning", "Not unregistering chatcommand " ..name..
|
|
|
|
" because it doesn't exist.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function core.override_chatcommand(name, redefinition)
|
|
|
|
local chatcommand = core.registered_chatcommands[name]
|
|
|
|
assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
|
|
|
|
for k, v in pairs(redefinition) do
|
|
|
|
rawset(chatcommand, k, v)
|
|
|
|
end
|
|
|
|
core.registered_chatcommands[name] = chatcommand
|
2017-03-25 00:43:36 +01:00
|
|
|
end
|
|
|
|
|
2021-04-03 22:51:09 +02:00
|
|
|
local function format_help_line(cmd, def)
|
|
|
|
local cmd_marker = INIT == "client" and "." or "/"
|
|
|
|
local msg = core.colorize("#00ffff", cmd_marker .. cmd)
|
|
|
|
if def.params and def.params ~= "" then
|
|
|
|
msg = msg .. " " .. def.params
|
|
|
|
end
|
|
|
|
if def.description and def.description ~= "" then
|
|
|
|
msg = msg .. ": " .. def.description
|
|
|
|
end
|
|
|
|
return msg
|
|
|
|
end
|
|
|
|
|
2017-03-25 00:43:36 +01:00
|
|
|
local function do_help_cmd(name, param)
|
2021-12-01 20:22:46 +01:00
|
|
|
local opts, args = getopts("help", param)
|
2021-04-03 22:51:09 +02:00
|
|
|
if not opts then
|
|
|
|
return false, args
|
|
|
|
end
|
|
|
|
if #args > 1 then
|
|
|
|
return false, S("Too many arguments, try using just /help <command>")
|
2017-03-25 00:43:36 +01:00
|
|
|
end
|
2021-04-03 22:51:09 +02:00
|
|
|
local use_gui = INIT ~= "client" and core.get_player_by_name(name)
|
|
|
|
use_gui = use_gui and not opts:find("t")
|
|
|
|
|
|
|
|
if #args == 0 and not use_gui then
|
2017-03-25 00:43:36 +01:00
|
|
|
local cmds = {}
|
|
|
|
for cmd, def in pairs(core.registered_chatcommands) do
|
|
|
|
if INIT == "client" or core.check_player_privs(name, def.privs) then
|
|
|
|
cmds[#cmds + 1] = cmd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(cmds)
|
2021-03-05 16:27:33 +01:00
|
|
|
local msg
|
|
|
|
if INIT == "game" then
|
|
|
|
msg = S("Available commands: @1",
|
|
|
|
table.concat(cmds, " ")) .. "\n"
|
|
|
|
.. S("Use '/help <cmd>' to get more "
|
|
|
|
.. "information, or '/help all' to list "
|
|
|
|
.. "everything.")
|
|
|
|
else
|
|
|
|
msg = core.gettext("Available commands: ")
|
|
|
|
.. table.concat(cmds, " ") .. "\n"
|
|
|
|
.. core.gettext("Use '.help <cmd>' to get more "
|
|
|
|
.. "information, or '.help all' to list "
|
|
|
|
.. "everything.")
|
|
|
|
end
|
|
|
|
return true, msg
|
2021-04-03 22:51:09 +02:00
|
|
|
elseif #args == 0 or (args[1] == "all" and use_gui) then
|
|
|
|
core.show_general_help_formspec(name)
|
|
|
|
return true
|
|
|
|
elseif args[1] == "all" then
|
2017-03-25 00:43:36 +01:00
|
|
|
local cmds = {}
|
|
|
|
for cmd, def in pairs(core.registered_chatcommands) do
|
|
|
|
if INIT == "client" or core.check_player_privs(name, def.privs) then
|
|
|
|
cmds[#cmds + 1] = format_help_line(cmd, def)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(cmds)
|
2021-03-05 16:27:33 +01:00
|
|
|
local msg
|
|
|
|
if INIT == "game" then
|
|
|
|
msg = S("Available commands:")
|
|
|
|
else
|
|
|
|
msg = core.gettext("Available commands:")
|
|
|
|
end
|
|
|
|
return true, msg.."\n"..table.concat(cmds, "\n")
|
2021-04-03 22:51:09 +02:00
|
|
|
elseif INIT == "game" and args[1] == "privs" then
|
|
|
|
if use_gui then
|
|
|
|
core.show_privs_help_formspec(name)
|
|
|
|
return true
|
|
|
|
end
|
2019-09-19 00:37:56 +02:00
|
|
|
local privs = {}
|
|
|
|
for priv, def in pairs(core.registered_privileges) do
|
|
|
|
privs[#privs + 1] = priv .. ": " .. def.description
|
|
|
|
end
|
|
|
|
table.sort(privs)
|
2021-03-05 16:27:33 +01:00
|
|
|
return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
|
2017-03-25 00:43:36 +01:00
|
|
|
else
|
2021-04-03 22:51:09 +02:00
|
|
|
local cmd = args[1]
|
2017-03-25 00:43:36 +01:00
|
|
|
local def = core.registered_chatcommands[cmd]
|
|
|
|
if not def then
|
2021-03-05 16:27:33 +01:00
|
|
|
local msg
|
|
|
|
if INIT == "game" then
|
|
|
|
msg = S("Command not available: @1", cmd)
|
|
|
|
else
|
|
|
|
msg = core.gettext("Command not available: ") .. cmd
|
|
|
|
end
|
|
|
|
return false, msg
|
2017-03-25 00:43:36 +01:00
|
|
|
else
|
|
|
|
return true, format_help_line(cmd, def)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if INIT == "client" then
|
|
|
|
core.register_chatcommand("help", {
|
2021-03-05 16:27:33 +01:00
|
|
|
params = core.gettext("[all | <cmd>]"),
|
|
|
|
description = core.gettext("Get help for commands"),
|
2017-03-25 00:43:36 +01:00
|
|
|
func = function(param)
|
|
|
|
return do_help_cmd(nil, param)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
core.register_chatcommand("help", {
|
2021-04-03 22:51:09 +02:00
|
|
|
params = S("[all | privs | <cmd>] [-t]"),
|
|
|
|
description = S("Get help for commands or list privileges (-t: output in chat)"),
|
2017-03-25 00:43:36 +01:00
|
|
|
func = do_help_cmd,
|
|
|
|
})
|
|
|
|
end
|