Merge this trash pls #1
2 changed files with 57 additions and 37 deletions
|
@ -23,6 +23,9 @@ use irc_stream::{IrcStream, IrcRead, IrcWrite};
|
||||||
use toml::value::Value;
|
use toml::value::Value;
|
||||||
use native_tls::{TlsConnector,TlsStream};
|
use native_tls::{TlsConnector,TlsStream};
|
||||||
|
|
||||||
|
// Weird workaround for multitrait dyn pointer
|
||||||
|
// https://github.com/rust-lang/rfcs/issues/2035
|
||||||
|
// would be a charm...
|
||||||
pub trait IrcReadWrite: IrcRead + IrcWrite {}
|
pub trait IrcReadWrite: IrcRead + IrcWrite {}
|
||||||
impl<T: IrcRead + IrcWrite> IrcReadWrite for T {}
|
impl<T: IrcRead + IrcWrite> IrcReadWrite for T {}
|
||||||
|
|
||||||
|
@ -51,9 +54,6 @@ fn get_tls_stream (config: &Value)
|
||||||
|
|
||||||
pub fn connect_irc (config: &Value)
|
pub fn connect_irc (config: &Value)
|
||||||
-> Result<Box<dyn IrcReadWrite>, Box<dyn std::error::Error>> {
|
-> 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 {
|
if *config["tls"].as_bool().get_or_insert(true) == true {
|
||||||
return Ok(Box::new(IrcStream::new(get_tls_stream(&config)?)))
|
return Ok(Box::new(IrcStream::new(get_tls_stream(&config)?)))
|
||||||
|
|
82
src/main.rs
82
src/main.rs
|
@ -23,9 +23,6 @@ use std::{
|
||||||
path::Path,
|
path::Path,
|
||||||
io::Read,
|
io::Read,
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
thread,
|
|
||||||
time,
|
|
||||||
sync::mpsc::channel,
|
|
||||||
};
|
};
|
||||||
use irc_proto::command::{
|
use irc_proto::command::{
|
||||||
CapSubCommand,
|
CapSubCommand,
|
||||||
|
@ -140,6 +137,28 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque<Message>)
|
||||||
Ok(queue)
|
Ok(queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_message(message: Message) -> Option<Message> {
|
||||||
|
let sender = match message.clone().source_nickname() {
|
||||||
|
Some(s) => String::from(s),
|
||||||
|
None => String::from("anonymous")
|
||||||
|
};
|
||||||
|
match message.clone().command {
|
||||||
|
Command::PING(ref data, _) => {
|
||||||
|
return Some(Message::from(
|
||||||
|
Command::PONG(data.to_owned(), None)
|
||||||
|
));
|
||||||
|
},
|
||||||
|
Command::PRIVMSG(ref rec, ref msg) => println!(
|
||||||
|
"{} -> {}: {}",
|
||||||
|
rec,
|
||||||
|
msg,
|
||||||
|
sender
|
||||||
|
),
|
||||||
|
_ => println!("{}", message.clone().to_string())
|
||||||
|
}
|
||||||
|
return None
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let config_path = args().nth(1)
|
let config_path = args().nth(1)
|
||||||
.expect("no config given");
|
.expect("no config given");
|
||||||
|
@ -155,15 +174,34 @@ fn main() {
|
||||||
|
|
||||||
message_queue = get_irc_join_messages(&config, message_queue).unwrap();
|
message_queue = get_irc_join_messages(&config, message_queue).unwrap();
|
||||||
|
|
||||||
//TODO remove this botch
|
// Wait for first ping and join channels after sending pong.
|
||||||
let (tx, rx) = channel();
|
// TODO this approach is not very DRY!
|
||||||
thread::spawn(move|| {
|
loop {
|
||||||
thread::sleep(time::Duration::from_secs(10));
|
let message = match stream.read() {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(e) => Message::from(
|
||||||
|
Command::PRIVMSG(
|
||||||
|
String::from("Error"),
|
||||||
|
format!("{}", e)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
};
|
||||||
|
match message.command {
|
||||||
|
Command::PING(ref data, _) => {
|
||||||
|
stream.write(Message::from(
|
||||||
|
Command::PONG(data.to_owned(), None)
|
||||||
|
)).unwrap();
|
||||||
while let Some(message) = message_queue.pop_front() {
|
while let Some(message) = message_queue.pop_front() {
|
||||||
println!("Sending: {}", message.clone().to_string());
|
stream.write(message).unwrap();
|
||||||
tx.send(message).unwrap();
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => match handle_message(message) {
|
||||||
|
Some(m) => stream.write(m).unwrap(),
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let message = match stream.read() {
|
let message = match stream.read() {
|
||||||
|
@ -175,27 +213,9 @@ fn main() {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
let sender = match message.clone().source_nickname() {
|
match handle_message(message) {
|
||||||
Some(s) => String::from(s),
|
Some(m) => stream.write(m).unwrap(),
|
||||||
None => String::from("anonymous")
|
None => ()
|
||||||
};
|
|
||||||
match message.clone().command {
|
|
||||||
Command::PING(ref data, _) => {
|
|
||||||
stream.write(Message::from(
|
|
||||||
Command::PONG(data.to_owned(), None)
|
|
||||||
)).unwrap();
|
|
||||||
},
|
|
||||||
Command::PRIVMSG(ref rec, ref msg) => println!(
|
|
||||||
"{} -> {}: {}",
|
|
||||||
rec,
|
|
||||||
msg,
|
|
||||||
sender
|
|
||||||
),
|
|
||||||
_ => println!("{}", message.clone().to_string())
|
|
||||||
}
|
|
||||||
match rx.try_recv() {
|
|
||||||
Ok(m) => stream.write(m).unwrap(),
|
|
||||||
Err(_e) => ()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue