From d8ee9146548389d3206230f791ca69e8b7c1109e Mon Sep 17 00:00:00 2001 From: Thelie Date: Sat, 1 May 2021 16:09:44 +0200 Subject: [PATCH] Got rid of errors and exceptions, but the connection doesn't work yet. --- irc-stream | 2 +- src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/irc-stream b/irc-stream index cd15222..5d134f7 160000 --- a/irc-stream +++ b/irc-stream @@ -1 +1 @@ -Subproject commit cd15222685e3bbd80acaa279ceabc1514636b08c +Subproject commit 5d134f716dfe205c6ebef627f08d8330073cbf1d diff --git a/src/main.rs b/src/main.rs index 93b0939..7346e8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ use irc_proto::command::{ }; use irc_proto::message::Message; use irc_stream::{IrcStream, IrcRead, IrcWrite}; -use toml::Value; +use toml::value::Value; use native_tls::{TlsConnector,TlsStream}; trait IrcReadWrite: IrcRead + IrcWrite {} @@ -37,32 +37,41 @@ impl IrcReadWrite for T {} fn get_config>(config_path: P) -> Result> { + + // I once read that this multiple let style + // is the "rusty" way to do things. + // Personally, I find this specific instance to be somewhat sketchy. let mut config = String::new(); std::fs::File::open(config_path)? - .read_to_string(&mut config); + .read_to_string(&mut config)?; let mut config = config.parse::()?; + // The important thing here is that config is a mut Table in the end. + let mut config = config.as_table_mut().unwrap().clone(); + if config.get("port") == None { - config["port"] = Value::from("6697"); + config.insert(String::from("port"), Value::from("6697")); } + if config.get("tls") == None { if config["port"].as_integer() == Some(194) || config["port"].as_integer() == Some(6667) { - config["tls"] = Value::from(false); + config.insert(String::from("tls"), Value::from(false)); }else if config["port"].as_integer() == Some(6696) || config["port"].as_integer() == Some(6697) { - config["tls"] = Value::from(true); + config.insert(String::from("tls"), Value::from(true)); } } - Ok(config) + Ok(Value::from(config)) } fn get_tcp_stream (config: &Value) -> Result> { - let adress = format!("{}:{}", + 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")? ); - Ok(TcpStream::connect("127.0.0.1:8080")?) + println!("Connectiing to: {}", address); + Ok(TcpStream::connect(address)?) } fn get_tls_stream (config: &Value) @@ -131,13 +140,40 @@ fn main() { message_queue = get_irc_identify_messages( message_queue, None, - config["Nick"].to_string(), - config["User"].to_string(), - config["Real"].to_string() + config["nickname"].to_string(), + config["nickname"].to_string(), + config["nickname"].to_string() ); while let Some(message) = message_queue.pop_front() { stream.write(message).unwrap(); } + + loop { + let message = match stream.read() { + Ok(m) => m, + Err(e) => Message::from( + Command::PRIVMSG( + String::from("Error"), + format!("{}", e) + ) + ) + }; +// 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 + ), + _ => () + } + } }