Move some IRC code to separate file and use the irc crate's config.toml

This commit is contained in:
Sijmen 2023-11-17 09:04:26 +01:00
parent 7f518f7e1e
commit c6f13f9388
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
3 changed files with 41 additions and 44 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
config.toml

35
src/irc_handler.rs Normal file
View file

@ -0,0 +1,35 @@
use color_eyre::eyre::Result;
use futures::StreamExt;
use tokio::{
select,
sync::mpsc::{UnboundedReceiver, UnboundedSender},
};
pub async fn connect() -> Result<irc::client::Client> {
Ok(irc::client::Client::new("config.toml").await?)
}
pub async fn message_loop(
mut client: irc::client::Client,
message_tx: UnboundedSender<irc::proto::Message>,
mut input_rx: UnboundedReceiver<irc::proto::Message>,
) -> 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)?;
}
}
}
}

View file

@ -1,9 +1,9 @@
mod irc_handler;
mod irc_message; mod irc_message;
mod message_log; mod message_log;
use crate::{irc_message::IrcMessage, message_log::MessageLog}; use crate::{irc_message::IrcMessage, message_log::MessageLog};
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use futures::StreamExt;
use iced::{ use iced::{
alignment::{Horizontal, Vertical}, alignment::{Horizontal, Vertical},
executor, executor,
@ -11,40 +11,13 @@ use iced::{
widget::{column, container, mouse_area, row, text, text_input}, widget::{column, container, mouse_area, row, text, text_input},
Application, Background, Color, Element, Length, Settings, Application, Background, Color, Element, Length, Settings,
}; };
use irc::proto::Command as IrcCommand;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::cell::RefCell; use std::cell::RefCell;
use tokio::{ use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
select,
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
};
static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique); static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique);
async fn irc_loop(
mut client: irc::client::Client,
message_tx: UnboundedSender<irc::proto::Message>,
mut input_rx: UnboundedReceiver<irc::proto::Message>,
) -> 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] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
color_eyre::install()?; color_eyre::install()?;
@ -52,20 +25,8 @@ async fn main() -> Result<()> {
let (message_tx, message_rx) = mpsc::unbounded_channel::<irc::proto::Message>(); let (message_tx, message_rx) = mpsc::unbounded_channel::<irc::proto::Message>();
let (input_tx, input_rx) = mpsc::unbounded_channel::<irc::proto::Message>(); let (input_tx, input_rx) = mpsc::unbounded_channel::<irc::proto::Message>();
let config = irc::client::prelude::Config { let client = irc_handler::connect().await?;
nickname: Some("cri".to_string()), let task = tokio::task::spawn(irc_handler::message_loop(client, message_tx, input_rx));
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));
Cri::run(Settings::with_flags(CriFlags { Cri::run(Settings::with_flags(CriFlags {
message_rx, message_rx,