diff options
Diffstat (limited to 'builtin/game/chat.lua')
-rw-r--r-- | builtin/game/chat.lua | 115 |
1 files changed, 85 insertions, 30 deletions
diff --git a/builtin/game/chat.lua b/builtin/game/chat.lua index bf2d7851e..0bd12c25f 100644 --- a/builtin/game/chat.lua +++ b/builtin/game/chat.lua @@ -75,9 +75,9 @@ core.register_on_chat_message(function(name, message) local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs) if has_privs then core.set_last_run_mod(cmd_def.mod_origin) - local t_before = minetest.get_us_time() + local t_before = core.get_us_time() local success, result = cmd_def.func(name, param) - local delay = (minetest.get_us_time() - t_before) / 1000000 + local delay = (core.get_us_time() - t_before) / 1000000 if success == false and result == nil then core.chat_send_player(name, "-!- "..S("Invalid command usage.")) local help_def = core.registered_chatcommands["help"] @@ -91,11 +91,12 @@ core.register_on_chat_message(function(name, message) if delay > msg_time_threshold then -- Show how much time it took to execute the command if result then - result = result .. - minetest.colorize("#f3d2ff", " (%.5g s)"):format(delay) + result = result .. core.colorize("#f3d2ff", S(" (@1 s)", + string.format("%.5f", delay))) else - result = minetest.colorize("#f3d2ff", - "Command execution took %.5f s"):format(delay) + result = core.colorize("#f3d2ff", S( + "Command execution took @1 s", + string.format("%.5f", delay))) end end if result then @@ -166,6 +167,18 @@ core.register_chatcommand("admin", { end, }) +local function privileges_of(name, privs) + if not privs then + privs = core.get_player_privs(name) + end + local privstr = core.privs_to_string(privs, ", ") + if privstr == "" then + return S("@1 does not have any privileges.", name) + else + return S("Privileges of @1: @2", name, privstr) + end +end + core.register_chatcommand("privs", { params = S("[<name>]"), description = S("Show privileges of yourself or another player"), @@ -175,9 +188,7 @@ core.register_chatcommand("privs", { if not core.player_exists(name) then return false, S("Player @1 does not exist.", name) end - return true, S("Privileges of @1: @2", name, - core.privs_to_string( - core.get_player_privs(name), ", ")) + return true, privileges_of(name) end, }) @@ -226,7 +237,10 @@ local function handle_grant_command(caller, grantname, grantprivstr) core.string_to_privs(core.settings:get("basic_privs") or "interact,shout") for priv, _ in pairs(grantprivs) do if not basic_privs[priv] and not caller_privs.privs then - return false, S("Your privileges are insufficient.") + return false, S("Your privileges are insufficient. ".. + "'@1' only allows you to grant: @2", + "basic_privs", + core.privs_to_string(basic_privs, ', ')) end if not core.registered_privileges[priv] then privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n" @@ -245,15 +259,13 @@ local function handle_grant_command(caller, grantname, grantprivstr) if grantname ~= caller then core.chat_send_player(grantname, S("@1 granted you privileges: @2", caller, - core.privs_to_string(grantprivs, ' '))) + core.privs_to_string(grantprivs, ', '))) end - return true, S("Privileges of @1: @2", grantname, - core.privs_to_string( - core.get_player_privs(grantname), ' ')) + return true, privileges_of(grantname) end core.register_chatcommand("grant", { - params = S("<name> (<privilege> | all)"), + params = S("<name> (<privilege> [, <privilege2> [<...>]] | all)"), description = S("Give privileges to player"), func = function(name, param) local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)") @@ -265,7 +277,7 @@ core.register_chatcommand("grant", { }) core.register_chatcommand("grantme", { - params = S("<privilege> | all"), + params = S("<privilege> [, <privilege2> [<...>]] | all"), description = S("Grant privileges to yourself"), func = function(name, param) if param == "" then @@ -285,16 +297,13 @@ local function handle_revoke_command(caller, revokename, revokeprivstr) return false, S("Player @1 does not exist.", revokename) end - local revokeprivs = core.string_to_privs(revokeprivstr) local privs = core.get_player_privs(revokename) - local basic_privs = - core.string_to_privs(core.settings:get("basic_privs") or "interact,shout") - for priv, _ in pairs(revokeprivs) do - if not basic_privs[priv] and not caller_privs.privs then - return false, S("Your privileges are insufficient.") - end - end + local revokeprivs = core.string_to_privs(revokeprivstr) + local is_singleplayer = core.is_singleplayer() + local is_admin = not is_singleplayer + and revokename == core.settings:get("name") + and revokename ~= "" if revokeprivstr == "all" then revokeprivs = privs privs = {} @@ -304,27 +313,73 @@ local function handle_revoke_command(caller, revokename, revokeprivstr) end end + local privs_unknown = "" + local basic_privs = + core.string_to_privs(core.settings:get("basic_privs") or "interact,shout") + local irrevokable = {} + local has_irrevokable_priv = false + for priv, _ in pairs(revokeprivs) do + if not basic_privs[priv] and not caller_privs.privs then + return false, S("Your privileges are insufficient. ".. + "'@1' only allows you to revoke: @2", + "basic_privs", + core.privs_to_string(basic_privs, ', ')) + end + local def = core.registered_privileges[priv] + if not def then + privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n" + elseif is_singleplayer and def.give_to_singleplayer then + irrevokable[priv] = true + elseif is_admin and def.give_to_admin then + irrevokable[priv] = true + end + end + for priv, _ in pairs(irrevokable) do + revokeprivs[priv] = nil + has_irrevokable_priv = true + end + if privs_unknown ~= "" then + return false, privs_unknown + end + if has_irrevokable_priv then + if is_singleplayer then + core.chat_send_player(caller, + S("Note: Cannot revoke in singleplayer: @1", + core.privs_to_string(irrevokable, ', '))) + elseif is_admin then + core.chat_send_player(caller, + S("Note: Cannot revoke from admin: @1", + core.privs_to_string(irrevokable, ', '))) + end + end + + local revokecount = 0 for priv, _ in pairs(revokeprivs) do -- call the on_revoke callbacks core.run_priv_callbacks(revokename, priv, caller, "revoke") + revokecount = revokecount + 1 end core.set_player_privs(revokename, privs) + local new_privs = core.get_player_privs(revokename) + + if revokecount == 0 then + return false, S("No privileges were revoked.") + end + core.log("action", caller..' revoked (' ..core.privs_to_string(revokeprivs, ', ') ..') privileges from '..revokename) if revokename ~= caller then core.chat_send_player(revokename, S("@1 revoked privileges from you: @2", caller, - core.privs_to_string(revokeprivs, ' '))) + core.privs_to_string(revokeprivs, ', '))) end - return true, S("Privileges of @1: @2", revokename, - core.privs_to_string( - core.get_player_privs(revokename), ' ')) + return true, privileges_of(revokename, new_privs) end core.register_chatcommand("revoke", { - params = S("<name> (<privilege> | all)"), + params = S("<name> (<privilege> [, <privilege2> [<...>]] | all)"), description = S("Remove privileges from player"), privs = {}, func = function(name, param) @@ -337,7 +392,7 @@ core.register_chatcommand("revoke", { }) core.register_chatcommand("revokeme", { - params = S("<privilege> | all"), + params = S("<privilege> [, <privilege2> [<...>]] | all"), description = S("Revoke privileges from yourself"), privs = {}, func = function(name, param) |