From be20b0f60445524c42db118adfb25bd0fba54298 Mon Sep 17 00:00:00 2001 From: The Dax Date: Fri, 20 Jun 2025 09:27:37 -0400 Subject: [PATCH] Add a basic permission system to Lua scripts to bring them in line with ingame GM commands. --- resources/scripts/Global.lua | 16 ++++++++++ resources/scripts/commands/debug/ClassJob.lua | 2 ++ resources/scripts/commands/debug/Festival.lua | 1 + resources/scripts/commands/debug/Nudge.lua | 1 + .../scripts/commands/debug/PermissionTest.lua | 5 +++ resources/scripts/commands/debug/SetPos.lua | 2 ++ src/bin/kawari-world.rs | 31 ++++++++++++------- 7 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 resources/scripts/commands/debug/PermissionTest.lua diff --git a/resources/scripts/Global.lua b/resources/scripts/Global.lua index fef7287..b9d5fa3 100644 --- a/resources/scripts/Global.lua +++ b/resources/scripts/Global.lua @@ -3,6 +3,10 @@ function onBeginLogin(player) player:send_message("Welcome to Kawari!") end +function onCommandPermissionError(player) + player:send_message("You do not have permission to run this command.") +end + function split(input, separator) if separator == nil then separator = '%s' @@ -16,6 +20,17 @@ function split(input, separator) return t end +-- Constants +GM_RANK_NORMALUSER = 0 +GM_RANK_GAMEMASTER = 1 +GM_RANK_EVENTJUNIOR = 3 +GM_RANK_EVENTSENIOR = 4 +GM_RANK_SUPPORT = 5 +GM_RANK_SENIOR = 7 +GM_RANK_DEBUG = 90 +GM_RANK_MAX = 255 -- Doesn't exist, used for purposes of testing permissions in scripts + + -- please keep these ids sorted! -- Actions @@ -51,3 +66,4 @@ registerCommand("classjob", "commands/debug/ClassJob.lua") registerCommand("setspeed", "commands/debug/SetSpeed.lua") registerCommand("nudge", "commands/debug/Nudge.lua") registerCommand("festival", "commands/debug/Festival.lua") +registerCommand("permtest", "commands/debug/PermissionTest.lua") diff --git a/resources/scripts/commands/debug/ClassJob.lua b/resources/scripts/commands/debug/ClassJob.lua index d7ce864..8922d7c 100644 --- a/resources/scripts/commands/debug/ClassJob.lua +++ b/resources/scripts/commands/debug/ClassJob.lua @@ -1,3 +1,5 @@ +permissions = GM_RANK_DEBUG + function onCommand(args, player) local parts = split(args) player:set_classjob(parts[1]) diff --git a/resources/scripts/commands/debug/Festival.lua b/resources/scripts/commands/debug/Festival.lua index 690b4c3..fff5f0a 100644 --- a/resources/scripts/commands/debug/Festival.lua +++ b/resources/scripts/commands/debug/Festival.lua @@ -1,5 +1,6 @@ -- A list of festival ids can be found in Hyperborea's source tree: -- https://github.com/kawaii/Hyperborea/blob/main/Hyperborea/festivals.yaml +permissions = GM_RANK_DEBUG function onCommand(args, player) local parts = split(args) diff --git a/resources/scripts/commands/debug/Nudge.lua b/resources/scripts/commands/debug/Nudge.lua index afb97d0..d2fa3f5 100644 --- a/resources/scripts/commands/debug/Nudge.lua +++ b/resources/scripts/commands/debug/Nudge.lua @@ -1,5 +1,6 @@ -- Ported from Ioncannon's Project Meteor Server -- https://bitbucket.org/Ioncannon/project-meteor-server/src/develop/Data/scripts/commands/gm/nudge.lua +permissions = GM_RANK_MAX function send_msg(message, player) player:send_message(string.format("[nudge] %s", message)) diff --git a/resources/scripts/commands/debug/PermissionTest.lua b/resources/scripts/commands/debug/PermissionTest.lua new file mode 100644 index 0000000..25c9ea4 --- /dev/null +++ b/resources/scripts/commands/debug/PermissionTest.lua @@ -0,0 +1,5 @@ +permissions = GM_RANK_MAX + +function onCommand(args, player) + player:send_message("How did you run this? This shouldn't be runnable!") +end diff --git a/resources/scripts/commands/debug/SetPos.lua b/resources/scripts/commands/debug/SetPos.lua index 5cfb508..abe34ef 100644 --- a/resources/scripts/commands/debug/SetPos.lua +++ b/resources/scripts/commands/debug/SetPos.lua @@ -1,3 +1,5 @@ +permissions = GM_RANK_DEBUG + function onCommand(args, player) local parts = split(args) player:set_position({ x = tonumber(parts[1]), y = tonumber(parts[2]), z = tonumber(parts[3]) }, 0) diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 8054f25..b405c67 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -535,20 +535,29 @@ async fn client_loop( .exec() .unwrap(); - let func: Function = - lua.globals().get("onCommand").unwrap(); + let permissions: u8 = lua.globals().get("permissions") + .expect("Script does not have permissions variable set"); - let mut func_args = ""; - if parts.len() > 1 { - func_args = &chat_message.message[command_name.len() + 2..]; - tracing::info!("Args passed to Lua command {}: {}", command_name, func_args); + if connection.player_data.gm_rank as u8 >= permissions { + let mut func_args = ""; + if parts.len() > 1 { + func_args = &chat_message.message[command_name.len() + 2..]; + tracing::info!("Args passed to Lua command {}: {}", command_name, func_args); + } else { + tracing::info!("No additional args passed to Lua command {}.", command_name); + } + let func: Function = + lua.globals().get("onCommand").unwrap(); + func.call::<()>((func_args, connection_data)). + unwrap(); + Ok(()) } else { - tracing::info!("No additional args passed to Lua command {}.", command_name); + tracing::info!("User with account_id {} tried to invoke GM command they have no permissions for!!!", connection.player_data.account_id); + let func: Function = + lua.globals().get("onCommandPermissionError").unwrap(); + func.call::<()>(connection_data).unwrap(); + Ok(()) } - func.call::<()>((func_args, connection_data)). - unwrap(); - - Ok(()) }) .unwrap(); }