move format_message and sanitize_text from old bRAC version

This commit is contained in:
MeexReay 2025-06-18 06:41:40 +03:00
parent 817f09f771
commit f262536e39
5 changed files with 107 additions and 77 deletions

77
Cargo.lock generated
View File

@ -76,12 +76,6 @@ dependencies = [
"windows-sys 0.59.0", "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]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.4.0" version = "1.4.0"
@ -111,20 +105,6 @@ dependencies = [
"fs_extra", "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]] [[package]]
name = "bindgen" name = "bindgen"
version = "0.69.5" version = "0.69.5"
@ -387,12 +367,6 @@ dependencies = [
"log", "log",
] ]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.11" version = "0.3.11"
@ -454,12 +428,6 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.5.0" version = "0.5.0"
@ -516,16 +484,6 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "indexmap"
version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]] [[package]]
name = "is_terminal_polyfill" name = "is_terminal_polyfill"
version = "1.70.1" version = "1.70.1"
@ -619,16 +577,6 @@ dependencies = [
"windows-targets", "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]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.15" version = "0.4.15"
@ -877,23 +825,19 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]] [[package]]
name = "sRAC" name = "sRAC"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bRAC",
"chrono", "chrono",
"clap", "clap",
"colog", "colog",
"colored 3.0.0",
"lazy_static",
"log", "log",
"md-5", "md-5",
"rand", "rand",
"regex",
"rustls", "rustls",
"tungstenite", "tungstenite",
] ]
@ -918,21 +862,6 @@ dependencies = [
"syn", "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]] [[package]]
name = "sha1" name = "sha1"
version = "0.10.6" version = "0.10.6"

View File

@ -5,7 +5,6 @@ edition = "2024"
[dependencies] [dependencies]
chrono = "0.4.40" 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" md-5 = "0.10.6"
rand = "0.9.0" rand = "0.9.0"
clap = { version = "4.5.36", features = ["derive"] } clap = { version = "4.5.36", features = ["derive"] }
@ -13,3 +12,6 @@ rustls = "0.23.25"
tungstenite = "0.27.0" tungstenite = "0.27.0"
colog = "1.3.0" colog = "1.3.0"
log = "0.4.27" log = "0.4.27"
regex = "1.11.1"
colored = "3.0.0"
lazy_static = "1.5.0"

View File

@ -11,13 +11,15 @@ use std::{
time::Duration, time::Duration,
}; };
use bRAC::{chat::format_message, util::sanitize_text};
use chrono::{DateTime, Local, TimeZone}; use chrono::{DateTime, Local, TimeZone};
use log::info; use log::info;
use md5::{Digest, Md5}; use md5::{Digest, Md5};
use rand::{Rng, distr::Alphanumeric}; use rand::{Rng, distr::Alphanumeric};
use crate::Args; use crate::{
Args,
util::{format_message, sanitize_text},
};
fn load_accounts(accounts_file: Option<String>) -> Vec<Account> { fn load_accounts(accounts_file: Option<String>) -> Vec<Account> {
if let Some(accounts_file) = accounts_file.clone() { if let Some(accounts_file) = accounts_file.clone() {

View File

@ -8,6 +8,7 @@ use crate::{ctx::Context, proto::run_listener};
pub mod ctx; pub mod ctx;
pub mod logic; pub mod logic;
pub mod proto; pub mod proto;
pub mod util;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version)] #[command(version)]

96
src/util.rs Normal file
View File

@ -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<String> {
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
}