Check for name to be ascii characters only
This commit is contained in:
parent
7dfbf7c081
commit
5a56afb709
@ -34,7 +34,6 @@ use self::chrono::Local;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// TODO: Implement requests 0.1 and 1.2
|
// TODO: Implement requests 0.1 and 1.2
|
||||||
// TODO: Limit usernames to ascii
|
|
||||||
|
|
||||||
fn hash_name(name: &str) -> usize {
|
fn hash_name(name: &str) -> usize {
|
||||||
let mut s = DefaultHasher::new();
|
let mut s = DefaultHasher::new();
|
||||||
@ -50,7 +49,7 @@ fn get_entry() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_name() -> String {
|
fn get_name() -> String {
|
||||||
loop {
|
'mainloop: loop {
|
||||||
println!("{}", "Please enter your name:".yellow().dimmed());
|
println!("{}", "Please enter your name:".yellow().dimmed());
|
||||||
let mut name = &*get_entry();
|
let mut name = &*get_entry();
|
||||||
name = name.trim();
|
name = name.trim();
|
||||||
@ -61,6 +60,17 @@ fn get_name() -> String {
|
|||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
for c in name.chars() {
|
||||||
|
if !c.is_ascii() {
|
||||||
|
println!(
|
||||||
|
"{}{}{}",
|
||||||
|
"Character ".red(),
|
||||||
|
&format!("{}", c).green(),
|
||||||
|
" is not an ASCII character.".red()
|
||||||
|
);
|
||||||
|
continue 'mainloop;
|
||||||
|
}
|
||||||
|
}
|
||||||
match name {
|
match name {
|
||||||
"" => {
|
"" => {
|
||||||
continue;
|
continue;
|
||||||
@ -193,6 +203,9 @@ fn exchange_with_server(stream: TcpStream) {
|
|||||||
let input: String = String::from(receive!());
|
let input: String = String::from(receive!());
|
||||||
let spliced_input: Vec<&str> = input.split(" ").collect();
|
let spliced_input: Vec<&str> = input.split(" ").collect();
|
||||||
match spliced_input[0] {
|
match spliced_input[0] {
|
||||||
|
"BAD" => {
|
||||||
|
println!("{}", "Bad request from client".red());
|
||||||
|
}
|
||||||
"WELCOME" => {
|
"WELCOME" => {
|
||||||
println!("{}", ">>> Login Successful <<<".green());
|
println!("{}", ">>> Login Successful <<<".green());
|
||||||
println!("Type /clients to get the list of users connected");
|
println!("Type /clients to get the list of users connected");
|
||||||
@ -217,10 +230,12 @@ fn exchange_with_server(stream: TcpStream) {
|
|||||||
name.push_str(spliced_input[1]);
|
name.push_str(spliced_input[1]);
|
||||||
name.push('>');
|
name.push('>');
|
||||||
|
|
||||||
|
// Display message with soft-wrap
|
||||||
if let Some((w, _)) = term_size::dimensions() {
|
if let Some((w, _)) = term_size::dimensions() {
|
||||||
let mut msg = String::new();
|
let mut msg = String::new();
|
||||||
let w = w - 34;
|
let w = w - 34;
|
||||||
|
|
||||||
|
// format message
|
||||||
for mut i in 3..spliced_input.len() {
|
for mut i in 3..spliced_input.len() {
|
||||||
if w > msg.len() + spliced_input[i].len() + 1 {
|
if w > msg.len() + spliced_input[i].len() + 1 {
|
||||||
msg.push(' ');
|
msg.push(' ');
|
||||||
@ -228,9 +243,10 @@ fn exchange_with_server(stream: TcpStream) {
|
|||||||
} else {
|
} else {
|
||||||
if first_line == true {
|
if first_line == true {
|
||||||
println!(
|
println!(
|
||||||
"{} {}:{}",
|
"{}{}{}{}",
|
||||||
date.format("[%H:%M:%S]").to_string().dimmed(),
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
||||||
name.color(COLORS[name_hash]),
|
name.color(COLORS[name_hash]),
|
||||||
|
" |".green(),
|
||||||
msg.yellow().dimmed()
|
msg.yellow().dimmed()
|
||||||
);
|
);
|
||||||
first_line = false;
|
first_line = false;
|
||||||
@ -249,9 +265,10 @@ fn exchange_with_server(stream: TcpStream) {
|
|||||||
|
|
||||||
if first_line == true {
|
if first_line == true {
|
||||||
println!(
|
println!(
|
||||||
"{} {}:{}",
|
"{}{}{}{}",
|
||||||
date.format("[%H:%M:%S]").to_string().dimmed(),
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
||||||
name.color(COLORS[name_hash]),
|
name.color(COLORS[name_hash]),
|
||||||
|
" |".green(),
|
||||||
msg.yellow().dimmed()
|
msg.yellow().dimmed()
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -179,6 +179,15 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
|
|||||||
if spliced_input.len() == 5 {
|
if spliced_input.len() == 5 {
|
||||||
if spliced_input[2] == "CONNECT" && spliced_input[3] == "USER" {
|
if spliced_input[2] == "CONNECT" && spliced_input[3] == "USER" {
|
||||||
let username = String::from(spliced_input[4]);
|
let username = String::from(spliced_input[4]);
|
||||||
|
let mut ascii_nick = true;
|
||||||
|
for c in username.chars() {
|
||||||
|
if !c.is_ascii() {
|
||||||
|
ascii_nick = false;
|
||||||
|
send!("NAME FAILURE");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ascii_nick {
|
||||||
let mut used = false;
|
let mut used = false;
|
||||||
{
|
{
|
||||||
let lock = clients.lock().unwrap();
|
let lock = clients.lock().unwrap();
|
||||||
@ -195,6 +204,7 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
|
|||||||
} else {
|
} else {
|
||||||
send!("NAME FAILURE");
|
send!("NAME FAILURE");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new(ErrorKind::Other, "BAD REQ"));
|
return Err(Error::new(ErrorKind::Other, "BAD REQ"));
|
||||||
}
|
}
|
||||||
@ -209,6 +219,15 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
|
|||||||
return Err(Error::new(ErrorKind::Other, "BAD REQ"));
|
return Err(Error::new(ErrorKind::Other, "BAD REQ"));
|
||||||
}
|
}
|
||||||
let username = String::from(spliced_input[1]);
|
let username = String::from(spliced_input[1]);
|
||||||
|
let mut ascii_nick = true;
|
||||||
|
for c in username.chars() {
|
||||||
|
if !c.is_ascii() {
|
||||||
|
ascii_nick = false;
|
||||||
|
send!("NAME FAILURE");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ascii_nick {
|
||||||
let mut used = false;
|
let mut used = false;
|
||||||
{
|
{
|
||||||
let lock = clients.lock().unwrap();
|
let lock = clients.lock().unwrap();
|
||||||
@ -228,7 +247,7 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// return Ok(String::new());
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
Loading…
Reference in New Issue
Block a user