Implement /join, /part and /query

This commit is contained in:
Sijmen 2023-11-17 09:04:48 +01:00
parent c6f13f9388
commit 6209393cd0
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

View file

@ -58,6 +58,81 @@ struct Cri {
input_value: String, input_value: String,
} }
impl Cri {
fn send_message(&mut self, input_value: &str) {
if let Some(active_channel) = &self.message_log.active_channel {
let command = IrcCommand::PRIVMSG(active_channel.to_string(), input_value.to_string());
let message: irc::proto::Message = command.into();
self.message_log
.get_mut(self.message_log.active_channel.clone())
.messages
.push(IrcMessage::Privmsg {
nickname: String::from("cri"),
message: input_value.to_string(),
});
self.input_tx.borrow().send(message.clone()).unwrap();
}
}
fn send_command(&mut self, command: &str) {
let mut tokens = command.split_whitespace();
let command = tokens.next().unwrap();
match command {
"/join" => {
let channel = tokens.next().map(str::to_string).or_else(|| self.message_log.active_channel.clone());
if channel.is_none() {
// TODO error message
return;
}
let channel = channel.unwrap();
if !channel.starts_with('#') {
// TODO error message
return;
}
self
.input_tx
.borrow()
.send(IrcCommand::JOIN(channel, tokens.next().map(str::to_string), None).into())
.unwrap()
},
"/part" => {
let channel = tokens.next().map(str::to_string).or_else(|| self.message_log.active_channel.clone());
if channel.is_none() {
// TODO error message
return;
}
let channel = channel.unwrap();
if !channel.starts_with('#') {
// TODO error message
return;
}
self
.input_tx
.borrow()
.send(
IrcCommand::PART(
channel,
tokens.next().map(str::to_string),
)
.into(),
)
.unwrap()
}
"/query" => self
.message_log
.set_active(Some(tokens.next().unwrap().to_string())),
_ => todo!(),
}
}
}
impl Application for Cri { impl Application for Cri {
type Executor = executor::Default; type Executor = executor::Default;
type Message = UiMessage; type Message = UiMessage;
@ -81,33 +156,31 @@ impl Application for Cri {
} }
fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> { fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
use irc::proto::Command;
match message { match message {
UiMessage::IrcMessageReceived(message) => { UiMessage::IrcMessageReceived(message) => {
// TODO use actual nickname // TODO use actual nickname
let source_nickname = message.source_nickname().unwrap_or("cri").to_string(); let source_nickname = message.source_nickname().unwrap_or("cri").to_string();
match &message.command { match &message.command {
Command::JOIN(chanlist, _, _) => { IrcCommand::JOIN(chanlist, _, _) => {
self.message_log.on_join(chanlist, &source_nickname); self.message_log.on_join(chanlist, &source_nickname);
} }
Command::PART(chanlist, comment) => { IrcCommand::PART(chanlist, comment) => {
self.message_log self.message_log
.on_part(chanlist, &source_nickname, comment.as_deref()); .on_part(chanlist, &source_nickname, comment.as_deref());
} }
Command::NICK(new) => { IrcCommand::NICK(new) => {
self.message_log.on_nick(&source_nickname, new); self.message_log.on_nick(&source_nickname, new);
} }
Command::QUIT(comment) => { IrcCommand::QUIT(comment) => {
self.message_log self.message_log
.on_quit(&source_nickname, comment.as_deref()); .on_quit(&source_nickname, comment.as_deref());
} }
Command::PRIVMSG(msgtarget, content) => { IrcCommand::PRIVMSG(msgtarget, content) => {
let channel = message.response_target().unwrap_or(msgtarget).to_string(); let channel = message.response_target().unwrap_or(msgtarget).to_string();
self.message_log self.message_log
.on_privmsg(&channel, &source_nickname, content); .on_privmsg(&channel, &source_nickname, content);
@ -118,21 +191,12 @@ impl Application for Cri {
} }
UiMessage::InputChanged(text) => self.input_value = text, UiMessage::InputChanged(text) => self.input_value = text,
UiMessage::InputSubmitted => { UiMessage::InputSubmitted => {
if let Some(active_channel) = &self.message_log.active_channel { if self.input_value.starts_with("//") {
let command = irc::proto::Command::PRIVMSG( self.send_message(&self.input_value.clone()[1..])
active_channel.to_string(), } else if self.input_value.starts_with('/') {
self.input_value.clone(), self.send_command(&self.input_value.clone())
); } else {
let message: irc::proto::Message = command.into(); self.send_message(&self.input_value.clone());
self.message_log
.get_mut(self.message_log.active_channel.clone())
.messages
.push(IrcMessage::Privmsg {
nickname: String::from("cri"),
message: self.input_value.clone(),
});
self.input_tx.borrow().send(message.clone()).unwrap();
} }
self.input_value.clear(); self.input_value.clear();