diff options
Diffstat (limited to 'builtin/builtin.lua')
-rw-r--r-- | builtin/builtin.lua | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/builtin/builtin.lua b/builtin/builtin.lua index 2be7a4dcf..71cd4cf53 100644 --- a/builtin/builtin.lua +++ b/builtin/builtin.lua @@ -821,6 +821,116 @@ function minetest.after(time, func, param) table.insert(minetest.timers_to_add, {time=time, func=func, param=param}) end +function minetest.check_player_privs(name, privs) + local player_privs = minetest.get_player_privs(name) + local missing_privileges = {} + for priv, val in pairs(privs) do + if val then + if not player_privs[priv] then + table.insert(missing_privileges, priv) + end + end + end + if #missing_privileges > 0 then + return false, missing_privileges + end + return true, "" +end + +-- +-- Chat commands +-- + +minetest.chatcommands = {} +function minetest.register_chatcommand(cmd, def) + def = def or {} + def.params = def.params or "" + def.description = def.description or "" + def.privs = def.privs or {} + minetest.chatcommands[cmd] = def +end + +-- Register the help command +minetest.register_chatcommand("help", { + privs = {}, + params = "(nothing)/all/<cmd>", + description = "Get help for commands", + func = function(name, param) + local format_help_line = function(cmd, def) + local msg = "/"..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 + if not param or param == "" then + local msg = "" + cmds = {} + for cmd, def in pairs(minetest.chatcommands) do + if minetest.check_player_privs(name, def.privs) then + table.insert(cmds, cmd) + end + end + minetest.chat_send_player(name, "Available commands: "..table.concat(cmds, " ")) + minetest.chat_send_player(name, "Use '/help <cmd>' to get more information, or '/help all' to list everything.") + elseif param == "all" then + minetest.chat_send_player(name, "Available commands:") + for cmd, def in pairs(minetest.chatcommands) do + if minetest.check_player_privs(name, def.privs) then + minetest.chat_send_player(name, format_help_line(cmd, def)) + end + end + else + local cmd = param + def = minetest.chatcommands[cmd] + if not def then + minetest.chat_send_player(name, "Command not available: "..cmd) + else + minetest.chat_send_player(name, format_help_line(cmd, def)) + end + end + end, +}) + +-- Register C++ commands without functions +minetest.register_chatcommand("me", {params = nil, description = "chat action (eg. /me orders a pizza)"}) +minetest.register_chatcommand("status", {description = "print server status line"}) +minetest.register_chatcommand("privs", {params = "<name>", description = "print out privileges of player"}) +minetest.register_chatcommand("shutdown", {params = "", description = "shutdown server", privs = {server=true}}) +minetest.register_chatcommand("setting", {params = "<name> = <value>", description = "set line in configuration file", privs = {server=true}}) +minetest.register_chatcommand("clearobjects", {params = "", description = "clear all objects in world", privs = {server=true}}) +minetest.register_chatcommand("time", {params = "<0...24000>", description = "set time of day", privs = {settime=true}}) +minetest.register_chatcommand("teleport", {params = "<X>,<Y>,<Z>", description = "teleport to given position", privs = {teleport=true}}) +minetest.register_chatcommand("grant", {params = "<name> <privilege>", description = "Give privilege to player", privs = {privs=true}}) +minetest.register_chatcommand("revoke", {params = "<name> <privilege>", description = "Remove privilege from player", privs = {privs=true}}) +minetest.register_chatcommand("ban", {params = "<name>", description = "ban IP of player", privs = {ban=true}}) +minetest.register_chatcommand("unban", {params = "<name/ip>", description = "remove IP ban", privs = {ban=true}}) +minetest.register_chatcommand("setpassword", {params = "<name> <password>", description = "set given password", privs = {password=true}}) +minetest.register_chatcommand("clearpassword", {params = "<name>", description = "set empty password", privs = {password=true}}) + +-- +-- Builtin chat handler +-- + +minetest.register_on_chat_message(function(name, message) + local cmd, param = string.match(message, "/([^ ]+) *(.*)") + local cmd_def = minetest.chatcommands[cmd] + if cmd_def then + if not cmd_def.func then + -- This is a C++ command + return false + else + local has_privs, missing_privs = minetest.check_player_privs(name, cmd_def.privs) + if has_privs then + cmd_def.func(name, param) + else + minetest.chat_send_player(name, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")") + end + return true -- handled chat message + end + end + return false +end) + -- -- Set random seed -- |