Version 0.2.0 #2
2 changed files with 74 additions and 37 deletions
70
src/config.rs
Normal file
70
src/config.rs
Normal file
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Value, Box<dyn std::error::Error>> {
|
||||
|
||||
// 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::<Value>()?;
|
||||
// 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<Value, Box<dyn std::error::Error>> {
|
||||
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)
|
||||
}
|
41
src/main.rs
41
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<T: IrcRead + IrcWrite> 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<P: AsRef<Path>>(config_path: P)
|
||||
-> Result<Value, Box<dyn std::error::Error>> {
|
||||
|
||||
// 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::<Value>()?;
|
||||
// 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 => (),
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue