register and login command and some fixes i apologize

This commit is contained in:
MeexReay 2025-04-15 02:15:05 +03:00
parent 41340f0f45
commit b99311bd7c
4 changed files with 117 additions and 82 deletions

View File

@ -1,11 +1,13 @@
use std::{
error::Error, io::{stdout, Write},
error::Error,
sync::{atomic::{AtomicUsize, Ordering}, Arc, RwLock},
time::{SystemTime, UNIX_EPOCH}
};
use colored::{Color, Colorize};
use crate::proto::{register_user, send_message_auth};
use super::{
proto::{connect, read_messages, send_message, send_message_spoof_auth},
util::sanitize_text,
@ -18,6 +20,7 @@ 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
@ -29,12 +32,12 @@ lazy_static! {
#[cfg(not(feature = "pretty"))]
pub mod minimal_tui;
#[cfg(not(feature = "pretty"))]
pub use minimal_tui::run_main_loop;
pub use minimal_tui::{run_main_loop, update_console};
#[cfg(feature = "pretty")]
pub mod pretty_tui;
#[cfg(feature = "pretty")]
pub use pretty_tui::run_main_loop;
pub use pretty_tui::{run_main_loop, update_console};
pub struct ChatStorage {
@ -81,51 +84,76 @@ impl ChatStorage {
}
const HELP_MESSAGE: &str = "Help message:\r
/help - show help message\r
/clear n - send empty message n times\r
/spam n text - send message with text n times\r
/ping - check server ping\r
\r
Press enter to close";
const HELP_MESSAGE: &str = "Help message:
/help - show help message
/register password - register user
/login password - login user
/clear n - send empty message n times
/spam n text - send message with text n times
/ping - check server ping";
pub fn add_message(ctx: Arc<Context>, message: &str) -> Result<(), Box<dyn Error>> {
ctx.messages.append(
ctx.max_messages,
message.split("\n").map(|o| o.to_string()).collect::<Vec<String>>()
);
update_console(ctx)
}
pub fn on_command(ctx: Arc<Context>, command: &str) -> Result<(), Box<dyn Error>> {
let command = command.trim_start_matches("/");
let (command, args) = command.split_once(" ").unwrap_or((&command, ""));
let args = args.split(" ").collect::<Vec<&str>>();
let response = move || -> Option<String> {
if command == "clear" {
let times = args.get(0)?.parse().ok()?;
let Some(times) = args.get(0) else { return Ok(()) };
let times = times.parse()?;
for _ in 0..times {
send_message(&mut connect(&ctx.host, ctx.enable_ssl).ok()?, "\r").ok()?;
send_message(&mut connect(&ctx.host, ctx.enable_ssl)?, "\r")?;
}
None
} else if command == "spam" {
let times = args.get(0)?.parse().ok()?;
let Some(times) = args.get(0) else { return Ok(()) };
let times = times.parse()?;
let msg = args[1..].join(" ");
for _ in 0..times {
send_message(&mut connect(&ctx.host, ctx.enable_ssl).ok()?, &("\r".to_string()+&msg)).ok()?;
send_message(&mut connect(&ctx.host, ctx.enable_ssl)?, &("\r".to_string()+&msg))?;
}
None
// send_message(&mut connect(&ctx.host, ctx.enable_ssl)?,
// &prepare_message(ctx.clone(),
// &format!("\r\x1B[1A{}{}", args.join(" "), " ".repeat(10)).repeat(ctx.max_messages)
// ))?;
} else if command == "help" {
Some(HELP_MESSAGE.to_string())
add_message(ctx.clone(), HELP_MESSAGE)?;
} else if command == "register" {
let Some(pass) = args.get(0) else {
add_message(ctx.clone(), "please provide password as the first argument")?;
return Ok(())
};
match register_user(&mut connect(&ctx.host, ctx.enable_ssl)?, &ctx.name, pass) {
Ok(true) => {
add_message(ctx.clone(), "you was registered successfully bro")?;
*ctx.registered.write().unwrap() = Some(pass.to_string());
},
Ok(false) => add_message(ctx.clone(), "user with this account already exists bruh")?,
Err(e) => add_message(ctx.clone(), &format!("ERROR while registrationing: {}", e))?
};
} else if command == "login" {
let Some(pass) = args.get(0) else {
add_message(ctx.clone(), "please provide password as the first argument")?;
return Ok(())
};
add_message(ctx.clone(), "ye bro you was logged in")?;
*ctx.registered.write().unwrap() = Some(pass.to_string());
} else if command == "ping" {
let mut before = ctx.messages.packet_size();
let message = format!("Checking ping... {:X}", SystemTime::now().duration_since(UNIX_EPOCH).ok()?.as_millis());
let message = format!("Checking ping... {:X}", SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis());
send_message(&mut connect(&ctx.host, ctx.enable_ssl).ok()?, &message).ok()?;
send_message(&mut connect(&ctx.host, ctx.enable_ssl)?, &message)?;
let start = SystemTime::now();
loop {
let data = read_messages(
&mut connect(&ctx.host, ctx.enable_ssl).ok()?,
&mut connect(&ctx.host, ctx.enable_ssl)?,
ctx.max_messages,
before,
!ctx.enable_ssl,
@ -145,17 +173,9 @@ pub fn on_command(ctx: Arc<Context>, command: &str) -> Result<(), Box<dyn Error>
}
}
send_message(&mut connect(&ctx.host, ctx.enable_ssl).ok()?, &format!("Ping = {}ms", start.elapsed().unwrap().as_millis())).ok()?;
None
add_message(ctx.clone(), &format!("Ping = {}ms", start.elapsed().unwrap().as_millis()))?;
} else {
None
}
}();
if let Some(response) = response {
write!(stdout(), "{}", response)?;
stdout().flush()?;
add_message(ctx.clone(), "Unknown command bruh")?;
}
Ok(())
@ -198,7 +218,9 @@ pub fn on_send_message(ctx: Arc<Context>, message: &str) -> Result<(), Box<dyn E
.replace("{text}", &message)
);
if ctx.enable_auth {
if let Some(password) = ctx.registered.read().unwrap().clone() {
send_message_auth(&mut connect(&ctx.host, ctx.enable_ssl)?, &ctx.name, &password, &message)?;
} else if ctx.enable_auth {
send_message_spoof_auth(&mut connect(&ctx.host, ctx.enable_ssl)?, &message)?;
} else {
send_message(&mut connect(&ctx.host, ctx.enable_ssl)?, &message)?;
@ -269,7 +291,7 @@ pub fn format_message(enable_ip_viewing: bool, message: String) -> Option<String
}.unwrap_or_else(|| {
format!(
"{}",
message.white().blink()
message.bright_white()
)
}))
}

View File

@ -1,6 +1,7 @@
use std::sync::Arc;
use std::io::stdout;
use std::io::Write;
use std::error::Error;
use colored::Colorize;
@ -12,6 +13,26 @@ use super::{
}, format_message, on_send_message
};
pub fn update_console(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
let messages = ctx.messages.messages();
let mut out = stdout().lock();
write!(
out,
"{}\n{}\n{} ",
"\n".repeat(ctx.max_messages - messages.len()),
messages
.into_iter()
.map(|o| o.white().blink().to_string())
.collect::<Vec<String>>()
.join("\n"),
">".bright_yellow()
);
out.flush();
Ok(())
}
pub fn run_main_loop(ctx: Arc<Context>) {
loop {
match connect(&ctx.host, ctx.enable_ssl) {
@ -49,21 +70,7 @@ pub fn run_main_loop(ctx: Arc<Context>) {
}
}
let messages = ctx.messages.messages();
let mut out = stdout().lock();
write!(
out,
"{}\n{}\n{} ",
"\n".repeat(ctx.max_messages - messages.len()),
messages
.into_iter()
.map(|o| o.white().blink().to_string())
.collect::<Vec<String>>()
.join("\n"),
">".bright_yellow()
);
out.flush();
let _ = update_console(ctx.clone());
if let Some(message) = get_input("") {
if let Err(e) = on_send_message(ctx.clone(), &message) {

View File

@ -353,6 +353,10 @@ pub fn on_close() {
execute!(stdout(), event::DisableMouseCapture).unwrap();
}
pub fn update_console(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
print_console(ctx.clone(), ctx.messages.messages(), &ctx.input.read().unwrap())
}
pub fn run_main_loop(ctx: Arc<Context>) {
enable_raw_mode().unwrap();
execute!(stdout(), event::EnableMouseCapture).unwrap();

View File

@ -213,6 +213,7 @@ pub struct Context {
pub input: Arc<RwLock<String>>,
pub host: String,
pub name: String,
pub registered: Arc<RwLock<Option<String>>>,
pub disable_formatting: bool,
pub disable_commands: bool,
pub disable_hiding_ip: bool,
@ -234,6 +235,7 @@ impl Context {
message_format: args.message_format.clone().unwrap_or(config.message_format.clone()),
host: args.host.clone().unwrap_or(config.host.clone()),
name: args.name.clone().or(config.name.clone()).unwrap_or_else(|| ask_string("Name", format!("Anon#{:X}", random::<u16>()))),
registered: Arc::new(RwLock::new(None)),
disable_formatting: args.disable_formatting,
disable_commands: args.disable_commands,
disable_hiding_ip: args.disable_ip_hiding,