diff --git a/src/chat.rs b/src/chat.rs index fce3c7d..fdb69ed 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -4,7 +4,7 @@ use colored::{Color, Colorize}; use crossterm::{cursor::{MoveLeft, MoveRight}, event::{self, Event, KeyCode, KeyModifiers, MouseEventKind}, terminal::{self, disable_raw_mode, enable_raw_mode}}; use rand::random; -use crate::{util::string_chunks, IP_REGEX}; +use crate::{proto::send_message_auth, util::string_chunks, IP_REGEX}; use super::{proto::read_messages, util::sanitize_text, COLORED_USERNAMES, DATE_REGEX, config::Context, proto::send_message}; @@ -269,10 +269,14 @@ fn poll_events(ctx: Arc) -> Result<(), Box> { if message.starts_with("/") && !ctx.disable_commands { on_command(ctx.clone(), &message)?; } else { - let message = ctx.message_format - .replace("{name}", &ctx.name) - .replace("{text}", &message); - send_message(&ctx.host, &message)?; + if let Some(password) = &ctx.auth_password { + send_message_auth(&ctx.host, &ctx.name, password, &message)?; + } else { + let message = ctx.message_format + .replace("{name}", &ctx.name) + .replace("{text}", &message); + send_message(&ctx.host, &message)?; + } } } else { print_console( diff --git a/src/config.rs b/src/config.rs index a98e30b..8eb5e2d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -169,6 +169,10 @@ pub struct Args { /// Configure client #[arg(short='C', long)] pub configure: bool, + + /// Authentication password + #[arg(short='a', long, value_name="PASSWORD")] + pub auth: Option, } pub struct Context { @@ -183,7 +187,8 @@ pub struct Context { pub update_time: usize, pub max_messages: usize, pub enable_ip_viewing: bool, - pub scroll: Arc + pub scroll: Arc, + pub auth_password: Option, } impl Context { @@ -206,7 +211,8 @@ impl Context { update_time: config.update_time, max_messages: config.max_messages, enable_ip_viewing: args.enable_users_ip_viewing || config.enable_ip_viewing, - scroll: Arc::new(AtomicUsize::new(0)) + scroll: Arc::new(AtomicUsize::new(0)), + auth_password: args.auth.clone() } } } \ No newline at end of file diff --git a/src/proto.rs b/src/proto.rs index 9bfd9ca..b314814 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -9,6 +9,40 @@ pub fn send_message(host: &str, message: &str) -> Result<(), Box> { Ok(()) } +pub fn send_message_auth(host: &str, name: &str, password: &str, message: &str) -> Result<(), Box> { + let mut stream = TcpStream::connect(host)?; + stream.write_all(&[0x02])?; + stream.write_all(name.as_bytes())?; + stream.write_all(&[b'\n'])?; + stream.write_all(password.as_bytes())?; + stream.write_all(&[b'\n'])?; + stream.write_all(message.as_bytes())?; + let mut buf = vec![0; 1]; + stream.read_exact(&mut buf)?; + if buf[0] == 0x01 { + register_user(host, name, password)?; + send_message_auth(host, name, password, message)? + } else if buf[0] == 0x02 { + println!("Password is incorrect"); + panic!() + } + Ok(()) +} + +pub fn register_user(host: &str, name: &str, password: &str) -> Result<(), Box> { + let mut stream = TcpStream::connect(host)?; + stream.write_all(&[0x00])?; + stream.write_all(name.as_bytes())?; + stream.write_all(&[b'\n'])?; + stream.write_all(password.as_bytes())?; + // let mut buf = vec![0; 1]; + // stream.read(&mut buf)?; + // if buf[0] == 0x01 { + // // похуй + // } + Ok(()) +} + fn skip_null(stream: &mut TcpStream) -> Result, Box> { loop { let mut buf = vec![0; 1];