1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-06-30 11:47:45 +00:00

Add a basic permission system to Lua scripts to bring them in line with ingame GM commands.

This commit is contained in:
The Dax 2025-06-20 09:27:37 -04:00 committed by Joshua Goins
parent b488cfb348
commit be20b0f604
7 changed files with 47 additions and 11 deletions

View file

@ -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")

View file

@ -1,3 +1,5 @@
permissions = GM_RANK_DEBUG
function onCommand(args, player)
local parts = split(args)
player:set_classjob(parts[1])

View file

@ -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)

View file

@ -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))

View file

@ -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

View file

@ -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)

View file

@ -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();
}