From a90df6ef31255506b17595fb7056ef1fe5b19b06 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Thu, 23 Nov 2023 00:34:37 +0100 Subject: [PATCH] Gracefully fall back when capability echo-message is not supported --- src/irc_handler.rs | 12 +++++++----- src/main.rs | 25 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/irc_handler.rs b/src/irc_handler.rs index 417d40b..b4279ea 100644 --- a/src/irc_handler.rs +++ b/src/irc_handler.rs @@ -8,8 +8,8 @@ use tokio::{ pub async fn connect() -> Result { let client = irc::client::Client::new("config.toml").await?; - client.send_cap_req(&[ - Capability::AccountTag, + // TODO Base this on a CAP LS + for capability in [ Capability::Batch, Capability::EchoMessage, Capability::ServerTime, @@ -17,7 +17,9 @@ pub async fn connect() -> Result { Capability::Custom("draft/chathistory"), Capability::Custom("draft/event-playback"), Capability::Custom("draft/multiline"), - ])?; + ] { + _ = client.send_cap_req(&[capability]); + } Ok(client) } @@ -33,13 +35,13 @@ pub async fn message_loop( select! { val = irc_stream.next() => { if let Some(message) = val.transpose()? { - println!("[Rx] {} {:?}", message.to_string().trim(), message.tags); + println!("[Rx] {}", message.to_string().trim()); message_tx.send(message)?; } } val = input_rx.recv() => { let message = val.unwrap(); - println!("[Tx] {} {:?}", message.to_string().trim(), message.tags); + println!("[Tx] {}", message.to_string().trim()); client.send(message)?; } } diff --git a/src/main.rs b/src/main.rs index 04dc7b5..8d7fb3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use iced::{ widget::{column, container, mouse_area, row, text, text_input}, Application, Background, Color, Element, Length, Settings, }; -use irc::proto::{message::Tag, Command as IrcCommand, Response}; +use irc::proto::{message::Tag, CapSubCommand, Capability, Command as IrcCommand, Response}; use irc_message::MessageDetail; use message_log::MessageLog; use once_cell::sync::Lazy; @@ -60,14 +60,30 @@ struct Cri { message_log: MessageLog, input_value: String, nickname: String, + capabilities: Vec, } impl Cri { fn send_message(&mut self, input_value: &str) { - if let Some(active_channel) = &self.message_log.active_channel { + let active_channel = self.message_log.active_channel.clone(); + if let Some(active_channel) = &active_channel { let command = IrcCommand::PRIVMSG(active_channel.to_string(), input_value.to_string()); let message: irc::proto::Message = command.into(); self.input_tx.borrow().send(message.clone()).unwrap(); + + let echo_message = self + .capabilities + .contains(&Capability::EchoMessage.as_ref().to_string()); + if !echo_message { + self.message_log.on_privmsg( + &self.nickname, + active_channel, + &self.nickname, + input_value, + None, + &Local::now(), + ); + } } } @@ -155,6 +171,7 @@ impl Application for Cri { message_log: MessageLog::new(), input_value: String::new(), nickname: "cri".to_string(), // TODO take default value from config + capabilities: Vec::new(), }, iced::Command::none(), ) @@ -193,6 +210,10 @@ impl Application for Cri { let message_id = message_id.as_deref(); match &message.command { + IrcCommand::CAP(_, CapSubCommand::ACK, capability, _) => { + let capability = capability.as_ref().unwrap(); + self.capabilities.push(capability.clone()); + } IrcCommand::JOIN(chanlist, _, _) => { let already_joined = self.message_log.has_channel(chanlist); self.message_log.on_join(