From 44203ab8171357980632bfa9809319daf412efe3 Mon Sep 17 00:00:00 2001 From: Samuel Keiffer Date: Tue, 30 Jun 2020 12:21:36 +0000 Subject: [PATCH] Adds a command that allows a username to be added or removed from the whitelist --- common/src/cmd.rs | 8 ++++++++ server/src/cmd.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/common/src/cmd.rs b/common/src/cmd.rs index c7314e25f9..16fab4703b 100644 --- a/common/src/cmd.rs +++ b/common/src/cmd.rs @@ -70,6 +70,7 @@ pub enum ChatCommand { Tp, Version, Waypoint, + Whitelist, World, } @@ -110,6 +111,7 @@ pub static CHAT_COMMANDS: &[ChatCommand] = &[ ChatCommand::Tp, ChatCommand::Version, ChatCommand::Waypoint, + ChatCommand::Whitelist, ChatCommand::World, ]; @@ -347,6 +349,11 @@ impl ChatCommand { ChatCommand::Waypoint => { cmd(vec![], "Set your waypoint to your current position", Admin) }, + ChatCommand::Whitelist => cmd( + vec![Any("add/remove", Required), Any("username", Required)], + "Adds/removes username to whitelist", + Admin, + ), ChatCommand::World => cmd( vec![Message(Optional)], "Send messages to everyone on the server", @@ -393,6 +400,7 @@ impl ChatCommand { ChatCommand::Tp => "tp", ChatCommand::Version => "version", ChatCommand::Waypoint => "waypoint", + ChatCommand::Whitelist => "whitelist", ChatCommand::World => "world", } } diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 4bd28d1f6c..b1744c22f8 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -96,6 +96,7 @@ fn get_handler(cmd: &ChatCommand) -> CommandHandler { ChatCommand::Tp => handle_tp, ChatCommand::Version => handle_version, ChatCommand::Waypoint => handle_waypoint, + ChatCommand::Whitelist => handle_whitelist, ChatCommand::World => handle_world, } } @@ -1679,3 +1680,43 @@ fn handle_version( )), ); } + +fn handle_whitelist( + server: &mut Server, + client: EcsEntity, + _target: EcsEntity, + args: String, + action: &ChatCommand, +) { + if let Ok((whitelist_action, username)) = scan_fmt!(&args, &action.arg_fmt(), String, String) { + if whitelist_action.eq_ignore_ascii_case("add") { + server + .settings_mut() + .edit(|s| s.whitelist.push(username.clone())); + server.notify_client( + client, + ChatType::CommandInfo.server_msg(format!("\"{}\" added to whitelist", username)), + ); + } else if whitelist_action.eq_ignore_ascii_case("remove") { + server.settings_mut().edit(|s| { + s.whitelist + .retain(|x| !x.eq_ignore_ascii_case(&username.clone())) + }); + server.notify_client( + client, + ChatType::CommandInfo + .server_msg(format!("\"{}\" removed from whitelist", username)), + ); + } else { + server.notify_client( + client, + ChatType::CommandError.server_msg(action.help_string()), + ); + } + } else { + server.notify_client( + client, + ChatType::CommandError.server_msg(action.help_string()), + ); + } +}