Added rejoin tokens
This commit is contained in:
parent
25de01961f
commit
80e2e4044c
1 changed files with 63 additions and 5 deletions
70
src/main.rs
70
src/main.rs
|
@ -30,6 +30,7 @@ impl UserStatus {
|
|||
struct User {
|
||||
name: String,
|
||||
status: UserStatus,
|
||||
token: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
@ -74,12 +75,60 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
|
|||
i = 0;
|
||||
continue;
|
||||
} else if buf1[0] == b'\n' {
|
||||
for user in &channel.lock().unwrap().online_users {
|
||||
if user.name == String::from_utf8_lossy(&(buf[..i])).to_string()
|
||||
&& user.status != UserStatus::DISCONNECTED
|
||||
{
|
||||
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;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;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 {
|
||||
if user.name == new_name {
|
||||
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(
|
||||
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();
|
||||
buf = [0u8; 30];
|
||||
buf1[0] = b'\0';
|
||||
|
@ -106,11 +155,20 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
|
|||
let mut user = User {
|
||||
name: String::from_utf8_lossy(&(buf[..i])).to_string(),
|
||||
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 {
|
||||
name: "Netbot".to_owned(),
|
||||
status: UserStatus::ONLINE,
|
||||
token: "".to_owned(),
|
||||
};
|
||||
|
||||
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 {
|
||||
if usr.name == user.name {
|
||||
usr.status = user.status.clone();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -195,7 +254,6 @@ fn handle_client(mut stream: TcpStream, channel: Channel) {
|
|||
|
||||
recieve_terminate_send.send(()).unwrap();
|
||||
let _ = recieve_thread.join();
|
||||
stream.shutdown(std::net::Shutdown::Both).unwrap();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue