Gracefully fall back when capability echo-message is not supported

This commit is contained in:
Sijmen 2023-11-23 00:34:37 +01:00
parent 9006f53e4d
commit a90df6ef31
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
2 changed files with 30 additions and 7 deletions

View file

@ -8,8 +8,8 @@ use tokio::{
pub async fn connect() -> Result<irc::client::Client> { pub async fn connect() -> Result<irc::client::Client> {
let client = irc::client::Client::new("config.toml").await?; let client = irc::client::Client::new("config.toml").await?;
client.send_cap_req(&[ // TODO Base this on a CAP LS
Capability::AccountTag, for capability in [
Capability::Batch, Capability::Batch,
Capability::EchoMessage, Capability::EchoMessage,
Capability::ServerTime, Capability::ServerTime,
@ -17,7 +17,9 @@ pub async fn connect() -> Result<irc::client::Client> {
Capability::Custom("draft/chathistory"), Capability::Custom("draft/chathistory"),
Capability::Custom("draft/event-playback"), Capability::Custom("draft/event-playback"),
Capability::Custom("draft/multiline"), Capability::Custom("draft/multiline"),
])?; ] {
_ = client.send_cap_req(&[capability]);
}
Ok(client) Ok(client)
} }
@ -33,13 +35,13 @@ pub async fn message_loop(
select! { select! {
val = irc_stream.next() => { val = irc_stream.next() => {
if let Some(message) = val.transpose()? { if let Some(message) = val.transpose()? {
println!("[Rx] {} {:?}", message.to_string().trim(), message.tags); println!("[Rx] {}", message.to_string().trim());
message_tx.send(message)?; message_tx.send(message)?;
} }
} }
val = input_rx.recv() => { val = input_rx.recv() => {
let message = val.unwrap(); let message = val.unwrap();
println!("[Tx] {} {:?}", message.to_string().trim(), message.tags); println!("[Tx] {}", message.to_string().trim());
client.send(message)?; client.send(message)?;
} }
} }

View file

@ -11,7 +11,7 @@ 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::{message::Tag, Command as IrcCommand, Response}; use irc::proto::{message::Tag, CapSubCommand, Capability, Command as IrcCommand, Response};
use irc_message::MessageDetail; use irc_message::MessageDetail;
use message_log::MessageLog; use message_log::MessageLog;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -60,14 +60,30 @@ struct Cri {
message_log: MessageLog, message_log: MessageLog,
input_value: String, input_value: String,
nickname: String, nickname: String,
capabilities: Vec<String>,
} }
impl Cri { impl Cri {
fn send_message(&mut self, input_value: &str) { 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 command = IrcCommand::PRIVMSG(active_channel.to_string(), input_value.to_string());
let message: irc::proto::Message = command.into(); let message: irc::proto::Message = command.into();
self.input_tx.borrow().send(message.clone()).unwrap(); 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(), message_log: MessageLog::new(),
input_value: String::new(), input_value: String::new(),
nickname: "cri".to_string(), // TODO take default value from config nickname: "cri".to_string(), // TODO take default value from config
capabilities: Vec::new(),
}, },
iced::Command::none(), iced::Command::none(),
) )
@ -193,6 +210,10 @@ impl Application for Cri {
let message_id = message_id.as_deref(); let message_id = message_id.as_deref();
match &message.command { match &message.command {
IrcCommand::CAP(_, CapSubCommand::ACK, capability, _) => {
let capability = capability.as_ref().unwrap();
self.capabilities.push(capability.clone());
}
IrcCommand::JOIN(chanlist, _, _) => { IrcCommand::JOIN(chanlist, _, _) => {
let already_joined = self.message_log.has_channel(chanlist); let already_joined = self.message_log.has_channel(chanlist);
self.message_log.on_join( self.message_log.on_join(