From 8ab294277b5b1ad61cae7025b5adfac9e0b92125 Mon Sep 17 00:00:00 2001 From: Thelie Date: Fri, 14 May 2021 21:16:20 +0200 Subject: [PATCH] Added the beginning of the threaded code. --- src/main.rs | 85 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index d1c86b8..57e7e52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,11 @@ use std::{ env::args, path::PathBuf, collections::VecDeque, + sync::mpsc::{ + channel, + Sender, + }, + thread, }; use irc_proto::command::{ CapSubCommand, @@ -134,41 +139,7 @@ fn handle_message(message: Message) -> Option { return None } -fn main() { - let mut server_conf_flag = false; - let mut config_path = None; - let mut server_conf_path = None; - for arg in args().skip(1) { - match arg.as_str() { - "-s" => server_conf_flag = true, - "-h" => { - println!("Usage: mention2mail [config file] [-s server config directory]"); - }, - _ => if server_conf_flag { - server_conf_path = Some(arg); - } else { - config_path = Some(arg); - } - } - } - - let config; - - match config_path { - Some(p) => config = config::get_main_config(PathBuf::from(p)) - .expect("Could not get config"), - None => config = config::get_main_config(PathBuf::from("/etc/Mention2Mail/config.toml")) - .expect("Could not get default config in /etc/Mention2Mail/default.toml"), - } - - let mut 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 => (), - } - +fn handle_server(config: Value, tx: Sender<[String; 4]>) { let mut stream = connect::connect_irc(&config).unwrap(); let mut message_queue = get_irc_identify_messages(&config).unwrap(); @@ -225,3 +196,47 @@ fn main() { } } +fn main() { + let mut server_conf_flag = false; + let mut config_path = None; + let mut server_conf_path = None; + for arg in args().skip(1) { + match arg.as_str() { + "-s" => server_conf_flag = true, + "-h" => { + println!("Usage: mention2mail [config file] [-s server config directory]"); + }, + _ => if server_conf_flag { + server_conf_path = Some(arg); + } else { + config_path = Some(arg); + } + } + } + + let config; + + match config_path { + Some(p) => config = config::get_main_config(PathBuf::from(p)) + .expect("Could not get config"), + None => config = config::get_main_config(PathBuf::from("/etc/Mention2Mail/config.toml")) + .expect("Could not get default config in /etc/Mention2Mail/default.toml"), + } + + let mut 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 => (), + } + + let (tx,rx) = channel(); + + for s_conf in server_configs { + // TODO: create channel and spawn server threads + handle_server(s_conf, tx.clone()); + } + +} +