mirror of
https://github.com/zyllian/classics.git
synced 2025-01-18 11:47:14 -08:00
add /ban, /allowentry, and /setpass commands, resolving #4
This commit is contained in:
parent
b76bb5bbc6
commit
5e8fe5a753
4 changed files with 149 additions and 10 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -108,6 +108,7 @@ dependencies = [
|
||||||
"flate2",
|
"flate2",
|
||||||
"half",
|
"half",
|
||||||
"internment",
|
"internment",
|
||||||
|
"nanoid",
|
||||||
"optional_struct",
|
"optional_struct",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"rand",
|
"rand",
|
||||||
|
@ -243,6 +244,15 @@ dependencies = [
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nanoid"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ffa00dec017b5b1a8b7cf5e2c008bfda1aa7e0697ac1508b491fdf2622fb4d8"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num_cpus"
|
name = "num_cpus"
|
||||||
version = "1.16.0"
|
version = "1.16.0"
|
||||||
|
|
|
@ -9,6 +9,7 @@ bytes = "1"
|
||||||
flate2 = "1"
|
flate2 = "1"
|
||||||
half = "2"
|
half = "2"
|
||||||
internment = {version = "0.8", features = ["serde"]}
|
internment = {version = "0.8", features = ["serde"]}
|
||||||
|
nanoid = "0.4"
|
||||||
optional_struct = "0.4"
|
optional_struct = "0.4"
|
||||||
parking_lot = "0.12"
|
parking_lot = "0.12"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
|
|
|
@ -2,13 +2,26 @@ use crate::player::PlayerType;
|
||||||
|
|
||||||
const CMD_ME: &str = "me";
|
const CMD_ME: &str = "me";
|
||||||
const CMD_SAY: &str = "say";
|
const CMD_SAY: &str = "say";
|
||||||
const CMD_SET_PERM: &str = "setperm";
|
const CMD_SETPERM: &str = "setperm";
|
||||||
const CMD_KICK: &str = "kick";
|
const CMD_KICK: &str = "kick";
|
||||||
const CMD_STOP: &str = "stop";
|
const CMD_STOP: &str = "stop";
|
||||||
const CMD_HELP: &str = "help";
|
const CMD_HELP: &str = "help";
|
||||||
|
const CMD_BAN: &str = "ban";
|
||||||
|
const CMD_ALLOWENTRY: &str = "allowentry";
|
||||||
|
const CMD_SETPASS: &str = "setpass";
|
||||||
|
|
||||||
/// list of commands available on the server
|
/// list of commands available on the server
|
||||||
pub const COMMANDS_LIST: &[&str] = &[CMD_ME, CMD_SAY, CMD_SET_PERM, CMD_KICK, CMD_STOP, CMD_HELP];
|
pub const COMMANDS_LIST: &[&str] = &[
|
||||||
|
CMD_ME,
|
||||||
|
CMD_SAY,
|
||||||
|
CMD_SETPERM,
|
||||||
|
CMD_KICK,
|
||||||
|
CMD_STOP,
|
||||||
|
CMD_HELP,
|
||||||
|
CMD_BAN,
|
||||||
|
CMD_ALLOWENTRY,
|
||||||
|
CMD_SETPASS,
|
||||||
|
];
|
||||||
|
|
||||||
/// enum for possible commands
|
/// enum for possible commands
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -33,6 +46,18 @@ pub enum Command<'m> {
|
||||||
Stop,
|
Stop,
|
||||||
/// gets help about the given command, or about all commands if no command is given
|
/// gets help about the given command, or about all commands if no command is given
|
||||||
Help { command: Option<&'m str> },
|
Help { command: Option<&'m str> },
|
||||||
|
/// bans a player from the server
|
||||||
|
Ban {
|
||||||
|
player_username: &'m str,
|
||||||
|
message: Option<&'m str>,
|
||||||
|
},
|
||||||
|
/// allows a player entry into the server
|
||||||
|
AllowEntry {
|
||||||
|
player_username: &'m str,
|
||||||
|
password: Option<&'m str>,
|
||||||
|
},
|
||||||
|
/// sets the current player's password
|
||||||
|
SetPass { password: &'m str },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'m> Command<'m> {
|
impl<'m> Command<'m> {
|
||||||
|
@ -45,7 +70,7 @@ impl<'m> Command<'m> {
|
||||||
Ok(match command_name {
|
Ok(match command_name {
|
||||||
CMD_ME => Self::Me { action: arguments },
|
CMD_ME => Self::Me { action: arguments },
|
||||||
CMD_SAY => Self::Say { message: arguments },
|
CMD_SAY => Self::Say { message: arguments },
|
||||||
CMD_SET_PERM => Self::SetPermissions {
|
CMD_SETPERM => Self::SetPermissions {
|
||||||
player_username: Self::next_string(&mut arguments)?,
|
player_username: Self::next_string(&mut arguments)?,
|
||||||
permissions: arguments.trim().try_into()?,
|
permissions: arguments.trim().try_into()?,
|
||||||
},
|
},
|
||||||
|
@ -59,6 +84,27 @@ impl<'m> Command<'m> {
|
||||||
CMD_HELP => Self::Help {
|
CMD_HELP => Self::Help {
|
||||||
command: (!arguments.is_empty()).then_some(arguments),
|
command: (!arguments.is_empty()).then_some(arguments),
|
||||||
},
|
},
|
||||||
|
CMD_BAN => {
|
||||||
|
let player_username = Self::next_string(&mut arguments)?;
|
||||||
|
let message = arguments.trim();
|
||||||
|
let message = (!message.is_empty()).then_some(message);
|
||||||
|
Self::Ban {
|
||||||
|
player_username,
|
||||||
|
message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CMD_ALLOWENTRY => {
|
||||||
|
let player_username = Self::next_string(&mut arguments)?;
|
||||||
|
let password = arguments.trim();
|
||||||
|
let password = (!password.is_empty()).then_some(password);
|
||||||
|
Self::AllowEntry {
|
||||||
|
player_username,
|
||||||
|
password,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CMD_SETPASS => Self::SetPass {
|
||||||
|
password: arguments.trim(),
|
||||||
|
},
|
||||||
_ => return Err(format!("Unknown command: {command_name}")),
|
_ => return Err(format!("Unknown command: {command_name}")),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -68,10 +114,13 @@ impl<'m> Command<'m> {
|
||||||
match self {
|
match self {
|
||||||
Self::Me { .. } => CMD_ME,
|
Self::Me { .. } => CMD_ME,
|
||||||
Self::Say { .. } => CMD_SAY,
|
Self::Say { .. } => CMD_SAY,
|
||||||
Self::SetPermissions { .. } => CMD_SET_PERM,
|
Self::SetPermissions { .. } => CMD_SETPERM,
|
||||||
Self::Kick { .. } => CMD_KICK,
|
Self::Kick { .. } => CMD_KICK,
|
||||||
Self::Stop => CMD_STOP,
|
Self::Stop => CMD_STOP,
|
||||||
Self::Help { .. } => CMD_HELP,
|
Self::Help { .. } => CMD_HELP,
|
||||||
|
Self::Ban { .. } => CMD_BAN,
|
||||||
|
Self::AllowEntry { .. } => CMD_ALLOWENTRY,
|
||||||
|
Self::SetPass { .. } => CMD_SETPASS,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,6 +135,7 @@ impl<'m> Command<'m> {
|
||||||
CMD_ME => PlayerType::Normal,
|
CMD_ME => PlayerType::Normal,
|
||||||
CMD_STOP => PlayerType::Operator,
|
CMD_STOP => PlayerType::Operator,
|
||||||
CMD_HELP => PlayerType::Normal,
|
CMD_HELP => PlayerType::Normal,
|
||||||
|
CMD_SETPASS => PlayerType::Normal,
|
||||||
_ => PlayerType::Moderator,
|
_ => PlayerType::Moderator,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,7 +153,7 @@ impl<'m> Command<'m> {
|
||||||
c("<message>"),
|
c("<message>"),
|
||||||
"&fSends a message as being from the server.".to_string(),
|
"&fSends a message as being from the server.".to_string(),
|
||||||
],
|
],
|
||||||
CMD_SET_PERM => vec![
|
CMD_SETPERM => vec![
|
||||||
c("<username> <permission level>"),
|
c("<username> <permission level>"),
|
||||||
"&fSets a player's permission level.".to_string(),
|
"&fSets a player's permission level.".to_string(),
|
||||||
],
|
],
|
||||||
|
@ -119,6 +169,15 @@ impl<'m> Command<'m> {
|
||||||
c("[command]"),
|
c("[command]"),
|
||||||
"&fGets a list of commands or help about a command.".to_string(),
|
"&fGets a list of commands or help about a command.".to_string(),
|
||||||
],
|
],
|
||||||
|
CMD_BAN => vec![
|
||||||
|
c("<username> [reason]"),
|
||||||
|
"&fBans a player from the server.".to_string(),
|
||||||
|
],
|
||||||
|
CMD_ALLOWENTRY => vec![
|
||||||
|
c("<username>"),
|
||||||
|
"&fAllows a player into the server.".to_string(),
|
||||||
|
],
|
||||||
|
CMD_SETPASS => vec![c("<new password>"), "&fUpdates your password.".to_string()],
|
||||||
_ => vec!["&eUnknown command!".to_string()],
|
_ => vec!["&eUnknown command!".to_string()],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -455,11 +455,12 @@ async fn handle_stream_inner(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
other_player.should_be_kicked = Some(
|
other_player.should_be_kicked =
|
||||||
message
|
Some(format!(
|
||||||
.unwrap_or("<no message>")
|
"Kicked: {}",
|
||||||
.to_string(),
|
message
|
||||||
);
|
.unwrap_or("<no message>")
|
||||||
|
));
|
||||||
msg!(format!(
|
msg!(format!(
|
||||||
"{} has been kicked",
|
"{} has been kicked",
|
||||||
other_player.username
|
other_player.username
|
||||||
|
@ -517,6 +518,74 @@ async fn handle_stream_inner(
|
||||||
msg!(msg);
|
msg!(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Command::Ban {
|
||||||
|
player_username,
|
||||||
|
message,
|
||||||
|
} => {
|
||||||
|
let player_perms = player.permissions;
|
||||||
|
if let ServerProtectionMode::PasswordsByUser(passwords) = &mut data.config.protection_mode {
|
||||||
|
if !passwords.contains_key(player_username) {
|
||||||
|
msg!("&cPlayer is already banned!".to_string());
|
||||||
|
} else {
|
||||||
|
passwords.remove(player_username);
|
||||||
|
data.config.player_perms.remove(player_username);
|
||||||
|
data.config_needs_saving = true;
|
||||||
|
if let Some(other_player) =
|
||||||
|
data.players.iter_mut().find(|p| {
|
||||||
|
p.username == player_username
|
||||||
|
}) {
|
||||||
|
if player_perms
|
||||||
|
<= other_player.permissions
|
||||||
|
{
|
||||||
|
msg!("&cThis player outranks or is the same rank as you".to_string());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
other_player.should_be_kicked =
|
||||||
|
Some(format!("Banned: {}", message.unwrap_or("<no_message>")));
|
||||||
|
}
|
||||||
|
msg!(format!(
|
||||||
|
"{} has been banned",
|
||||||
|
player_username
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
msg!("&cServer must be set to per-user passwords!".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::AllowEntry {
|
||||||
|
player_username,
|
||||||
|
password,
|
||||||
|
} => {
|
||||||
|
if let ServerProtectionMode::PasswordsByUser(passwords) = &mut data.config.protection_mode {
|
||||||
|
if passwords.contains_key(player_username) {
|
||||||
|
msg!("&cPlayer is already allowed in the server!".to_string());
|
||||||
|
} else {
|
||||||
|
let password = password.map(|p| p.to_string()).unwrap_or_else(|| {
|
||||||
|
nanoid::nanoid!()
|
||||||
|
});
|
||||||
|
msg!(format!("{player_username} is now allowed in the server."));
|
||||||
|
msg!(format!("Password: {password}"));
|
||||||
|
passwords.insert(player_username.to_string(), password);
|
||||||
|
data.config_needs_saving = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
msg!("&cServer must be set to per-user passwords!".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::SetPass { password } => {
|
||||||
|
let username = player.username.clone();
|
||||||
|
if let ServerProtectionMode::PasswordsByUser(passwords) = &mut data.config.protection_mode {
|
||||||
|
passwords.insert(username, password.to_string());
|
||||||
|
data.config_needs_saving = true;
|
||||||
|
msg!("Updated password!".to_string());
|
||||||
|
} else {
|
||||||
|
msg!("&cServer must be set to per-user passwords!".to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue