diff --git a/src/connect.rs b/src/connect.rs index d0ff148..9d8f12c 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -23,6 +23,9 @@ use irc_stream::{IrcStream, IrcRead, IrcWrite}; use toml::value::Value; use native_tls::{TlsConnector,TlsStream}; +// Weird workaround for multitrait dyn pointer +// https://github.com/rust-lang/rfcs/issues/2035 +// would be a charm... pub trait IrcReadWrite: IrcRead + IrcWrite {} impl IrcReadWrite for T {} @@ -51,10 +54,7 @@ fn get_tls_stream (config: &Value) pub fn connect_irc (config: &Value) -> Result, Box> { - // Weird workaround for multitrait dyn pointer - // https://github.com/rust-lang/rfcs/issues/2035 - // would be a charm... - + if *config["tls"].as_bool().get_or_insert(true) == true { return Ok(Box::new(IrcStream::new(get_tls_stream(&config)?))) } else { diff --git a/src/main.rs b/src/main.rs index 301fae1..ec283bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,9 +23,6 @@ use std::{ path::Path, io::Read, collections::VecDeque, - thread, - time, - sync::mpsc::channel, }; use irc_proto::command::{ CapSubCommand, @@ -140,6 +137,28 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque) Ok(queue) } +fn handle_message(message: Message) -> Option { + let sender = match message.clone().source_nickname() { + Some(s) => String::from(s), + None => String::from("anonymous") + }; + match message.clone().command { + Command::PING(ref data, _) => { + return Some(Message::from( + Command::PONG(data.to_owned(), None) + )); + }, + Command::PRIVMSG(ref rec, ref msg) => println!( + "{} -> {}: {}", + rec, + msg, + sender + ), + _ => println!("{}", message.clone().to_string()) + } + return None +} + fn main() { let config_path = args().nth(1) .expect("no config given"); @@ -155,15 +174,34 @@ fn main() { message_queue = get_irc_join_messages(&config, message_queue).unwrap(); - //TODO remove this botch - let (tx, rx) = channel(); - thread::spawn(move|| { - thread::sleep(time::Duration::from_secs(10)); - while let Some(message) = message_queue.pop_front() { - println!("Sending: {}", message.clone().to_string()); - tx.send(message).unwrap(); - } - }); + // Wait for first ping and join channels after sending pong. + // TODO this approach is not very DRY! + loop { + let message = match stream.read() { + Ok(m) => m, + Err(e) => Message::from( + Command::PRIVMSG( + String::from("Error"), + format!("{}", e) + ) + ) + }; + match message.command { + Command::PING(ref data, _) => { + stream.write(Message::from( + Command::PONG(data.to_owned(), None) + )).unwrap(); + while let Some(message) = message_queue.pop_front() { + stream.write(message).unwrap(); + } + break; + } + _ => match handle_message(message) { + Some(m) => stream.write(m).unwrap(), + None => () + } + } + } loop { let message = match stream.read() { @@ -175,27 +213,9 @@ fn main() { ) ) }; - 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!("{}", message.clone().to_string()) - } - match rx.try_recv() { - Ok(m) => stream.write(m).unwrap(), - Err(_e) => () + match handle_message(message) { + Some(m) => stream.write(m).unwrap(), + None => () } } }