The fight with generics continues…
This commit is contained in:
parent
2e0c95ab57
commit
ca48225d6b
3 changed files with 68 additions and 35 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit 455c49dbb237b98c89692b46869ecb22cca8d0f5
|
Subproject commit ef1e849d0ea7e6472a3fad86ea859a00597d27ea
|
64
src/connect.rs
Normal file
64
src/connect.rs
Normal file
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<T: IrcRead + IrcWrite> IrcReadWrite for T {}
|
||||||
|
|
||||||
|
fn get_tcp_stream (config: &Value)
|
||||||
|
-> Result<TcpStream, Box<dyn std::error::Error>> {
|
||||||
|
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<TlsStream<TcpStream>, Box<dyn std::error::Error>> {
|
||||||
|
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<S: std::io::Read + std::io::Write> (config: &Value)
|
||||||
|
-> Result<Box<dyn IrcReadWrite>, Box<dyn std::error::Error>> {
|
||||||
|
// 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)?)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
37
src/main.rs
37
src/main.rs
|
@ -15,6 +15,8 @@
|
||||||
* along with Mention2Mail. If not, see <https://www.gnu.org/licenses/>.
|
* along with Mention2Mail. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
pub mod connect;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
format,
|
format,
|
||||||
env::args,
|
env::args,
|
||||||
|
@ -68,29 +70,6 @@ fn get_config<P: AsRef<Path>>(config_path: P)
|
||||||
Ok(Value::from(config))
|
Ok(Value::from(config))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tcp_stream (config: &Value)
|
|
||||||
-> Result<TcpStream, Box<dyn std::error::Error>> {
|
|
||||||
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<TlsStream<TcpStream>, Box<dyn std::error::Error>> {
|
|
||||||
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)
|
fn get_irc_identify_messages (config: &Value)
|
||||||
-> Result<VecDeque<Message>, Box<dyn std::error::Error>> {
|
-> Result<VecDeque<Message>, Box<dyn std::error::Error>> {
|
||||||
let mut queue = VecDeque::new();
|
let mut queue = VecDeque::new();
|
||||||
|
@ -165,17 +144,7 @@ fn main() {
|
||||||
.expect("no config given");
|
.expect("no config given");
|
||||||
let config = get_config(config_path).expect("Could not get config");
|
let config = get_config(config_path).expect("Could not get config");
|
||||||
|
|
||||||
// Weird workaround for multitrait dyn pointer
|
let stream = connect::connect_irc(&config).unwrap();
|
||||||
// https://github.com/rust-lang/rfcs/issues/2035
|
|
||||||
// would be a charm...
|
|
||||||
|
|
||||||
let mut stream: Box<dyn IrcReadWrite>;
|
|
||||||
|
|
||||||
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 mut message_queue = get_irc_identify_messages(&config).unwrap();
|
let mut message_queue = get_irc_identify_messages(&config).unwrap();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue