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]);
|
println!("err: {:x?}", msg_bytes[1]);
|
||||||
match msg_bytes[1] {
|
match msg_bytes[1] {
|
||||||
codes::error::INVALID_ROOM => {
|
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 => {
|
codes::error::NICKNAME_COLLISION => {
|
||||||
println!(
|
println!(
|
||||||
@ -93,6 +93,7 @@ fn join(nick: &str, room: &str, stream: &mut TcpStream) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn show(stream: &mut TcpStream) {}
|
fn show(stream: &mut TcpStream) {}
|
||||||
|
|
||||||
fn leave(nick: &str, room: &str, stream: &mut TcpStream) {
|
fn leave(nick: &str, room: &str, stream: &mut TcpStream) {
|
||||||
let size = room.to_string().capacity() + nick.to_string().capacity() + 2;
|
let size = room.to_string().capacity() + nick.to_string().capacity() + 2;
|
||||||
let mut out_buf: Vec<u8> = vec![0; size];
|
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);
|
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() {
|
pub fn start() {
|
||||||
println!("Starting the IRC client. No spaces allowed in nicknames or room names");
|
println!("Starting the IRC client. No spaces allowed in nicknames or room names");
|
||||||
let mut nick: String;
|
let mut nick: String;
|
||||||
@ -151,6 +164,11 @@ pub fn start() {
|
|||||||
"/quit" => disconnect(),
|
"/quit" => disconnect(),
|
||||||
"/rooms" => rooms(&mut stream),
|
"/rooms" => rooms(&mut stream),
|
||||||
"/users" => users(&mut stream),
|
"/users" => users(&mut stream),
|
||||||
|
"/list" => {
|
||||||
|
let room = *cmds.get(1).unwrap();
|
||||||
|
list(room, &mut stream);
|
||||||
|
|
||||||
|
}
|
||||||
"/join" => {
|
"/join" => {
|
||||||
let room = *cmds.get(1).unwrap();
|
let room = *cmds.get(1).unwrap();
|
||||||
join(&nick, room, &mut stream)
|
join(&nick, room, &mut stream)
|
||||||
|
@ -8,12 +8,14 @@ pub mod codes {
|
|||||||
pub const SEND_MESSAGE: u8 = 0x05;
|
pub const SEND_MESSAGE: u8 = 0x05;
|
||||||
pub const REGISTER_NICK: u8 = 0x06;
|
pub const REGISTER_NICK: u8 = 0x06;
|
||||||
pub const LIST_USERS: u8 = 0x07;
|
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 KEEP_ALIVE: u8 = 0x0C;
|
||||||
pub const RESPONSE: u8 = 0x0D;
|
pub const RESPONSE: u8 = 0x0D;
|
||||||
pub const RESPONSE_OK: u8 = 0x0E;
|
pub const RESPONSE_OK: u8 = 0x0E;
|
||||||
pub const ERROR: u8 = 0x0F;
|
pub const ERROR: u8 = 0x0F;
|
||||||
pub const QUIT: u8 = 0x0B;
|
|
||||||
pub mod error {
|
pub mod error {
|
||||||
pub const INVALID_ROOM: u8 = 0x10;
|
pub const INVALID_ROOM: u8 = 0x10;
|
||||||
pub const NICKNAME_COLLISION: u8 = 0x11;
|
pub const NICKNAME_COLLISION: u8 = 0x11;
|
||||||
|
@ -61,6 +61,25 @@ fn handle_client(
|
|||||||
stream.write(&buf_out);
|
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 => {
|
codes::client::JOIN_ROOM => {
|
||||||
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
||||||
let params: Vec<&str> = p.split(' ').collect();
|
let params: Vec<&str> = p.split(' ').collect();
|
||||||
@ -129,6 +148,7 @@ fn leave_room(server: &Arc<Mutex<Server>>, user: &str, room: &str, stream: &mut
|
|||||||
l.retain(|item| item != user);
|
l.retain(|item| item != user);
|
||||||
if l.len() == 0 {
|
if l.len() == 0 {
|
||||||
unlocked_server.rooms.remove(room);
|
unlocked_server.rooms.remove(room);
|
||||||
|
stream.write_all(&[codes::RESPONSE_OK]);
|
||||||
} else if l.len() == before_len {
|
} else if l.len() == before_len {
|
||||||
stream.write_all(&[codes::ERROR, codes::error::INVALID_ROOM]);
|
stream.write_all(&[codes::ERROR, codes::error::INVALID_ROOM]);
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user