diff --git a/src/main.rs b/src/main.rs index b82a74c..93b0939 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use std::{ path::Path, io::Read, net::TcpStream, + collections::VecDeque, }; use irc_proto::command::{ CapSubCommand, @@ -31,6 +32,9 @@ use irc_stream::{IrcStream, IrcRead, IrcWrite}; use toml::Value; use native_tls::{TlsConnector,TlsStream}; +trait IrcReadWrite: IrcRead + IrcWrite {} +impl IrcReadWrite for T {} + fn get_config>(config_path: P) -> Result> { let mut config = String::new(); @@ -72,37 +76,37 @@ fn get_tls_stream (config: &Value) ) } -fn identify_irc ( - stream: &mut IrcStream, +fn get_irc_identify_messages ( + queue: VecDeque, pass: Option, nick: String, user: String, real_name: String -) -> Result<(), irc_stream::IrcStreamError>{ - stream.write(Message::from( +) -> VecDeque{ + let mut queue = queue; + queue.push_back(Message::from( Command::CAP( None, CapSubCommand::END, None, None - ) - ))?; + ))); match pass { - Some(p) => stream.write(Message::from( + Some(p) => queue.push_back(Message::from( Command::PASS(p) - ))?, + )), None => () } - stream.write(Message::from( + queue.push_back(Message::from( Command::NICK(nick) - ))?; - stream.write(Message::from( + )); + queue.push_back(Message::from( Command::USER( user, "0".to_owned(), real_name) - ))?; - Ok(()) + )); + queue } fn main() { @@ -113,23 +117,27 @@ fn main() { // Weird workaround for multitrait dyn pointer // https://github.com/rust-lang/rfcs/issues/2035 // would be a charm... - trait IrcReadWrite: IrcRead + IrcWrite {} - impl IrcReadWrite for T {} - let mut stream: &dyn IrcReadWrite; + + let mut stream: Box; if config["tls"].as_bool().unwrap() == true { - stream = &IrcStream::new(get_tls_stream(&config).unwrap()); + stream = Box::new(IrcStream::new(get_tls_stream(&config).unwrap())); } else { - stream = &IrcStream::new(get_tcp_stream(&config).unwrap()); + stream = Box::new(IrcStream::new(get_tcp_stream(&config).unwrap())); } - identify_irc( - &mut stream, + let mut message_queue: VecDeque = VecDeque::new(); + + message_queue = get_irc_identify_messages( + message_queue, None, config["Nick"].to_string(), config["User"].to_string(), config["Real"].to_string() ); - + + while let Some(message) = message_queue.pop_front() { + stream.write(message).unwrap(); + } }