Implemented basic IRC message handling.

Fun fact: The functionality is now similar to 6f3f8614d7.
This commit is contained in:
Thelie 2021-05-03 01:48:31 +02:00
parent d8ee914654
commit 66308f678f
2 changed files with 60 additions and 46 deletions

@ -1 +1 @@
Subproject commit 5d134f716dfe205c6ebef627f08d8330073cbf1d
Subproject commit bf53e62f1713dfe83198f75992cc0e889de398ff

View file

@ -65,10 +65,13 @@ fn get_config<P: AsRef<Path>>(config_path: P)
Ok(Value::from(config))
}
fn get_tcp_stream (config: &Value) -> Result<TcpStream, Box<dyn std::error::Error>> {
fn get_tcp_stream (config: &Value)
-> Result<TcpStream, Box<dyn std::error::Error>> {
let address = format!("{}:{}",
config["server"].as_str().ok_or("Could not get server adress from config")?,
config["port"].as_str().ok_or("Could not get port from config")?
config.get("server").unwrap().as_str()
.ok_or("Could not get server adress from config")?,
config.get("port").unwrap().as_str()
.ok_or("Could not get port from config")?
);
println!("Connectiing to: {}", address);
Ok(TcpStream::connect(address)?)
@ -85,14 +88,10 @@ fn get_tls_stream (config: &Value)
)
}
fn get_irc_identify_messages (
queue: VecDeque<Message>,
pass: Option<String>,
nick: String,
user: String,
real_name: String
) -> VecDeque<Message>{
let mut queue = queue;
fn get_irc_identify_messages (config: &Value)
-> Result<VecDeque<Message>, Box<dyn std::error::Error>> {
let mut queue = VecDeque::new();
queue.push_back(Message::from(
Command::CAP(
None,
@ -100,22 +99,40 @@ fn get_irc_identify_messages (
None,
None
)));
match pass {
match config.get("password") {
Some(p) => queue.push_back(Message::from(
Command::PASS(p)
Command::PASS(
String::from(p.as_str().ok_or("Could not parse password.")?)
)
)),
None => ()
}
queue.push_back(Message::from(
Command::NICK(nick)
));
queue.push_back(Message::from(
let nick: String;
match config.get("nickname") {
Some(n) => {
nick = String::from(n.as_str().ok_or("Could not parse nickname.")?);
queue.push_back(Message::from(Command::NICK(nick.clone())));
},
None => return Err("No nickname supplied!".into()),
}
match config.get("username") {
Some(u) => queue.push_back(Message::from(
Command::USER(
user,
String::from(u.as_str().ok_or("Could not parse username.")?),
"0".to_owned(),
real_name)
));
queue
String::from(u.as_str().ok_or("Could not parse username.")?)
)
)),
None => queue.push_back(Message::from(
// nick.clone() is only used once because the value
// can be moved the second time
Command::USER(nick.clone(), "0".to_owned(), nick)
))
}
Ok(queue)
}
fn main() {
@ -135,20 +152,14 @@ fn main() {
stream = Box::new(IrcStream::new(get_tcp_stream(&config).unwrap()));
}
let mut message_queue: VecDeque<Message> = VecDeque::new();
message_queue = get_irc_identify_messages(
message_queue,
None,
config["nickname"].to_string(),
config["nickname"].to_string(),
config["nickname"].to_string()
);
let mut message_queue = get_irc_identify_messages(&config).unwrap();
while let Some(message) = message_queue.pop_front() {
stream.write(message).unwrap();
}
stream.flush().unwrap();
loop {
let message = match stream.read() {
Ok(m) => m,
@ -159,20 +170,23 @@ fn main() {
)
)
};
// let sender = match message.source_nickname() {
// Some(s) => s.clone(),
// None => "anonymous"
// };
match message.command {
Command::PING(_s1, _s2) => stream.write(Message::from(
Command::PONG(String::from("PONG"), None)
)).unwrap(),
Command::PRIVMSG(rec, msg) => println!(
"someone -> {}: {}",
&rec,
&msg
let sender = match message.clone().source_nickname() {
Some(s) => String::from(s),
None => String::from("anonymous")
};
match message.clone().command {
Command::PING(ref data, _) => {
stream.write(Message::from(
Command::PONG(data.to_owned(), None)
)).unwrap();
},
Command::PRIVMSG(ref rec, ref msg) => println!(
"{} -> {}: {}",
rec,
msg,
sender
),
_ => ()
_ => println!("Other message: {}", message.clone().to_string())
}
}
}