diff --git a/resources/scripts/Global.lua b/resources/scripts/Global.lua index b9d5fa3..839fc6f 100644 --- a/resources/scripts/Global.lua +++ b/resources/scripts/Global.lua @@ -3,10 +3,16 @@ function onBeginLogin(player) player:send_message("Welcome to Kawari!") end -function onCommandPermissionError(player) +function onCommandRequiredRankInsufficientError(player) player:send_message("You do not have permission to run this command.") end +function onCommandRequiredRankMissingError(additional_information, player) + local error_msg = "Your script does not define the required_rank variable. Please define it in your script for it to run." + + player:send_message(string.format("%s\nAdditional information: %s", error_msg, additional_information)) +end + function split(input, separator) if separator == nil then separator = '%s' diff --git a/resources/scripts/commands/debug/ClassJob.lua b/resources/scripts/commands/debug/ClassJob.lua index 8922d7c..767e05a 100644 --- a/resources/scripts/commands/debug/ClassJob.lua +++ b/resources/scripts/commands/debug/ClassJob.lua @@ -1,4 +1,4 @@ -permissions = GM_RANK_DEBUG +required_rank = GM_RANK_DEBUG function onCommand(args, player) local parts = split(args) diff --git a/resources/scripts/commands/debug/Festival.lua b/resources/scripts/commands/debug/Festival.lua index fff5f0a..6969884 100644 --- a/resources/scripts/commands/debug/Festival.lua +++ b/resources/scripts/commands/debug/Festival.lua @@ -1,6 +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 +required_rank = 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 36c9167..6a63a06 100644 --- a/resources/scripts/commands/debug/Nudge.lua +++ b/resources/scripts/commands/debug/Nudge.lua @@ -1,6 +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_DEBUG +required_rank = GM_RANK_DEBUG 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 index 25c9ea4..e73e593 100644 --- a/resources/scripts/commands/debug/PermissionTest.lua +++ b/resources/scripts/commands/debug/PermissionTest.lua @@ -1,4 +1,4 @@ -permissions = GM_RANK_MAX +required_rank = GM_RANK_MAX function onCommand(args, player) player:send_message("How did you run this? This shouldn't be runnable!") diff --git a/resources/scripts/commands/debug/SetPos.lua b/resources/scripts/commands/debug/SetPos.lua index abe34ef..b24f310 100644 --- a/resources/scripts/commands/debug/SetPos.lua +++ b/resources/scripts/commands/debug/SetPos.lua @@ -1,4 +1,4 @@ -permissions = GM_RANK_DEBUG +required_rank = GM_RANK_DEBUG function onCommand(args, player) local parts = split(args) diff --git a/src/bin/kawari-world.rs b/src/bin/kawari-world.rs index 5394d9e..8a109e8 100644 --- a/src/bin/kawari-world.rs +++ b/src/bin/kawari-world.rs @@ -535,10 +535,20 @@ async fn client_loop( .exec() .unwrap(); - let permissions: u8 = lua.globals().get("permissions") - .expect("Script does not have permissions variable set"); + let required_rank = lua.globals().get("required_rank"); + if let Err(error) = required_rank { + tracing::info!("Script is missing required_rank! Unable to run command, sending error to user. Additional information: {}", error); + let func: Function = + lua.globals().get("onCommandRequiredRankMissingError").unwrap(); + func.call::<()>((error.to_string(), connection_data)).unwrap(); + return Ok(()); + } - if connection.player_data.gm_rank as u8 >= permissions { + /* Reset state for future commands. Without this it'll stay set to the last value + * and allow other commands that omit required_rank to run, which is undesirable. */ + lua.globals().set("required_rank", mlua::Value::Nil).unwrap(); + + if connection.player_data.gm_rank as u8 >= required_rank.unwrap() { let mut func_args = ""; if parts.len() > 1 { func_args = &chat_message.message[command_name.len() + 2..]; @@ -552,10 +562,10 @@ async fn client_loop( unwrap(); Ok(()) } else { - tracing::info!("User with account_id {} tried to invoke GM command {} they have no permissions for!", + tracing::info!("User with account_id {} tried to invoke GM command {} with insufficient privileges!", connection.player_data.account_id, command_name); let func: Function = - lua.globals().get("onCommandPermissionError").unwrap(); + lua.globals().get("onCommandRequiredRankInsufficientError").unwrap(); func.call::<()>(connection_data).unwrap(); Ok(()) }