Limited username length to 20 characters

This commit is contained in:
Phuntsok Drak-pa 2018-03-22 00:06:15 +01:00
parent 92be982856
commit cea07a8e19
2 changed files with 13 additions and 7 deletions

View File

@ -4,10 +4,13 @@ use std;
use std::io::*; use std::io::*;
use std::net::TcpStream; use std::net::TcpStream;
use std::thread; use std::thread;
use self::colored::*;
use self::chrono::Local;
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use self::colored::*;
use self::chrono::Local;
// TODO: Limit usernames to ascii
// TODO: implement requests 1.x from protocol
fn hash_name(name: &str) -> usize { fn hash_name(name: &str) -> usize {
let mut s = DefaultHasher::new(); let mut s = DefaultHasher::new();
@ -26,6 +29,10 @@ fn get_name(writer: &mut BufWriter<&TcpStream>) {
loop { loop {
let mut line = &*get_entry(); let mut line = &*get_entry();
line = line.trim(); line = line.trim();
if line.len() > 20 {
println!("Nickname too long, it must be at most 20 characters long");
continue;
}
match line { match line {
"" => { "" => {
continue; continue;
@ -38,7 +45,6 @@ fn get_name(writer: &mut BufWriter<&TcpStream>) {
} }
line => { line => {
let line_str: String = String::from(line); let line_str: String = String::from(line);
// let spliced: Vec<&str> = line_str.split(" ").collect();
let spliced: Vec<&str> = line_str.split_whitespace().collect(); let spliced: Vec<&str> = line_str.split_whitespace().collect();
if spliced.len() > 1 { if spliced.len() > 1 {
println!("Cannot use whitespace in username."); println!("Cannot use whitespace in username.");
@ -175,8 +181,7 @@ fn exchange_with_server(stream: TcpStream) {
} }
"JOIN" => { "JOIN" => {
let date = Local::now(); let date = Local::now();
let name_hash: usize = let name_hash: usize = hash_name(spliced_input[1].clone()) % COLORS.len();
hash_name(spliced_input[1].clone()) % COLORS.len();
println!( println!(
"{}{}{}{}", "{}{}{}{}",
@ -188,8 +193,7 @@ fn exchange_with_server(stream: TcpStream) {
} }
"LOGOUT" => { "LOGOUT" => {
let date = Local::now(); let date = Local::now();
let name_hash: usize = let name_hash: usize = hash_name(spliced_input[1].clone()) % COLORS.len();
hash_name(spliced_input[1].clone()) % COLORS.len();
println!( println!(
"{}{}{}{}", "{}{}{}{}",

View File

@ -4,6 +4,8 @@ use std::thread;
use std::sync::{Arc, Mutex, MutexGuard}; use std::sync::{Arc, Mutex, MutexGuard};
use std::collections::HashMap; use std::collections::HashMap;
// TODO: implement requests 1.x from protocol
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Evolution implementation protocole // // Evolution implementation protocole //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////