Check for name to be ascii characters only

This commit is contained in:
Phuntsok Drak-pa 2018-03-23 17:25:13 +01:00
parent 7dfbf7c081
commit 5a56afb709
2 changed files with 71 additions and 35 deletions

View File

@ -34,7 +34,6 @@ use self::chrono::Local;
*/
// TODO: Implement requests 0.1 and 1.2
// TODO: Limit usernames to ascii
fn hash_name(name: &str) -> usize {
let mut s = DefaultHasher::new();
@ -50,7 +49,7 @@ fn get_entry() -> String {
}
fn get_name() -> String {
loop {
'mainloop: loop {
println!("{}", "Please enter your name:".yellow().dimmed());
let mut name = &*get_entry();
name = name.trim();
@ -61,6 +60,17 @@ fn get_name() -> String {
);
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 {
"" => {
continue;
@ -193,6 +203,9 @@ fn exchange_with_server(stream: TcpStream) {
let input: String = String::from(receive!());
let spliced_input: Vec<&str> = input.split(" ").collect();
match spliced_input[0] {
"BAD" => {
println!("{}", "Bad request from client".red());
}
"WELCOME" => {
println!("{}", ">>> Login Successful <<<".green());
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('>');
// Display message with soft-wrap
if let Some((w, _)) = term_size::dimensions() {
let mut msg = String::new();
let w = w - 34;
// format message
for mut i in 3..spliced_input.len() {
if w > msg.len() + spliced_input[i].len() + 1 {
msg.push(' ');
@ -228,9 +243,10 @@ fn exchange_with_server(stream: TcpStream) {
} else {
if first_line == true {
println!(
"{} {}:{}",
"{}{}{}{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
name.color(COLORS[name_hash]),
" |".green(),
msg.yellow().dimmed()
);
first_line = false;
@ -249,9 +265,10 @@ fn exchange_with_server(stream: TcpStream) {
if first_line == true {
println!(
"{} {}:{}",
"{}{}{}{}",
date.format("[%H:%M:%S]").to_string().dimmed(),
name.color(COLORS[name_hash]),
" |".green(),
msg.yellow().dimmed()
);
} else {

View File

@ -179,6 +179,15 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
if spliced_input.len() == 5 {
if spliced_input[2] == "CONNECT" && spliced_input[3] == "USER" {
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 lock = clients.lock().unwrap();
@ -195,6 +204,7 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
} else {
send!("NAME FAILURE");
}
}
} else {
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"));
}
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 lock = clients.lock().unwrap();
@ -228,7 +247,7 @@ fn handle_client(stream: TcpStream, clients: Arc<Mutex<UserMap>>) {
}
}
}
// return Ok(String::new());
}
}
}
})()