diff --git a/irc-stream b/irc-stream index 455c49d..ef1e849 160000 --- a/irc-stream +++ b/irc-stream @@ -1 +1 @@ -Subproject commit 455c49dbb237b98c89692b46869ecb22cca8d0f5 +Subproject commit ef1e849d0ea7e6472a3fad86ea859a00597d27ea diff --git a/src/connect.rs b/src/connect.rs new file mode 100644 index 0000000..dc3ee6b --- /dev/null +++ b/src/connect.rs @@ -0,0 +1,64 @@ +/* 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::{ + format, + net::TcpStream, +}; +use irc_stream::{IrcStream, IrcRead, IrcWrite}; +use toml::value::Value; +use native_tls::{TlsConnector,TlsStream}; + +pub trait IrcReadWrite: IrcRead + IrcWrite {} +impl IrcReadWrite for T {} + +fn get_tcp_stream (config: &Value) + -> Result> { + let address = format!("{}:{}", + config.get("server").unwrap().as_str() + .ok_or("Could not get server adress from config")?, + config.get("port").unwrap().as_str() + .ok_or("Could not get port from config")? + ); + println!("Connectiing to: {}", address); + Ok(TcpStream::connect(address)?) +} + +fn get_tls_stream (config: &Value) + -> Result, Box> { + let connector = TlsConnector::new().unwrap(); + let stream = get_tcp_stream(config)?; + Ok(connector.connect(config["server"] + .as_str() + .ok_or("Could not get server adress from config")?, + stream)? + ) +} + +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 { + return Ok(Box::new(IrcStream::new(get_tcp_stream(&config)?))) + } +} + diff --git a/src/main.rs b/src/main.rs index 0d2c84d..9fc6218 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,8 @@ * along with Mention2Mail. If not, see . */ +pub mod connect; + use std::{ format, env::args, @@ -68,29 +70,6 @@ fn get_config>(config_path: P) Ok(Value::from(config)) } -fn get_tcp_stream (config: &Value) - -> Result> { - let address = format!("{}:{}", - config.get("server").unwrap().as_str() - .ok_or("Could not get server adress from config")?, - config.get("port").unwrap().as_str() - .ok_or("Could not get port from config")? - ); - println!("Connectiing to: {}", address); - Ok(TcpStream::connect(address)?) -} - -fn get_tls_stream (config: &Value) - -> Result, Box> { - let connector = TlsConnector::new().unwrap(); - let stream = get_tcp_stream(config)?; - Ok(connector.connect(config["server"] - .as_str() - .ok_or("Could not get server adress from config")?, - stream)? - ) -} - fn get_irc_identify_messages (config: &Value) -> Result, Box> { let mut queue = VecDeque::new(); @@ -165,17 +144,7 @@ fn main() { .expect("no config given"); let config = get_config(config_path).expect("Could not get config"); - // Weird workaround for multitrait dyn pointer - // https://github.com/rust-lang/rfcs/issues/2035 - // would be a charm... - - let mut stream: Box; - - if config["tls"].as_bool().unwrap() == true { - stream = Box::new(IrcStream::new(get_tls_stream(&config).unwrap())); - } else { - stream = Box::new(IrcStream::new(get_tcp_stream(&config).unwrap())); - } + let stream = connect::connect_irc(&config).unwrap(); let mut message_queue = get_irc_identify_messages(&config).unwrap();