/me and other stuff

This commit is contained in:
Arthur Beck 2025-03-04 17:54:42 +00:00
parent 65086a3b3c
commit 13dab47532

View file

@ -37,6 +37,7 @@ struct Message {
sender: User, sender: User,
contents: String, contents: String,
timestamp: time::OffsetDateTime, timestamp: time::OffsetDateTime,
me_message: bool
} }
type Channel = Arc<Mutex<UnwrappedChannel>>; type Channel = Arc<Mutex<UnwrappedChannel>>;
@ -134,6 +135,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
if current_message_count != new_message_count { if current_message_count != new_message_count {
let new_messages = &channel.lock().unwrap().messages[current_message_count..]; let new_messages = &channel.lock().unwrap().messages[current_message_count..];
for msg in new_messages { for msg in new_messages {
if !msg.me_message {
stream stream
.write_fmt(format_args!( .write_fmt(format_args!(
"\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n", "\x1b[0m\x1b[34m[{}]\x1b[0m \x1b[1m{}\x1b[0m \x1b[31m⇛\x1b[0m \x1b[35m{}\x1b[0m\n",
@ -142,6 +144,16 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
msg.contents msg.contents
)) ))
.unwrap(); .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; 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::<u8>::new(); let mut buf = Vec::<u8>::new();
let mut buf1 = [0u8]; let mut buf1 = [0u8];
@ -171,6 +190,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
sender: netbot.clone(), sender: netbot.clone(),
contents: format!("User {} has left.", user.name), contents: format!("User {} has left.", user.name),
timestamp: time::OffsetDateTime::now_local().unwrap(), timestamp: time::OffsetDateTime::now_local().unwrap(),
me_message: false
}); });
recieve_terminate_send.send(()).unwrap(); recieve_terminate_send.send(()).unwrap();
@ -191,6 +211,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
sender: user.clone(), sender: user.clone(),
contents, contents,
timestamp: time::OffsetDateTime::now_local().unwrap(), timestamp: time::OffsetDateTime::now_local().unwrap(),
me_message: false
}); });
} else { } else {
if contents.split(" ").collect::<Vec<&str>>()[0] == "/help" || contents.split(" ").collect::<Vec<&str>>()[0] == "/?" { if contents.split(" ").collect::<Vec<&str>>()[0] == "/help" || contents.split(" ").collect::<Vec<&str>>()[0] == "/?" {
@ -201,9 +222,25 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
/status [online|idle|disconnected|(any value)]: Set your status.\n \ /status [online|idle|disconnected|(any value)]: Set your status.\n \
/user [nick]: Get information about a user.\n \ /user [nick]: Get information about a user.\n \
/server [new name]: Get or set the server name.\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(); .unwrap();
} else if contents.split(" ").collect::<Vec<&str>>()[0] == "/me" {
if contents.split(" ").collect::<Vec<&str>>().len() < 2 || contents.split(" ").collect::<Vec<&str>>()[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::<Vec<&str>>()[0] == "/status" || contents == "/user" { } else if contents.split(" ").collect::<Vec<&str>>()[0] == "/status" || contents == "/user" {
let contents = contents.split(" ").collect::<Vec<&str>>(); let contents = contents.split(" ").collect::<Vec<&str>>();
if contents.len() == 1 { if contents.len() == 1 {