mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Add max chat message length
Fixes #115 Credit to @scorpion9979 for the previous implementation (https://gitlab.com/veloren/veloren/merge_requests/215)
This commit is contained in:
parent
101c563b03
commit
6e4d556073
@ -9,7 +9,10 @@ pub use specs::{join::Join, saveload::Marker, Entity as EcsEntity, ReadStorage};
|
||||
|
||||
use common::{
|
||||
comp,
|
||||
msg::{ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg},
|
||||
msg::{
|
||||
validate_chat_msg, ChatMsgValidationError, ClientMsg, ClientState, RequestStateError,
|
||||
ServerError, ServerInfo, ServerMsg, MAX_BYTES_CHAT_MSG,
|
||||
},
|
||||
net::PostBox,
|
||||
state::{State, Uid},
|
||||
terrain::{block::Block, TerrainChunk, TerrainChunkSize},
|
||||
@ -232,7 +235,13 @@ impl Client {
|
||||
/// Send a chat message to the server.
|
||||
#[allow(dead_code)]
|
||||
pub fn send_chat(&mut self, msg: String) {
|
||||
self.postbox.send_message(ClientMsg::chat(msg))
|
||||
match validate_chat_msg(&msg) {
|
||||
Ok(()) => self.postbox.send_message(ClientMsg::chat(msg)),
|
||||
Err(ChatMsgValidationError::TooLong) => log::warn!(
|
||||
"Attempted to send a message that's too long (Over {} bytes)",
|
||||
MAX_BYTES_CHAT_MSG
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove all cached terrain
|
||||
|
@ -16,3 +16,18 @@ pub enum ClientState {
|
||||
Dead,
|
||||
Character,
|
||||
}
|
||||
|
||||
pub const MAX_BYTES_CHAT_MSG: usize = 80;
|
||||
|
||||
pub enum ChatMsgValidationError {
|
||||
TooLong,
|
||||
}
|
||||
|
||||
pub fn validate_chat_msg(msg: &str) -> Result<(), ChatMsgValidationError> {
|
||||
// TODO: Consider using grapheme cluster count instead of size in bytes
|
||||
if msg.len() <= MAX_BYTES_CHAT_MSG {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ChatMsgValidationError::TooLong)
|
||||
}
|
||||
}
|
||||
|
@ -999,8 +999,17 @@ impl Server {
|
||||
ClientState::Registered
|
||||
| ClientState::Spectator
|
||||
| ClientState::Dead
|
||||
| ClientState::Character => new_chat_msgs
|
||||
.push((Some(entity), ServerMsg::ChatMsg { chat_type, message })),
|
||||
| ClientState::Character => match validate_chat_msg(&message) {
|
||||
Ok(()) => new_chat_msgs.push((
|
||||
Some(entity),
|
||||
ServerMsg::ChatMsg { chat_type, message },
|
||||
)),
|
||||
Err(ChatMsgValidationError::TooLong) => log::warn!(
|
||||
"Recieved a chat message that's too long (max:{} len:{})",
|
||||
MAX_BYTES_CHAT_MSG,
|
||||
message.len()
|
||||
),
|
||||
},
|
||||
ClientState::Pending => {}
|
||||
},
|
||||
ClientMsg::PlayerPhysics { pos, vel, ori } => match client.client_state {
|
||||
|
@ -3,7 +3,7 @@ use super::{
|
||||
KILL_COLOR, META_COLOR, PRIVATE_COLOR, SAY_COLOR, TELL_COLOR, TEXT_COLOR,
|
||||
};
|
||||
use client::Event as ClientEvent;
|
||||
use common::ChatType;
|
||||
use common::{msg::validate_chat_msg, ChatType};
|
||||
use conrod_core::{
|
||||
input::Key,
|
||||
position::Dimension,
|
||||
@ -59,7 +59,9 @@ impl<'a> Chat<'a> {
|
||||
}
|
||||
|
||||
pub fn input(mut self, input: String) -> Self {
|
||||
self.force_input = Some(input);
|
||||
if let Ok(()) = validate_chat_msg(&input) {
|
||||
self.force_input = Some(input);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
@ -206,7 +208,9 @@ impl<'a> Widget for Chat<'a> {
|
||||
{
|
||||
let mut input = str.to_owned();
|
||||
input.retain(|c| c != '\n');
|
||||
state.update(|s| s.input = input);
|
||||
if let Ok(()) = validate_chat_msg(&input) {
|
||||
state.update(|s| s.input = input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user