From f262536e396638d7019ff58054ff078a790204cf Mon Sep 17 00:00:00 2001 From: MeexReay Date: Wed, 18 Jun 2025 06:41:40 +0300 Subject: [PATCH] move format_message and sanitize_text from old bRAC version --- Cargo.lock | 77 ++---------------------------------------- Cargo.toml | 4 ++- src/ctx.rs | 6 ++-- src/main.rs | 1 + src/util.rs | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 77 deletions(-) create mode 100644 src/util.rs diff --git a/Cargo.lock b/Cargo.lock index 22d2fb2..d0e2491 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,12 +76,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "anyhow" -version = "1.0.98" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" - [[package]] name = "autocfg" version = "1.4.0" @@ -111,20 +105,6 @@ dependencies = [ "fs_extra", ] -[[package]] -name = "bRAC" -version = "0.1.2+2.0" -source = "git+https://github.com/MeexReay/bRAC.git?tag=0.1.2%2B2.0#52720c2748c3153f5ada996cc2b32366a9397549" -dependencies = [ - "clap", - "colored 3.0.0", - "lazy_static", - "rand", - "regex", - "serde", - "serde_yml", -] - [[package]] name = "bindgen" version = "0.69.5" @@ -387,12 +367,6 @@ dependencies = [ "log", ] -[[package]] -name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - [[package]] name = "errno" version = "0.3.11" @@ -454,12 +428,6 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" -[[package]] -name = "hashbrown" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" - [[package]] name = "heck" version = "0.5.0" @@ -516,16 +484,6 @@ dependencies = [ "cc", ] -[[package]] -name = "indexmap" -version = "2.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" -dependencies = [ - "equivalent", - "hashbrown", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -619,16 +577,6 @@ dependencies = [ "windows-targets", ] -[[package]] -name = "libyml" -version = "0.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3302702afa434ffa30847a83305f0a69d6abd74293b6554c18ec85c7ef30c980" -dependencies = [ - "anyhow", - "version_check", -] - [[package]] name = "linux-raw-sys" version = "0.4.15" @@ -877,23 +825,19 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" - [[package]] name = "sRAC" version = "0.1.0" dependencies = [ - "bRAC", "chrono", "clap", "colog", + "colored 3.0.0", + "lazy_static", "log", "md-5", "rand", + "regex", "rustls", "tungstenite", ] @@ -918,21 +862,6 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_yml" -version = "0.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59e2dd588bf1597a252c3b920e0143eb99b0f76e4e082f4c92ce34fbc9e71ddd" -dependencies = [ - "indexmap", - "itoa", - "libyml", - "memchr", - "ryu", - "serde", - "version_check", -] - [[package]] name = "sha1" version = "0.10.6" diff --git a/Cargo.toml b/Cargo.toml index 51e566c..98c17c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,6 @@ edition = "2024" [dependencies] chrono = "0.4.40" -bRAC = { git = "https://github.com/MeexReay/bRAC.git", default-features = false, tag = "0.1.2+2.0" } md-5 = "0.10.6" rand = "0.9.0" clap = { version = "4.5.36", features = ["derive"] } @@ -13,3 +12,6 @@ rustls = "0.23.25" tungstenite = "0.27.0" colog = "1.3.0" log = "0.4.27" +regex = "1.11.1" +colored = "3.0.0" +lazy_static = "1.5.0" \ No newline at end of file diff --git a/src/ctx.rs b/src/ctx.rs index 7027071..cc5e82a 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -11,13 +11,15 @@ use std::{ time::Duration, }; -use bRAC::{chat::format_message, util::sanitize_text}; use chrono::{DateTime, Local, TimeZone}; use log::info; use md5::{Digest, Md5}; use rand::{Rng, distr::Alphanumeric}; -use crate::Args; +use crate::{ + Args, + util::{format_message, sanitize_text}, +}; fn load_accounts(accounts_file: Option) -> Vec { if let Some(accounts_file) = accounts_file.clone() { diff --git a/src/main.rs b/src/main.rs index a7d721f..da14c67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use crate::{ctx::Context, proto::run_listener}; pub mod ctx; pub mod logic; pub mod proto; +pub mod util; #[derive(Parser, Debug)] #[command(version)] diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..38288ad --- /dev/null +++ b/src/util.rs @@ -0,0 +1,96 @@ +use colored::{Color, Colorize}; +use lazy_static::lazy_static; +use regex::Regex; + +lazy_static! { + pub static ref DATE_REGEX: Regex = Regex::new(r"\[(.*?)\] (.*)").unwrap(); + pub static ref IP_REGEX: Regex = Regex::new(r"\{(.*?)\} (.*)").unwrap(); + pub static ref COLORED_USERNAMES: Vec<(Regex, Color)> = vec![ + (Regex::new(r"\u{B9AC}\u{3E70}<(.*?)> (.*)").unwrap(), Color::Green), // bRAC + (Regex::new(r"\u{2550}\u{2550}\u{2550}<(.*?)> (.*)").unwrap(), Color::BrightRed), // CRAB + (Regex::new(r"\u{00B0}\u{0298}<(.*?)> (.*)").unwrap(), Color::Magenta), // Mefidroniy + (Regex::new(r"<(.*?)> (.*)").unwrap(), Color::Cyan), // clRAC + ]; + pub static ref ANSI_REGEX: Regex = Regex::new(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])").unwrap(); + pub static ref CONTROL_CHARS_REGEX: Regex = Regex::new(r"[\x00-\x1F\x7F]").unwrap(); +} + +pub fn sanitize_text(input: &str) -> String { + let without_ansi = ANSI_REGEX.replace_all(input, ""); + let cleaned_text = CONTROL_CHARS_REGEX.replace_all(&without_ansi, ""); + cleaned_text.into_owned() +} + +pub fn format_message(enable_ip_viewing: bool, message: String) -> Option { + let message = sanitize_text(&message); + + let date = DATE_REGEX.captures(&message)?; + let (date, message) = ( + date.get(1)?.as_str().to_string(), + date.get(2)?.as_str().to_string(), + ); + + let (ip, message) = if let Some(message) = IP_REGEX.captures(&message) { + ( + Some(message.get(1)?.as_str().to_string()), + message.get(2)?.as_str().to_string(), + ) + } else { + (None, message) + }; + + let message = message + .trim_start_matches("(UNREGISTERED)") + .trim_start_matches("(UNAUTHORIZED)") + .trim_start_matches("(UNAUTHENTICATED)") + .trim() + .to_string() + + " "; + + let prefix = if enable_ip_viewing { + if let Some(ip) = ip { + format!( + "{}{} [{}]", + ip, + " ".repeat(if 15 >= ip.chars().count() { + 15 - ip.chars().count() + } else { + 0 + }), + date + ) + } else { + format!("{} [{}]", " ".repeat(15), date) + } + } else { + format!("[{}]", date) + }; + + Some(if let Some(captures) = find_username_color(&message) { + let nick = captures.0; + let content = captures.1; + let color = captures.2; + + format!( + "{} {} {}", + prefix.white().dimmed(), + format!("<{}>", nick).color(color).bold(), + content.white().blink() + ) + } else { + format!("{} {}", prefix.white().dimmed(), message.white().blink()) + }) +} + +pub fn find_username_color(message: &str) -> Option<(String, String, Color)> { + for (re, color) in COLORED_USERNAMES.iter() { + if let Some(captures) = re.captures(message) { + return Some(( + captures[1].to_string(), + captures[2].to_string(), + color.clone(), + )); + } + } + None +}