Compare commits
2 commits
f1d089829f
...
e5c8b122b3
Author | SHA1 | Date | |
---|---|---|---|
Yash Karandikar | e5c8b122b3 | ||
Yash Karandikar | 422f640c61 |
77
src/main.rs
77
src/main.rs
|
@ -10,9 +10,6 @@ use tokio::{
|
|||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
// TODO:
|
||||
// * track nicknames
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum BroadcastMessage {
|
||||
MSG(String, String, String),
|
||||
|
@ -95,6 +92,8 @@ async fn connection_handler(
|
|||
(prefix, parts)
|
||||
};
|
||||
|
||||
tracing::info!("sender: {}, prefix: {}, arguments: {:#?}", nick, prefix, arguments.join(" "));
|
||||
|
||||
match prefix.as_str() {
|
||||
"MSG" => {
|
||||
if arguments.len() < 1 {
|
||||
|
@ -104,47 +103,40 @@ async fn connection_handler(
|
|||
}
|
||||
},
|
||||
"NICK" => {
|
||||
let old_nick = nick.clone();
|
||||
if arguments.len() < 1 {
|
||||
cm_sender.send(ClientMessage::ERR(String::from("NICK requires at least 1 argument")))?;
|
||||
if let Some(new_nick) = arguments.iter().next() {
|
||||
let old_nick = nick.clone();
|
||||
|
||||
let mut should_change = true;
|
||||
for (_, nick) in users.lock().await.iter() {
|
||||
if nick == new_nick {
|
||||
should_change = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if should_change {
|
||||
nick = new_nick.to_owned();
|
||||
users.lock().await.insert(id.clone(), nick.clone());
|
||||
br_sender.send(BroadcastMessage::NICK(old_nick, nick.clone(), id.clone()))?;
|
||||
} else {
|
||||
cm_sender.send(ClientMessage::ERR(format!("Nickname {} is already taken!", new_nick)))?;
|
||||
}
|
||||
} else {
|
||||
let new_nick = arguments.join("-");
|
||||
cm_sender.send(ClientMessage::ERR(String::from("NICK requires at least 1 argument")))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"LIST" => {
|
||||
let mut list = Vec::new();
|
||||
let current_nick = &nick;
|
||||
|
||||
if buf.starts_with("NICK") {
|
||||
let old_nick = nick.clone();
|
||||
let new_nick = buf[5..].trim().to_string();
|
||||
|
||||
let mut should_change = true;
|
||||
for (_, nick) in users.lock().await.iter() {
|
||||
if nick == &new_nick {
|
||||
should_change = false;
|
||||
break;
|
||||
for (_, nick) in users.lock().await.iter() {
|
||||
if nick != current_nick {
|
||||
list.push(nick.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
if should_change {
|
||||
nick = new_nick;
|
||||
users.lock().await.insert(id.clone(), nick.clone()); // successive calls to insert will overwrite the old value
|
||||
br_sender.send(BroadcastMessage::NICK(old_nick, nick.clone(), id.clone()))?;
|
||||
} else {
|
||||
cm_sender.send(ClientMessage::ERR(format!("Nickname {} is already taken!", new_nick)))?;
|
||||
}
|
||||
} else if buf.starts_with("LIST") {
|
||||
let mut list = Vec::new();
|
||||
let current_nick = &nick;
|
||||
|
||||
for (_, nick) in users.lock().await.iter() {
|
||||
if nick != current_nick {
|
||||
list.push(nick.clone());
|
||||
}
|
||||
}
|
||||
cm_sender.send(ClientMessage::LIST(list))?;
|
||||
} else {
|
||||
if let Some(prefix) = buf.split_whitespace().collect::<Vec<&str>>().get(0) {
|
||||
|
||||
cm_sender.send(ClientMessage::ERR(format!("Unknown command: {}", *prefix)))?;
|
||||
cm_sender.send(ClientMessage::LIST(list))?;
|
||||
},
|
||||
_ => {
|
||||
cm_sender.send(ClientMessage::ERR(format!("Unknown command: {}", prefix)))?;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -153,13 +145,11 @@ async fn connection_handler(
|
|||
match msg {
|
||||
BroadcastMessage::MSG(nick, msg, sender) => {
|
||||
if sender != id {
|
||||
tracing::info!("{}: {:?}", nick, msg);
|
||||
tx.write_all(format!("{} MSG {}\n", nick, msg).as_bytes()).await?;
|
||||
}
|
||||
},
|
||||
BroadcastMessage::NICK(old_nick, new_nick, sender) => {
|
||||
if sender != id {
|
||||
tracing::info!("{} changed to {}", old_nick, new_nick);
|
||||
tx.write_all(format!("{} NICK {}\n", old_nick, new_nick).as_bytes()).await?;
|
||||
}
|
||||
},
|
||||
|
@ -179,13 +169,12 @@ async fn connection_handler(
|
|||
let msg = o?;
|
||||
match msg {
|
||||
ClientMessage::ERR(msg) => {
|
||||
tracing::error!("ERR: {}", msg);
|
||||
tracing::error!("ERR triggered by {}: {}", nick, msg);
|
||||
tx.write_all(format!("ERR {}\n", msg).as_bytes()).await?;
|
||||
},
|
||||
ClientMessage::LIST(list) => {
|
||||
let names = list.join(" ");
|
||||
|
||||
tracing::info!("LIST: {}", names);
|
||||
tx.write_all(format!("LIST {}\n", names).as_bytes()).await?;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue