support LIST_USERS_IN_ROOM
This commit is contained in:
parent
b7c7be8dca
commit
9f1f23623d
@ -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<u8> = 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<u8> = 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)
|
||||
|
@ -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;
|
||||
|
@ -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<u8> = 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<Mutex<Server>>, 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 {
|
||||
|
Reference in New Issue
Block a user