Compare commits
2 commits
3e3821d835
...
ca48225d6b
Author | SHA1 | Date | |
---|---|---|---|
ca48225d6b | |||
2e0c95ab57 |
3 changed files with 87 additions and 37 deletions
|
@ -1 +1 @@
|
|||
Subproject commit bf53e62f1713dfe83198f75992cc0e889de398ff
|
||||
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)?)))
|
||||
}
|
||||
}
|
||||
|
58
src/main.rs
58
src/main.rs
|
@ -15,6 +15,8 @@
|
|||
* along with Mention2Mail. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
pub mod connect;
|
||||
|
||||
use std::{
|
||||
format,
|
||||
env::args,
|
||||
|
@ -22,6 +24,9 @@ use std::{
|
|||
io::Read,
|
||||
net::TcpStream,
|
||||
collections::VecDeque,
|
||||
thread,
|
||||
time,
|
||||
sync::mpsc::channel,
|
||||
};
|
||||
use irc_proto::command::{
|
||||
CapSubCommand,
|
||||
|
@ -65,29 +70,6 @@ fn get_config<P: AsRef<Path>>(config_path: P)
|
|||
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)
|
||||
-> Result<VecDeque<Message>, Box<dyn std::error::Error>> {
|
||||
let mut queue = VecDeque::new();
|
||||
|
@ -162,26 +144,26 @@ 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<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 stream = connect::connect_irc(&config).unwrap();
|
||||
|
||||
let mut message_queue = get_irc_identify_messages(&config).unwrap();
|
||||
message_queue = get_irc_join_messages(&config, message_queue).unwrap();
|
||||
|
||||
while let Some(message) = message_queue.pop_front() {
|
||||
println!("Sending: {}", message.clone().to_string());
|
||||
stream.write(message).unwrap();
|
||||
}
|
||||
|
||||
message_queue = get_irc_join_messages(&config, message_queue).unwrap();
|
||||
|
||||
//TODO remove this botch
|
||||
let (tx, rx) = channel();
|
||||
thread::spawn(move|| {
|
||||
thread::sleep(time::Duration::from_secs(10));
|
||||
while let Some(message) = message_queue.pop_front() {
|
||||
println!("Sending: {}", message.clone().to_string());
|
||||
tx.send(message).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
let message = match stream.read() {
|
||||
Ok(m) => m,
|
||||
|
@ -210,6 +192,10 @@ fn main() {
|
|||
),
|
||||
_ => println!("{}", message.clone().to_string())
|
||||
}
|
||||
match rx.try_recv() {
|
||||
Ok(m) => stream.write(m).unwrap(),
|
||||
Err(_e) => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue