diff --git a/src/cri.rs b/src/cri.rs index 8f5818e..6695406 100644 --- a/src/cri.rs +++ b/src/cri.rs @@ -1,4 +1,4 @@ -use std::{cell::RefCell, collections::HashMap}; +use std::{cell::RefCell, collections::HashMap, str::SplitWhitespace}; use chrono::{DateTime, Local}; use iced::{ @@ -18,7 +18,7 @@ use crate::{ irc_message::MessageDetail, message_log::MessageLog, ui_message::{self, UiMessage}, - util, + util::{self, BATCH_SUB_COMMAND_CHATHISTORY}, }; static INPUT_ID: Lazy = Lazy::new(text_input::Id::unique); @@ -63,13 +63,14 @@ impl Cri { match command { "/join" | "/j" => self.handle_join_command(&mut tokens), "/part" | "/p" => self.handle_part_command(&mut tokens), - "/query" | "/q" => self.handle_query_command(tokens), + "/query" | "/q" => self.handle_query_command(&mut tokens), + "/nick" | "/n" => self.handle_nick_command(&mut tokens), "/list" => self.handle_list_command(), _ => (), } } - fn handle_part_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) { + fn handle_part_command(&mut self, tokens: &mut SplitWhitespace<'_>) { let channel = tokens .next() .map(String::from) @@ -98,7 +99,7 @@ impl Cri { .unwrap(); } - fn handle_join_command(&mut self, tokens: &mut std::str::SplitWhitespace<'_>) { + fn handle_join_command(&mut self, tokens: &mut SplitWhitespace<'_>) { let channel = tokens .next() .map(String::from) @@ -121,10 +122,19 @@ impl Cri { self.message_log.set_active(Some(&channel)); } - fn handle_query_command(&mut self, mut tokens: std::str::SplitWhitespace<'_>) { + fn handle_query_command(&mut self, tokens: &mut SplitWhitespace<'_>) { self.message_log.set_active(Some(tokens.next().unwrap())); } + fn handle_nick_command(&self, tokens: &mut SplitWhitespace<'_>) { + if let Some(new_nick) = tokens.next() { + self.input_tx + .borrow() + .send(IrcCommand::NICK(new_nick.into()).into()) + .unwrap(); + } + } + fn handle_list_command(&self) { self.input_tx .borrow() @@ -239,6 +249,18 @@ impl Application for Cri { IrcCommand::NICK(new) => { self.message_log .on_nick(&source_nickname, new, ×tamp, message_id); + + if let Some(batch) = batch { + let (subcommand, _) = &self.message_log.batch_channels[&batch]; + + if subcommand != &*BATCH_SUB_COMMAND_CHATHISTORY + && source_nickname == self.nickname + { + self.nickname = new.into(); + } + } else { + self.nickname = new.into(); + } } IrcCommand::QUIT(comment) => { diff --git a/src/message_log.rs b/src/message_log.rs index 235f004..7d2b15f 100644 --- a/src/message_log.rs +++ b/src/message_log.rs @@ -1,6 +1,6 @@ use crate::{ irc_message::{IrcMessage, MessageDetail}, - util, + util::{self, BATCH_SUB_COMMAND_CHATHISTORY}, }; use chrono::{DateTime, Local}; @@ -184,7 +184,7 @@ impl<'a> MessageLog { ) { if let Some(batch) = batch { let (subcommand, channel_name) = &self.batch_channels[&batch]; - if subcommand == &BatchSubCommand::CUSTOM("CHATHISTORY".into()) { + if subcommand == &*BATCH_SUB_COMMAND_CHATHISTORY { if let Some(channel) = self.channels.get_mut(&Some(channel_name.to_owned())) { if message_id.is_some() && !channel.message_ids.insert(message_id.unwrap().into()) @@ -246,7 +246,7 @@ impl<'a> MessageLog { channel.is_multiline = true; channel.multiline_privmsgs = Some(Vec::new()); channel.multiline_timestamp = Some(*timestamp); - } else if subcommand == Some(&BatchSubCommand::CUSTOM("CHATHISTORY".into())) { + } else if subcommand == Some(&*BATCH_SUB_COMMAND_CHATHISTORY) { let channel_name = ¶ms.unwrap()[0]; self.batch_channels.insert( tag.into(), @@ -277,7 +277,7 @@ impl<'a> MessageLog { None, ×tamp, ); - } else if subcommand == BatchSubCommand::CUSTOM("CHATHISTORY".into()) { + } else if subcommand == *BATCH_SUB_COMMAND_CHATHISTORY { // TODO } } diff --git a/src/util.rs b/src/util.rs index c63b218..4b64b08 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,6 @@ use iced::Color; +use irc::proto::BatchSubCommand; +use once_cell::sync::Lazy; use regex::Regex; pub const WHITE: Color = Color::WHITE; @@ -44,6 +46,9 @@ pub const DARK_RED: Color = Color { a: 1.0, }; +pub static BATCH_SUB_COMMAND_CHATHISTORY: Lazy = + Lazy::new(|| BatchSubCommand::CUSTOM("CHATHISTORY".into())); + pub fn contains_word(needle: &str, haystack: &str) -> bool { let pattern = format!(r"\b{}\b", regex::escape(needle)); let regex = Regex::new(&pattern).unwrap();