Compare commits
No commits in common. "ca48225d6b927dae92fde1bc889e848bb0b84bbf" and "3e3821d835c084ca980db1d7402d14668c1db5cb" have entirely different histories.
ca48225d6b
...
3e3821d835
3 changed files with 38 additions and 88 deletions
|
@ -1 +1 @@
|
|||
Subproject commit ef1e849d0ea7e6472a3fad86ea859a00597d27ea
|
||||
Subproject commit bf53e62f1713dfe83198f75992cc0e889de398ff
|
|
@ -1,64 +0,0 @@
|
|||
/* 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)?)))
|
||||
}
|
||||
}
|
||||
|
56
src/main.rs
56
src/main.rs
|
@ -15,8 +15,6 @@
|
|||
* along with Mention2Mail. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
pub mod connect;
|
||||
|
||||
use std::{
|
||||
format,
|
||||
env::args,
|
||||
|
@ -24,9 +22,6 @@ use std::{
|
|||
io::Read,
|
||||
net::TcpStream,
|
||||
collections::VecDeque,
|
||||
thread,
|
||||
time,
|
||||
sync::mpsc::channel,
|
||||
};
|
||||
use irc_proto::command::{
|
||||
CapSubCommand,
|
||||
|
@ -70,6 +65,29 @@ 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();
|
||||
|
@ -144,25 +162,25 @@ fn main() {
|
|||
.expect("no config given");
|
||||
let config = get_config(config_path).expect("Could not get config");
|
||||
|
||||
let stream = connect::connect_irc(&config).unwrap();
|
||||
// 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 mut message_queue = get_irc_identify_messages(&config).unwrap();
|
||||
|
||||
while let Some(message) = message_queue.pop_front() {
|
||||
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();
|
||||
stream.write(message).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
let message = match stream.read() {
|
||||
|
@ -192,10 +210,6 @@ 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