From 3e3821d835c084ca980db1d7402d14668c1db5cb Mon Sep 17 00:00:00 2001 From: Thelie Date: Tue, 4 May 2021 00:40:51 +0200 Subject: [PATCH 1/5] First trash commit --- src/main.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index a913402..68ef1cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,6 +135,28 @@ fn get_irc_identify_messages (config: &Value) Ok(queue) } +fn get_irc_join_messages(config: &Value, queue: VecDeque) + -> Result, Box> { + let mut queue = queue; + + match config.get("channels") { + Some(c) => { + for channel in c.as_array().ok_or("Could not parse channels.")? { + queue.push_back(Message::from( + Command::JOIN( + String::from(channel.as_str().ok_or("Could not parse one of the channels")?), + None, + None + ) + )) + } + }, + None => () + } + + Ok(queue) +} + fn main() { let config_path = args().nth(1) .expect("no config given"); @@ -153,13 +175,13 @@ fn main() { } 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(); } - stream.flush().unwrap(); - loop { let message = match stream.read() { Ok(m) => m, @@ -186,7 +208,7 @@ fn main() { msg, sender ), - _ => println!("Other message: {}", message.clone().to_string()) + _ => println!("{}", message.clone().to_string()) } } } -- 2.45.2 From 2e0c95ab570574af24b5c4fa38dd9aac1d9e9658 Mon Sep 17 00:00:00 2001 From: Thelie Date: Wed, 5 May 2021 20:31:49 +0200 Subject: [PATCH 2/5] Updated irc-stream and appearantly changed this crate, too. --- irc-stream | 2 +- src/main.rs | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/irc-stream b/irc-stream index bf53e62..455c49d 160000 --- a/irc-stream +++ b/irc-stream @@ -1 +1 @@ -Subproject commit bf53e62f1713dfe83198f75992cc0e889de398ff +Subproject commit 455c49dbb237b98c89692b46869ecb22cca8d0f5 diff --git a/src/main.rs b/src/main.rs index 68ef1cb..0d2c84d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,9 @@ use std::{ io::Read, net::TcpStream, collections::VecDeque, + thread, + time, + sync::mpsc::channel, }; use irc_proto::command::{ CapSubCommand, @@ -175,13 +178,23 @@ fn main() { } 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 +223,10 @@ fn main() { ), _ => println!("{}", message.clone().to_string()) } + match rx.try_recv() { + Ok(m) => stream.write(m).unwrap(), + Err(_e) => () + } } } -- 2.45.2 From ca48225d6b927dae92fde1bc889e848bb0b84bbf Mon Sep 17 00:00:00 2001 From: Thelie Date: Wed, 5 May 2021 21:17:35 +0200 Subject: [PATCH 3/5] =?UTF-8?q?The=20fight=20with=20generics=20continues?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- irc-stream | 2 +- src/connect.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 37 +++-------------------------- 3 files changed, 68 insertions(+), 35 deletions(-) create mode 100644 src/connect.rs diff --git a/irc-stream b/irc-stream index 455c49d..ef1e849 160000 --- a/irc-stream +++ b/irc-stream @@ -1 +1 @@ -Subproject commit 455c49dbb237b98c89692b46869ecb22cca8d0f5 +Subproject commit ef1e849d0ea7e6472a3fad86ea859a00597d27ea diff --git a/src/connect.rs b/src/connect.rs new file mode 100644 index 0000000..dc3ee6b --- /dev/null +++ b/src/connect.rs @@ -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 . + */ + +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 IrcReadWrite for T {} + +fn get_tcp_stream (config: &Value) + -> Result> { + 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, Box> { + 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 (config: &Value) + -> Result, Box> { + // 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)?))) + } +} + diff --git a/src/main.rs b/src/main.rs index 0d2c84d..9fc6218 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,8 @@ * along with Mention2Mail. If not, see . */ +pub mod connect; + use std::{ format, env::args, @@ -68,29 +70,6 @@ fn get_config>(config_path: P) Ok(Value::from(config)) } -fn get_tcp_stream (config: &Value) - -> Result> { - 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, Box> { - 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, Box> { let mut queue = VecDeque::new(); @@ -165,17 +144,7 @@ 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; - - 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(); -- 2.45.2 From a0f87876d7c6b31e71865cb74c1cd7d332dfe337 Mon Sep 17 00:00:00 2001 From: Thelie Date: Wed, 5 May 2021 21:20:39 +0200 Subject: [PATCH 4/5] Never mind. --- src/connect.rs | 2 +- src/main.rs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/connect.rs b/src/connect.rs index dc3ee6b..d0ff148 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -49,7 +49,7 @@ fn get_tls_stream (config: &Value) ) } -pub fn connect_irc (config: &Value) +pub fn connect_irc (config: &Value) -> Result, Box> { // Weird workaround for multitrait dyn pointer // https://github.com/rust-lang/rfcs/issues/2035 diff --git a/src/main.rs b/src/main.rs index 9fc6218..301fae1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,6 @@ use std::{ env::args, path::Path, io::Read, - net::TcpStream, collections::VecDeque, thread, time, @@ -33,9 +32,11 @@ use irc_proto::command::{ Command, }; use irc_proto::message::Message; -use irc_stream::{IrcStream, IrcRead, IrcWrite}; +use irc_stream::{ + IrcRead, + IrcWrite, +}; use toml::value::Value; -use native_tls::{TlsConnector,TlsStream}; trait IrcReadWrite: IrcRead + IrcWrite {} impl IrcReadWrite for T {} @@ -144,7 +145,7 @@ fn main() { .expect("no config given"); let config = get_config(config_path).expect("Could not get config"); - let stream = connect::connect_irc(&config).unwrap(); + let mut stream = connect::connect_irc(&config).unwrap(); let mut message_queue = get_irc_identify_messages(&config).unwrap(); -- 2.45.2 From 7cffb849dce88fdc734b5b994282d188060c2c69 Mon Sep 17 00:00:00 2001 From: Thelie Date: Wed, 5 May 2021 22:10:08 +0200 Subject: [PATCH 5/5] Fixed the channel joining issue. --- src/connect.rs | 8 ++--- src/main.rs | 86 +++++++++++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 37 deletions(-) diff --git a/src/connect.rs b/src/connect.rs index d0ff148..9d8f12c 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -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 IrcReadWrite for T {} @@ -51,10 +54,7 @@ fn get_tls_stream (config: &Value) pub fn connect_irc (config: &Value) -> Result, Box> { - // 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 { diff --git a/src/main.rs b/src/main.rs index 301fae1..ec283bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) Ok(queue) } +fn handle_message(message: Message) -> Option { + 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 => () } } } -- 2.45.2