From 716584841ec857d6739e4752774d34fde0423a03 Mon Sep 17 00:00:00 2001 From: Thelie Date: Tue, 11 May 2021 00:56:54 +0200 Subject: [PATCH] Split config commands into a module and broke the code a bit. --- src/config.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 41 +++--------------------------- 2 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..5dd2761 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,70 @@ +/* Copyright 2021 Daniel Mowitz + * This file is part of Mention2Mail. + * + * Mention2Mail is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Mention2Mail is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Mention2Mail. If not, see . + */ + +use std::{ + path::Path, + io::Read, + fs::read_dir, +}; +use toml::value::Value; + +/// parses a toml config file and sets some default values +/// if the corresponding fields are not set. +// TODO: Document config file options once they are stable. +pub fn get_main_config(config_path: &Path) + -> 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)?; + 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.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.insert(String::from("tls"), Value::from(false)); + }else if config["port"].as_integer() == Some(6696) || + config["port"].as_integer() == Some(6697) { + config.insert(String::from("tls"), Value::from(true)); + } + } + + Ok(Value::from(config)) +} + +pub fn get_server_config (config: Value, config_path: &Path) + -> Result> { + if config_path.is_dir() { + for entry in read_dir(config_path)? { + let entry = entry?; + let server_path = entry.path(); + if server_path.is_dir(){ + config = server_config_helper(config, server_path)?; + } + } + } + Ok(config) +} diff --git a/src/main.rs b/src/main.rs index 988c2df..a402bc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,12 +16,12 @@ */ pub mod connect; +pub mod config; use std::{ format, env::args, path::Path, - io::Read, collections::VecDeque, }; use irc_proto::command::{ @@ -38,39 +38,6 @@ use toml::value::Value; trait IrcReadWrite: IrcRead + IrcWrite {} impl IrcReadWrite for T {} -/// parses a toml config file and sets some default values -/// if the corresponding fields are not set. -// TODO: Document config file options once they are stable. -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)?; - 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.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.insert(String::from("tls"), Value::from(false)); - }else if config["port"].as_integer() == Some(6696) || - config["port"].as_integer() == Some(6697) { - config.insert(String::from("tls"), Value::from(true)); - } - } - - Ok(Value::from(config)) -} - /// Constructs a VecDeque with the messages /// required to identify with an IRC server. /// Uses the credentials set in -`config.toml`. @@ -188,14 +155,14 @@ fn main() { let mut config; match config_path { - Some(p) => config = get_config(p) + Some(p) => config = config::get_main_config(&Path::from(p)) .expect("Could not get config"), - None => config = get_config("/etc/Mention2Mail/config.toml") + None => config = config::get_main_config(&Path::from("/etc/Mention2Mail/config.toml")) .expect("Could not get default config in /etc/Mention2Mail/default.toml"), } match server_conf_path { - Some(p) => config = get_server_config(config, p) + Some(p) => config = config::get_server_config(config, p) .expect("Could not get server config."), None => (), }