From 13dab475323bda43cfd62ff99962624cbaccb6c5 Mon Sep 17 00:00:00 2001 From: Arthur Beck Date: Tue, 4 Mar 2025 17:54:42 +0000 Subject: [PATCH] /me and other stuff --- src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1e7becf..da27753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ struct Message { sender: User, contents: String, timestamp: time::OffsetDateTime, + me_message: bool } type Channel = Arc>; @@ -134,14 +135,25 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { if current_message_count != new_message_count { let new_messages = &channel.lock().unwrap().messages[current_message_count..]; for msg in new_messages { - stream - .write_fmt(format_args!( - "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n", - msg.timestamp.format(&time_format).unwrap(), - msg.sender.name, - msg.contents - )) - .unwrap(); + if !msg.me_message { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n", + msg.timestamp.format(&time_format).unwrap(), + msg.sender.name, + msg.contents + )) + .unwrap(); + } else { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1;3m{}\x1b[0m \x1b[3m{}\x1b[0m\n", + msg.timestamp.format(&time_format).unwrap(), + msg.sender.name, + msg.contents + )) + .unwrap(); + } } current_message_count = new_message_count; @@ -154,6 +166,13 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { }); } + channel.lock().unwrap().messages.push(Message { + sender: netbot.clone(), + contents: format!("User {} has joined!", user.name), + timestamp: time::OffsetDateTime::now_local().unwrap(), + me_message: false + }); + let mut buf = Vec::::new(); let mut buf1 = [0u8]; @@ -171,6 +190,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { sender: netbot.clone(), contents: format!("User {} has left.", user.name), timestamp: time::OffsetDateTime::now_local().unwrap(), + me_message: false }); recieve_terminate_send.send(()).unwrap(); @@ -191,6 +211,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { sender: user.clone(), contents, timestamp: time::OffsetDateTime::now_local().unwrap(), + me_message: false }); } else { if contents.split(" ").collect::>()[0] == "/help" || contents.split(" ").collect::>()[0] == "/?" { @@ -201,9 +222,25 @@ fn handle_client(mut stream: TcpStream, channel: Channel) { /status [online|idle|disconnected|(any value)]: Set your status.\n \ /user [nick]: Get information about a user.\n \ /server [new name]: Get or set the server name.\n \ - /users: List users.\x1b[0m\n", + /users: List users.\n \ + /me x: Say that you are doing x(for example, \"Netbot purrs\").\x1b[0m\n", ) .unwrap(); + } else if contents.split(" ").collect::>()[0] == "/me" { + if contents.split(" ").collect::>().len() < 2 || contents.split(" ").collect::>()[1] == "" { + stream + .write_fmt(format_args!( + "\x1b[0m\x1b[31mYou need to provide at least one argument to /me!\n\x1b[0m", + )) + .unwrap(); + } else { + channel.lock().unwrap().messages.push(Message { + sender: user.clone(), + contents: contents.replace("/me ", ""), + timestamp: time::OffsetDateTime::now_local().unwrap(), + me_message: true + }); + } } else if contents.split(" ").collect::>()[0] == "/status" || contents == "/user" { let contents = contents.split(" ").collect::>(); if contents.len() == 1 {