Fixed the channel joining issue.

This commit is contained in:
Thelie 2021-05-05 22:10:08 +02:00
parent a0f87876d7
commit 7cffb849dc
2 changed files with 57 additions and 37 deletions

View file

@ -23,6 +23,9 @@ use irc_stream::{IrcStream, IrcRead, IrcWrite};
use toml::value::Value;
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 {}
impl<T: IrcRead + IrcWrite> IrcReadWrite for T {}
@ -51,10 +54,7 @@ fn get_tls_stream (config: &Value)
pub fn connect_irc (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 {

View file

@ -23,9 +23,6 @@ use std::{
path::Path,
io::Read,
collections::VecDeque,
thread,
time,
sync::mpsc::channel,
};
use irc_proto::command::{
CapSubCommand,
@ -140,6 +137,28 @@ fn get_irc_join_messages(config: &Value, queue: VecDeque<Message>)
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() {
let config_path = args().nth(1)
.expect("no config given");
@ -155,15 +174,34 @@ fn main() {
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();
}
});
// Wait for first ping and join channels after sending pong.
// TODO this approach is not very DRY!
loop {
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() {
stream.write(message).unwrap();
}
break;
}
_ => match handle_message(message) {
Some(m) => stream.write(m).unwrap(),
None => ()
}
}
}
loop {
let message = match stream.read() {
@ -175,27 +213,9 @@ fn main() {
)
)
};
let sender = match message.clone().source_nickname() {
Some(s) => String::from(s),
None => String::from("anonymous")
};
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) => ()
match handle_message(message) {
Some(m) => stream.write(m).unwrap(),
None => ()
}
}
}