2018-03-21 19:38:20 +00:00
|
|
|
extern crate chrono;
|
|
|
|
extern crate colored;
|
2018-03-22 10:36:06 +00:00
|
|
|
extern crate term_size;
|
2018-03-18 21:03:50 +00:00
|
|
|
use std;
|
2018-03-18 20:07:20 +00:00
|
|
|
use std::io::*;
|
2018-02-26 10:52:41 +00:00
|
|
|
use std::net::TcpStream;
|
2018-03-18 20:07:20 +00:00
|
|
|
use std::thread;
|
2018-03-21 22:23:54 +00:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
|
|
|
use std::hash::{Hash, Hasher};
|
2018-03-21 23:06:15 +00:00
|
|
|
use self::colored::*;
|
|
|
|
use self::chrono::Local;
|
|
|
|
|
2018-03-22 23:30:55 +00:00
|
|
|
/*
|
|
|
|
|
2018-03-25 14:31:53 +00:00
|
|
|
0.1 [X]
|
2018-03-23 01:33:37 +00:00
|
|
|
1.1 [X]
|
2018-03-22 23:30:55 +00:00
|
|
|
1.2 [ ]
|
2018-03-23 01:33:37 +00:00
|
|
|
1.3 [X]
|
|
|
|
1.4 [X]
|
|
|
|
1.5 [X]
|
|
|
|
1.6 [X]
|
2018-03-22 23:30:55 +00:00
|
|
|
1.7 [X]
|
|
|
|
1.8 [X]
|
2018-03-23 01:33:37 +00:00
|
|
|
1.9 [X]
|
2018-03-22 23:30:55 +00:00
|
|
|
2.1 [X]
|
|
|
|
2.2 [X]
|
|
|
|
3.1 [ ] // pas utile avec Rust
|
|
|
|
3.2 [X]
|
|
|
|
4.1.1 [X]
|
|
|
|
4.1.2 [X]
|
|
|
|
4.2.1 [X]
|
2018-03-25 14:31:53 +00:00
|
|
|
4.2.2 [X]
|
2018-03-22 23:30:55 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-03-25 14:37:00 +00:00
|
|
|
// TODO: Implement requests 1.2
|
2018-03-18 20:07:20 +00:00
|
|
|
|
2018-03-21 22:23:54 +00:00
|
|
|
fn hash_name(name: &str) -> usize {
|
|
|
|
let mut s = DefaultHasher::new();
|
|
|
|
let name = String::from(name);
|
|
|
|
name.hash(&mut s);
|
|
|
|
s.finish() as usize
|
|
|
|
}
|
2018-03-18 20:07:20 +00:00
|
|
|
|
2018-02-26 10:52:41 +00:00
|
|
|
fn get_entry() -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
stdin().read_line(&mut buf).unwrap();
|
|
|
|
buf.replace("\n", "").replace("\r", "")
|
|
|
|
}
|
|
|
|
|
2018-03-23 01:19:27 +00:00
|
|
|
fn get_name() -> String {
|
2018-03-23 16:25:13 +00:00
|
|
|
'mainloop: loop {
|
2018-03-23 01:19:27 +00:00
|
|
|
println!("{}", "Please enter your name:".yellow().dimmed());
|
|
|
|
let mut name = &*get_entry();
|
|
|
|
name = name.trim();
|
|
|
|
if name.len() > 20 {
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
"Nickname too long, it must be at most 20 characters long".red()
|
|
|
|
);
|
2018-03-21 23:06:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-23 16:25:13 +00:00
|
|
|
for c in name.chars() {
|
|
|
|
if !c.is_ascii() {
|
|
|
|
println!(
|
|
|
|
"{}{}{}",
|
|
|
|
"Character ".red(),
|
|
|
|
&format!("{}", c).green(),
|
|
|
|
" is not an ASCII character.".red()
|
|
|
|
);
|
|
|
|
continue 'mainloop;
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 01:19:27 +00:00
|
|
|
match name {
|
2018-03-21 14:51:34 +00:00
|
|
|
"" => {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-23 01:19:27 +00:00
|
|
|
_ => {
|
|
|
|
let spliced_name: Vec<&str> = name.split_whitespace().collect();
|
|
|
|
if spliced_name.len() != 1 {
|
|
|
|
println!("{}", "Cannot use whitespace in name".red());
|
2018-03-21 14:51:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-23 01:19:27 +00:00
|
|
|
return String::from(name);
|
2018-03-05 16:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 14:51:34 +00:00
|
|
|
}
|
2018-03-21 19:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_to_server(stream: TcpStream) {
|
|
|
|
let mut writer = BufWriter::new(&stream);
|
|
|
|
|
2018-03-21 14:51:34 +00:00
|
|
|
loop {
|
2018-03-21 19:38:20 +00:00
|
|
|
let line = &*get_entry();
|
|
|
|
line.trim();
|
|
|
|
match line {
|
|
|
|
"" => {}
|
2018-03-21 14:51:34 +00:00
|
|
|
"/quit" => {
|
|
|
|
writeln!(writer, "BYE").unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
"/clients" => {
|
|
|
|
writeln!(writer, "REQ CLIENTS").unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
}
|
|
|
|
line => {
|
2018-03-21 23:10:32 +00:00
|
|
|
if line.len() > 2000 {
|
|
|
|
println!(
|
|
|
|
"{}",
|
2018-03-22 10:36:06 +00:00
|
|
|
"Cannot send a message longer than 2000 characters".bright_red()
|
2018-03-21 23:10:32 +00:00
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-21 14:51:34 +00:00
|
|
|
writeln!(writer, "MSG {}", line).unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 16:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
fn exchange_with_server(stream: TcpStream) {
|
|
|
|
let server = stream.peer_addr().unwrap();
|
|
|
|
println!("Connected to {}", server);
|
2018-03-21 14:51:34 +00:00
|
|
|
|
2018-03-21 22:23:54 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
let COLORS: Vec<&str> = vec![
|
|
|
|
// "black",
|
|
|
|
"red",
|
|
|
|
"green",
|
|
|
|
"yellow",
|
|
|
|
"blue",
|
|
|
|
"magenta",
|
|
|
|
"cyan",
|
|
|
|
// "white",
|
|
|
|
];
|
|
|
|
|
2018-03-21 11:12:35 +00:00
|
|
|
let stream_cpy = stream.try_clone().unwrap();
|
|
|
|
let mut reader = BufReader::new(&stream_cpy);
|
2018-03-23 01:19:27 +00:00
|
|
|
let mut writer = BufWriter::new(&stream_cpy);
|
2018-03-18 20:07:20 +00:00
|
|
|
|
|
|
|
macro_rules! receive {
|
|
|
|
() => ({
|
|
|
|
let mut line = String::new();
|
|
|
|
match reader.read_line(&mut line) {
|
|
|
|
Ok(len) => {
|
|
|
|
if len == 0 {
|
2018-03-21 19:38:20 +00:00
|
|
|
// Reader is at EOF. Could use ErrorKind::UnexpectedEOF,
|
|
|
|
// but still unstable.
|
|
|
|
let ret = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other, "test");
|
|
|
|
return
|
|
|
|
Err(ret): std::result::Result<&str,std::io::Error>;
|
2018-03-18 20:07:20 +00:00
|
|
|
}
|
|
|
|
line.pop();
|
|
|
|
}
|
2018-03-18 21:03:50 +00:00
|
|
|
Err(e) => { return Err(e); }
|
2018-03-18 20:07:20 +00:00
|
|
|
};
|
|
|
|
line
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-23 01:19:27 +00:00
|
|
|
// entrée du nom d'utilisateur
|
|
|
|
writeln!(writer, "PROT {} CONNECT NEW", ::PROTOCOL).unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
let _name: String = match (|| loop {
|
|
|
|
let _answer = receive!();
|
|
|
|
if _answer != "NAME REQ" {
|
|
|
|
return Err(Error::new(ErrorKind::Other, _answer));
|
|
|
|
}
|
|
|
|
let nick = get_name();
|
|
|
|
writeln!(writer, "NAME {}", nick).unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
match receive!().as_str() {
|
|
|
|
"NAME OK" => {
|
|
|
|
println!("NAME OK");
|
|
|
|
nick
|
|
|
|
}
|
|
|
|
|
|
|
|
"NAME FAILURE" => {
|
|
|
|
println!("{}", "Username refused by server.".red());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
answer => {
|
|
|
|
println!("{}{}", "Server answered: ".yellow().dimmed(), answer);
|
|
|
|
return Err(Error::new(ErrorKind::Other, answer));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})()
|
|
|
|
{
|
|
|
|
Ok(name) => String::from(name),
|
2018-03-23 01:23:57 +00:00
|
|
|
Err(_) => {
|
2018-03-23 01:19:27 +00:00
|
|
|
println!("{}", ">>> Login successful".green());
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
write_to_server(stream.try_clone().unwrap());
|
2018-03-05 16:39:11 +00:00
|
|
|
});
|
|
|
|
|
2018-03-18 21:03:50 +00:00
|
|
|
match (|| loop {
|
2018-03-21 14:51:34 +00:00
|
|
|
let input: String = String::from(receive!());
|
|
|
|
let spliced_input: Vec<&str> = input.split(" ").collect();
|
|
|
|
match spliced_input[0] {
|
2018-03-23 16:25:13 +00:00
|
|
|
"BAD" => {
|
|
|
|
println!("{}", "Bad request from client".red());
|
|
|
|
}
|
2018-03-21 19:38:20 +00:00
|
|
|
"WELCOME" => {
|
2018-03-21 22:23:54 +00:00
|
|
|
println!("{}", ">>> Login Successful <<<".green());
|
2018-03-21 19:38:20 +00:00
|
|
|
println!("Type /clients to get the list of users connected");
|
|
|
|
println!("Type /quit to disconnect and quit");
|
|
|
|
}
|
2018-03-21 14:51:34 +00:00
|
|
|
"FROM" => {
|
2018-03-21 19:38:20 +00:00
|
|
|
let date = Local::now();
|
2018-03-21 22:23:54 +00:00
|
|
|
let name = String::from(spliced_input[1]);
|
2018-03-22 10:36:06 +00:00
|
|
|
let mut first_line = true;
|
2018-03-21 22:23:54 +00:00
|
|
|
|
|
|
|
// Hashing name for color
|
|
|
|
let mut s = DefaultHasher::new();
|
|
|
|
name.hash(&mut s);
|
|
|
|
let name_hash: usize = (s.finish() as usize) % COLORS.len();
|
|
|
|
|
|
|
|
// Formatting name
|
|
|
|
let mut name = String::new();
|
|
|
|
for _i in 0..(20 - spliced_input[1].to_string().len()) {
|
|
|
|
name.push(' ');
|
|
|
|
}
|
|
|
|
name.push('<');
|
|
|
|
name.push_str(spliced_input[1]);
|
|
|
|
name.push('>');
|
|
|
|
|
2018-03-23 16:25:13 +00:00
|
|
|
// Display message with soft-wrap
|
2018-03-22 10:36:06 +00:00
|
|
|
if let Some((w, _)) = term_size::dimensions() {
|
|
|
|
let mut msg = String::new();
|
|
|
|
let w = w - 34;
|
|
|
|
|
2018-03-23 16:25:13 +00:00
|
|
|
// format message
|
2018-03-22 10:36:06 +00:00
|
|
|
for mut i in 3..spliced_input.len() {
|
|
|
|
if w > msg.len() + spliced_input[i].len() + 1 {
|
|
|
|
msg.push(' ');
|
|
|
|
msg.push_str(spliced_input[i]);
|
|
|
|
} else {
|
|
|
|
if first_line == true {
|
|
|
|
println!(
|
2018-03-23 16:25:13 +00:00
|
|
|
"{}{}{}{}",
|
2018-03-22 10:36:06 +00:00
|
|
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
|
|
|
name.color(COLORS[name_hash]),
|
2018-03-23 16:25:13 +00:00
|
|
|
" |".green(),
|
2018-03-22 10:36:06 +00:00
|
|
|
msg.yellow().dimmed()
|
|
|
|
);
|
|
|
|
first_line = false;
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"{}{}",
|
|
|
|
" |".green(),
|
|
|
|
msg.yellow().dimmed()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
msg = String::new();
|
|
|
|
#[allow(unused_assignments)]
|
2018-03-25 14:31:53 +00:00
|
|
|
#[allow(unused_assignments)]
|
2018-03-22 10:36:06 +00:00
|
|
|
i = i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if first_line == true {
|
|
|
|
println!(
|
2018-03-23 16:25:13 +00:00
|
|
|
"{}{}{}{}",
|
2018-03-22 10:36:06 +00:00
|
|
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
|
|
|
name.color(COLORS[name_hash]),
|
2018-03-23 16:25:13 +00:00
|
|
|
" |".green(),
|
2018-03-22 10:36:06 +00:00
|
|
|
msg.yellow().dimmed()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"{}{}",
|
|
|
|
" |".green(),
|
|
|
|
msg.yellow().dimmed()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut msg = String::new();
|
|
|
|
for i in 3..spliced_input.len() {
|
|
|
|
msg.push_str(" ");
|
|
|
|
msg.push_str(spliced_input[i]);
|
|
|
|
}
|
|
|
|
println!(
|
|
|
|
"{} {}{}",
|
|
|
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
|
|
|
name.color(COLORS[name_hash]),
|
|
|
|
msg.yellow().dimmed()
|
|
|
|
);
|
|
|
|
}
|
2018-03-21 19:38:20 +00:00
|
|
|
}
|
|
|
|
"BYE" => {
|
|
|
|
return Ok("Ok");
|
|
|
|
}
|
|
|
|
"LIST" => {
|
2018-03-25 14:31:53 +00:00
|
|
|
println!(
|
|
|
|
"{}{}{}",
|
|
|
|
">>>> LIST OF CLIENTS CONNECTED (".bold().yellow(),
|
|
|
|
spliced_input[2],
|
|
|
|
") <<<<".bold().yellow()
|
|
|
|
);
|
|
|
|
for i in 3..spliced_input.len() {
|
2018-03-21 19:38:20 +00:00
|
|
|
println!("\t\t{}", spliced_input[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"JOIN" => {
|
2018-03-21 22:23:54 +00:00
|
|
|
let date = Local::now();
|
2018-03-21 23:06:15 +00:00
|
|
|
let name_hash: usize = hash_name(spliced_input[1].clone()) % COLORS.len();
|
2018-03-21 22:23:54 +00:00
|
|
|
|
2018-03-21 19:38:20 +00:00
|
|
|
println!(
|
2018-03-21 22:23:54 +00:00
|
|
|
"{}{}{}{}",
|
|
|
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
2018-03-23 16:25:13 +00:00
|
|
|
" ------> ".green(),
|
2018-03-21 22:23:54 +00:00
|
|
|
spliced_input[1].color(COLORS[name_hash]),
|
2018-03-21 19:38:20 +00:00
|
|
|
" has joined".green()
|
2018-03-21 22:23:54 +00:00
|
|
|
)
|
2018-03-21 19:38:20 +00:00
|
|
|
}
|
|
|
|
"LOGOUT" => {
|
2018-03-21 22:23:54 +00:00
|
|
|
let date = Local::now();
|
2018-03-21 23:06:15 +00:00
|
|
|
let name_hash: usize = hash_name(spliced_input[1].clone()) % COLORS.len();
|
2018-03-21 22:23:54 +00:00
|
|
|
|
2018-03-21 19:38:20 +00:00
|
|
|
println!(
|
2018-03-21 22:23:54 +00:00
|
|
|
"{}{}{}{}",
|
|
|
|
date.format("[%H:%M:%S]").to_string().dimmed(),
|
2018-03-23 16:25:13 +00:00
|
|
|
" <------ ".red(),
|
2018-03-21 22:23:54 +00:00
|
|
|
spliced_input[1].color(COLORS[name_hash]),
|
2018-03-21 19:38:20 +00:00
|
|
|
" has left".red()
|
2018-03-21 22:23:54 +00:00
|
|
|
)
|
2018-03-21 14:51:34 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
println!("{}", input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// println!("{}", input);
|
2018-03-18 21:03:50 +00:00
|
|
|
})()
|
|
|
|
{
|
2018-03-18 20:07:20 +00:00
|
|
|
Ok(_) => {
|
2018-03-21 20:48:40 +00:00
|
|
|
println!("{}", ">>> Logout successful <<<".green());
|
2018-03-18 20:07:20 +00:00
|
|
|
}
|
2018-03-21 22:23:54 +00:00
|
|
|
Err(_) => {
|
|
|
|
println!("{}", "Error: Connection with server lost".red());
|
2018-02-26 10:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn client(server_address: String) {
|
2018-03-21 19:38:20 +00:00
|
|
|
println!("Trying to connect to the server...");
|
2018-02-26 10:52:41 +00:00
|
|
|
match TcpStream::connect(server_address) {
|
|
|
|
Ok(stream) => {
|
|
|
|
exchange_with_server(stream);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2018-03-21 19:38:20 +00:00
|
|
|
println!("{} {}", "Connection to server failed:".red(), e);
|
2018-03-05 16:39:11 +00:00
|
|
|
return;
|
2018-02-26 10:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|