From 500d87d65b4d724c3ed1eb66222c72b2f2f06d01 Mon Sep 17 00:00:00 2001 From: Thelie Date: Mon, 17 May 2021 22:50:21 +0200 Subject: [PATCH] Added threading and beautified the code a bit. --- src/main.rs | 77 +++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 57e7e52..e4a1347 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,16 +117,19 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque) Ok(queue) } -fn handle_message(message: Message) -> Option { +fn handle_message(message: Message) -> (Option, Option<[String; 4]>) { 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) - )); + return ( + Some(Message::from( + Command::PONG(data.to_owned(), None) + )), + None + ); }, Command::PRIVMSG(ref rec, ref msg) => println!( "{} -> {}: {}", @@ -136,10 +139,11 @@ fn handle_message(message: Message) -> Option { ), _ => println!("{}", message.clone().to_string()) } - return None + return (None, None) } -fn handle_server(config: Value, tx: Sender<[String; 4]>) { +fn handle_server(config: Value, tx: Sender<[String; 4]>) + -> Result<(), Box> { let mut stream = connect::connect_irc(&config).unwrap(); let mut message_queue = get_irc_identify_messages(&config).unwrap(); @@ -151,17 +155,8 @@ fn handle_server(config: Value, tx: Sender<[String; 4]>) { message_queue = get_irc_join_messages(&config, message_queue).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) - ) - ) - }; + let message = stream.read()?; match message.command { Command::PING(ref data, _) => { stream.write(Message::from( @@ -172,27 +167,22 @@ fn handle_server(config: Value, tx: Sender<[String; 4]>) { } break; } - _ => match handle_message(message) { - Some(m) => stream.write(m).unwrap(), - None => () - } + _ => () } } + // Handle all incoming messages after joining the channels. loop { - let message = match stream.read() { - Ok(m) => m, - Err(e) => Message::from( - Command::PRIVMSG( - String::from("Error"), - format!("{}", e) - ) - ) - }; - match handle_message(message) { - Some(m) => stream.write(m).unwrap(), + let message = stream.read()?; + let (answer, data) = handle_message(message); + match answer { + Some(a) => stream.write(a).unwrap(), None => () } + match data { + Some(d) => tx.send(d)?, + None => (), + } } } @@ -223,19 +213,36 @@ fn main() { .expect("Could not get default config in /etc/Mention2Mail/default.toml"), } - let mut server_configs; + let server_configs; match server_conf_path { Some(p) => server_configs = config::get_server_configs(config, PathBuf::from(p)) .expect("Could not get server config."), - None => (), + None => server_configs = vec![config], } let (tx,rx) = channel(); + let mut server_threads = vec![]; for s_conf in server_configs { - // TODO: create channel and spawn server threads - handle_server(s_conf, tx.clone()); + let t = tx.clone(); + server_threads.push(thread::Builder::new() + .name("server name here".to_string()) + .spawn(move || { + handle_server(s_conf, t).unwrap(); + }) + ); + } + + loop { + match rx.recv() { + Ok(data) => println!("{},{},{},{}", + data[0], + data[1], + data[2], + data[3]), + Err(_e) => (), + } } }