From 9f1f23623da4eda216c0d34ff5b0394b6011fab8 Mon Sep 17 00:00:00 2001 From: David Westgate Date: Sat, 25 Nov 2023 18:09:40 -0800 Subject: [PATCH] support LIST_USERS_IN_ROOM --- src/client.rs | 20 +++++++++++++++++++- src/lib.rs | 4 +++- src/server.rs | 22 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/client.rs b/src/client.rs index dd97ecc..31fcb7e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -29,7 +29,7 @@ fn process_message(msg_bytes: &[u8]) { println!("err: {:x?}", msg_bytes[1]); match msg_bytes[1] { codes::error::INVALID_ROOM => { - println!("Attempted to message non-existant room. Try again"); + println!("Attempted to message or list non-existant room. Try again"); } codes::error::NICKNAME_COLLISION => { println!( @@ -93,6 +93,7 @@ fn join(nick: &str, room: &str, stream: &mut TcpStream) { } fn show(stream: &mut TcpStream) {} + fn leave(nick: &str, room: &str, stream: &mut TcpStream) { let size = room.to_string().capacity() + nick.to_string().capacity() + 2; let mut out_buf: Vec = vec![0; size]; @@ -112,6 +113,18 @@ fn leave(nick: &str, room: &str, stream: &mut TcpStream) { stream.write(&out_buf); } +fn list( room: &str, stream: &mut TcpStream) { + let size = room.to_string().capacity() +1; + let mut out_buf: Vec = vec![0; size]; + out_buf[0] = codes::client::LIST_USERS_IN_ROOM; + + + for i in 1..room.len()+1 { + out_buf[i] = *room.as_bytes().get(i-1).unwrap(); + } + stream.write(&out_buf); +} + pub fn start() { println!("Starting the IRC client. No spaces allowed in nicknames or room names"); let mut nick: String; @@ -151,6 +164,11 @@ pub fn start() { "/quit" => disconnect(), "/rooms" => rooms(&mut stream), "/users" => users(&mut stream), + "/list" => { + let room = *cmds.get(1).unwrap(); + list(room, &mut stream); + + } "/join" => { let room = *cmds.get(1).unwrap(); join(&nick, room, &mut stream) diff --git a/src/lib.rs b/src/lib.rs index 15c8b61..17df567 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,12 +8,14 @@ pub mod codes { pub const SEND_MESSAGE: u8 = 0x05; pub const REGISTER_NICK: u8 = 0x06; pub const LIST_USERS: u8 = 0x07; + pub const LIST_USERS_IN_ROOM: u8 = 0x08; } + pub const QUIT: u8 = 0x0B; pub const KEEP_ALIVE: u8 = 0x0C; pub const RESPONSE: u8 = 0x0D; pub const RESPONSE_OK: u8 = 0x0E; pub const ERROR: u8 = 0x0F; - pub const QUIT: u8 = 0x0B; + pub mod error { pub const INVALID_ROOM: u8 = 0x10; pub const NICKNAME_COLLISION: u8 = 0x11; diff --git a/src/server.rs b/src/server.rs index 852b412..c6775d1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -61,6 +61,25 @@ fn handle_client( stream.write(&buf_out); } + codes::client::LIST_USERS_IN_ROOM => { + let room: String = String::from_utf8_lossy(param_bytes).to_string(); + let unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap(); + let mut buf_out: Vec = Vec::new(); + buf_out.extend_from_slice(&[codes::RESPONSE]); + match unlocked_server.rooms.get(&room) { + Some(l) =>{ + for ele in l { + buf_out.extend_from_slice(ele.as_bytes()); + buf_out.extend_from_slice(&[0x20]); + } + stream.write_all(&buf_out); + }, + None =>{ + stream.write_all(&[codes::ERROR, codes::error::INVALID_ROOM]); + } + } + } + codes::client::JOIN_ROOM => { let p: String = String::from_utf8_lossy(param_bytes).to_string(); let params: Vec<&str> = p.split(' ').collect(); @@ -76,7 +95,7 @@ fn handle_client( let room = params.get(1).unwrap(); leave_room(server, user, room, stream) } - + codes::client::SEND_MESSAGE => { #[cfg(debug_assertions)] println!("SEND_MESSAGE"); @@ -129,6 +148,7 @@ fn leave_room(server: &Arc>, user: &str, room: &str, stream: &mut l.retain(|item| item != user); if l.len() == 0 { unlocked_server.rooms.remove(room); + stream.write_all(&[codes::RESPONSE_OK]); } else if l.len() == before_len { stream.write_all(&[codes::ERROR, codes::error::INVALID_ROOM]); } else {