From c6f13f9388a640a4db5bcfb1bc1a874f2b3d90c1 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Fri, 17 Nov 2023 09:04:26 +0100 Subject: [PATCH] Move some IRC code to separate file and use the irc crate's config.toml --- .gitignore | 1 + src/irc_handler.rs | 35 +++++++++++++++++++++++++++++++++ src/main.rs | 49 +++++----------------------------------------- 3 files changed, 41 insertions(+), 44 deletions(-) create mode 100644 src/irc_handler.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..db38427 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +config.toml \ No newline at end of file diff --git a/src/irc_handler.rs b/src/irc_handler.rs new file mode 100644 index 0000000..125c973 --- /dev/null +++ b/src/irc_handler.rs @@ -0,0 +1,35 @@ +use color_eyre::eyre::Result; +use futures::StreamExt; +use tokio::{ + select, + sync::mpsc::{UnboundedReceiver, UnboundedSender}, +}; + +pub async fn connect() -> Result { + Ok(irc::client::Client::new("config.toml").await?) +} + +pub async fn message_loop( + mut client: irc::client::Client, + message_tx: UnboundedSender, + mut input_rx: UnboundedReceiver, +) -> Result<()> { + client.identify()?; + let mut irc_stream = client.stream()?; + + loop { + select! { + val = irc_stream.next() => { + if let Some(message) = val.transpose()? { + print!("[Rx] {message}"); + message_tx.send(message)?; + } + } + val = input_rx.recv() => { + let message = val.unwrap(); + print!("[Tx] {message}"); + client.send(message)?; + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 281a1c2..38f7ac6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ +mod irc_handler; mod irc_message; mod message_log; use crate::{irc_message::IrcMessage, message_log::MessageLog}; use color_eyre::eyre::Result; -use futures::StreamExt; use iced::{ alignment::{Horizontal, Vertical}, executor, @@ -11,40 +11,13 @@ use iced::{ widget::{column, container, mouse_area, row, text, text_input}, Application, Background, Color, Element, Length, Settings, }; +use irc::proto::Command as IrcCommand; use once_cell::sync::Lazy; use std::cell::RefCell; -use tokio::{ - select, - sync::mpsc::{self, UnboundedReceiver, UnboundedSender}, -}; +use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; static INPUT_ID: Lazy = Lazy::new(text_input::Id::unique); -async fn irc_loop( - mut client: irc::client::Client, - message_tx: UnboundedSender, - mut input_rx: UnboundedReceiver, -) -> Result<()> { - client.identify()?; - let mut irc_stream = client.stream()?; - - loop { - select! { - val = irc_stream.next() => { - if let Some(message) = val.transpose()? { - print!("[Rx] {message}"); - message_tx.send(message)?; - } - } - val = input_rx.recv() => { - let message = val.unwrap(); - print!("[Tx] {message}"); - client.send(message)?; - } - } - } -} - #[tokio::main] async fn main() -> Result<()> { color_eyre::install()?; @@ -52,20 +25,8 @@ async fn main() -> Result<()> { let (message_tx, message_rx) = mpsc::unbounded_channel::(); let (input_tx, input_rx) = mpsc::unbounded_channel::(); - let config = irc::client::prelude::Config { - nickname: Some("cri".to_string()), - server: Some("vijf.life".to_string()), - channels: vec![ - "#h".to_string(), - "#test".to_string(), - "#test1".to_string(), - "#test2".to_string(), - ], - ..Default::default() - }; - - let client = irc::client::Client::from_config(config).await?; - let task = tokio::task::spawn(irc_loop(client, message_tx, input_rx)); + let client = irc_handler::connect().await?; + let task = tokio::task::spawn(irc_handler::message_loop(client, message_tx, input_rx)); Cri::run(Settings::with_flags(CriFlags { message_rx,