From d68cd8fffc27f78499569aa6511cad326f315a6e Mon Sep 17 00:00:00 2001 From: Fabian Wunsch Date: Thu, 30 May 2024 19:52:52 +0200 Subject: [PATCH] ignore `HELP` spam --- breakwater-core/src/lib.rs | 2 ++ breakwater-parser/src/original.rs | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/breakwater-core/src/lib.rs b/breakwater-core/src/lib.rs index 3e75fe9..941dbca 100644 --- a/breakwater-core/src/lib.rs +++ b/breakwater-core/src/lib.rs @@ -20,3 +20,5 @@ if cfg!(feature = "alpha") { "PX x y rrggbbaa: Color the pixel (x,y) with the given hexadecimal color rrggbb. The alpha part is discarded for performance reasons, as breakwater was compiled without the alpha feature" } ).as_bytes(); + +pub const ALT_HELP_TEXT: &[u8] = b"Stop spamming HELP!\n"; diff --git a/breakwater-parser/src/original.rs b/breakwater-parser/src/original.rs index 993043c..39c5c33 100644 --- a/breakwater-parser/src/original.rs +++ b/breakwater-parser/src/original.rs @@ -3,7 +3,7 @@ use std::{ sync::Arc, }; -use breakwater_core::{framebuffer::FrameBuffer, HELP_TEXT}; +use breakwater_core::{framebuffer::FrameBuffer, ALT_HELP_TEXT, HELP_TEXT}; use tokio::io::AsyncWriteExt; use crate::{Parser, ParserError}; @@ -38,6 +38,7 @@ impl Parser for OriginalParser { mut stream: impl AsyncWriteExt + Send + Unpin, ) -> Result { let mut last_byte_parsed = 0; + let mut help_count = 0; let mut i = 0; // We can't use a for loop here because Rust don't lets use skip characters by incrementing i let loop_end = buffer.len().saturating_sub(PARSER_LOOKAHEAD); // Let's extract the .len() call and the subtraction into it's own variable so we only compute it once @@ -178,10 +179,20 @@ impl Parser for OriginalParser { i += 4; last_byte_parsed = i - 1; - stream - .write_all(HELP_TEXT) - .await - .expect("Failed to write bytes to tcp socket"); + #[allow(clippy::comparison_chain)] + if help_count < 3 { + stream + .write_all(HELP_TEXT) + .await + .expect("Failed to write bytes to tcp socket"); + help_count += 1; + } else if help_count == 3 { + stream + .write_all(ALT_HELP_TEXT) + .await + .expect("Failed to write bytes to tcp socket"); + help_count += 1; + } continue; }