Added rejoin tokens

This commit is contained in:
Arthur Beck 2025-03-04 16:16:16 -06:00
parent 25de01961f
commit 80e2e4044c
Signed by: ArthurB
GPG key ID: ACE3D14F5CEF14BF

View file

@ -30,6 +30,7 @@ impl UserStatus {
struct User { struct User {
name: String, name: String,
status: UserStatus, status: UserStatus,
token: String,
} }
#[derive(Clone)] #[derive(Clone)]
@ -74,12 +75,60 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
i = 0; i = 0;
continue; continue;
} else if buf1[0] == b'\n' { } else if buf1[0] == b'\n' {
let new_name = String::from_utf8_lossy(&(buf[..i])).to_string();
if new_name.to_lowercase() == "netbot" {
stream.write_all(
b"\x1b[0m\x1b[31;1mNetbot: Hey! Rude.\n \
\x1b[0m\x1b[33mEnter your chosen nickname(up to 30 characters):\x1b[0m\x1b[4m\n"
).unwrap();
buf = [0u8; 30];
buf1[0] = b'\0';
i = 0;
continue 'bufreadloop;
}
for user in &channel.lock().unwrap().online_users { for user in &channel.lock().unwrap().online_users {
if user.name == String::from_utf8_lossy(&(buf[..i])).to_string() if user.name == new_name {
&& user.status != UserStatus::DISCONNECTED if user.status == UserStatus::DISCONNECTED {
{ stream
.write_all(b"\x1b[0m\x1b[33mEnter your login token(up to 50 characters):\x1b[0m\x1b[4m\n")
.unwrap();
let mut buf = [0u8; 50];
let mut i = 0usize;
let mut buf1 = [0u8];
'bufreadloop2: while buf1[0] != b'\n' || i == 0 {
stream.read_exact(&mut buf1).unwrap();
if buf1[0] == b'\n' && i >= 50 {
stream.write_all(
b"\x1b[0m\x1b[31;1mEnter less than 50 characters.\n \
\x1b[0m\x1b[33mEnter your login token(up to 50 characters):\x1b[0m\x1b[4m\n"
).unwrap();
buf = [0u8; 50];
buf1[0] = b'\0';
i = 0;
continue;
} else if buf1[0] == b'\n' {
if user.token != String::from_utf8_lossy(&(buf[..i])) {
break 'bufreadloop2;
}
let mut i = 0usize;
for user in &channel.lock().unwrap().online_users {
if user.name == new_name {
channel.lock().unwrap().online_users.swap_remove(i);
break;
}
i += 1;
}
break 'bufreadloop;
}
if i < 50 {
buf[i] = buf1[0];
i += 1;
}
}
}
stream.write_all( stream.write_all(
b"\x1b[0m\x1b[31;1mSorry, but that nickname is taken.\n\x1b[0m\x1b[33mEnter your chosen nickname(up to 30 characters):\x1b[0m\x1b[4m\n" b"\x1b[0m\x1b[31;1mSorry, but that nickname is taken.\n \
\x1b[0m\x1b[33mEnter your chosen nickname(up to 30 characters):\x1b[0m\x1b[4m\n"
).unwrap(); ).unwrap();
buf = [0u8; 30]; buf = [0u8; 30];
buf1[0] = b'\0'; buf1[0] = b'\0';
@ -106,11 +155,20 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
let mut user = User { let mut user = User {
name: String::from_utf8_lossy(&(buf[..i])).to_string(), name: String::from_utf8_lossy(&(buf[..i])).to_string(),
status: UserStatus::ONLINE, status: UserStatus::ONLINE,
token: format!("{:x}", random::random::<u128>()),
}; };
stream
.write_fmt(format_args!(
"\x1b[0m\x1b[32;1mYour token to rejoin after quitting is {}.\x1b[0m\n",
user.token
))
.unwrap();
let netbot = User { let netbot = User {
name: "Netbot".to_owned(), name: "Netbot".to_owned(),
status: UserStatus::ONLINE, status: UserStatus::ONLINE,
token: "".to_owned(),
}; };
channel.lock().unwrap().online_users.push(user.clone()); channel.lock().unwrap().online_users.push(user.clone());
@ -183,6 +241,7 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
for usr in &mut channel.lock().unwrap().online_users { for usr in &mut channel.lock().unwrap().online_users {
if usr.name == user.name { if usr.name == user.name {
usr.status = user.status.clone(); usr.status = user.status.clone();
break;
} }
} }
@ -195,7 +254,6 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
recieve_terminate_send.send(()).unwrap(); recieve_terminate_send.send(()).unwrap();
let _ = recieve_thread.join(); let _ = recieve_thread.join();
stream.shutdown(std::net::Shutdown::Both).unwrap();
return; return;
} }
} }