From f6de14bea18bc90f11b1e4f7fc2fc1847ef99f32 Mon Sep 17 00:00:00 2001 From: Dastan-glitch Date: Thu, 19 May 2022 19:48:14 +0000 Subject: [PATCH] bin/ircd: fix part command --- bin/ircd/src/main.rs | 3 +++ bin/ircd/src/server.rs | 54 +++++++++++++++++++++------------------- bin/ircd/src/settings.rs | 4 ++- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/bin/ircd/src/main.rs b/bin/ircd/src/main.rs index dde259b71..fec4c6e41 100644 --- a/bin/ircd/src/main.rs +++ b/bin/ircd/src/main.rs @@ -120,6 +120,9 @@ async fn process( // Try to potentially decrypt the incoming message. if conn.configured_chans.contains_key(&msg.channel) { let chan_info = conn.configured_chans.get(&msg.channel).unwrap(); + if !chan_info.joined { + continue + } if let Some(salt_box) = &chan_info.salt_box { if let Some(decrypted_msg) = try_decrypt_message(salt_box, &msg.message) { msg.message = decrypted_msg; diff --git a/bin/ircd/src/server.rs b/bin/ircd/src/server.rs index e517ca182..57caa2780 100644 --- a/bin/ircd/src/server.rs +++ b/bin/ircd/src/server.rs @@ -83,6 +83,8 @@ impl IrcServerConnection { for chan in channels.split(',') { let part_reply = format!(":{}!anon@dark.fi PART {}\r\n", self.nickname, chan); self.reply(&part_reply).await?; + let chan_info = self.configured_chans.get_mut(chan).unwrap(); + chan_info.joined = false; } } "TOPIC" => { @@ -131,34 +133,34 @@ impl IrcServerConnection { let message = &line[substr_idx + 1..]; info!("(Plain) PRIVMSG {} :{}", channel, message); - let message = if self.configured_chans.contains_key(channel) { + if self.configured_chans.contains_key(channel) { let channel_info = self.configured_chans.get(channel).unwrap(); - if let Some(salt_box) = &channel_info.salt_box { - let encrypted = encrypt_message(salt_box, message); - info!("(Encrypted) PRIVMSG {} :{}", channel, encrypted); - encrypted - } else { - message.to_string() + if channel_info.joined { + let message = if let Some(salt_box) = &channel_info.salt_box { + let encrypted = encrypt_message(salt_box, message); + info!("(Encrypted) PRIVMSG {} :{}", channel, encrypted); + encrypted + } else { + message.to_string() + }; + + let random_id = OsRng.next_u32(); + + let protocol_msg = Privmsg { + id: random_id, + nickname: self.nickname.clone(), + channel: channel.to_string(), + message, + }; + + let mut smi = self.seen_msg_id.lock().await; + smi.push(random_id); + drop(smi); + + debug!(target: "ircd", "PRIVMSG to be sent: {:?}", protocol_msg); + self.p2p_sender.send(protocol_msg).await?; } - } else { - message.to_string() - }; - - let random_id = OsRng.next_u32(); - - let protocol_msg = Privmsg { - id: random_id, - nickname: self.nickname.clone(), - channel: channel.to_string(), - message, - }; - - let mut smi = self.seen_msg_id.lock().await; - smi.push(random_id); - drop(smi); - - debug!(target: "ircd", "PRIVMSG to be sent: {:?}", protocol_msg); - self.p2p_sender.send(protocol_msg).await?; + } } "QUIT" => { // Close the connection diff --git a/bin/ircd/src/settings.rs b/bin/ircd/src/settings.rs index 51d13bcd4..596db29de 100644 --- a/bin/ircd/src/settings.rs +++ b/bin/ircd/src/settings.rs @@ -68,11 +68,13 @@ pub struct ChannelInfo { pub topic: Option, /// Optional NaCl box for the channel, used for {en,de}cryption. pub salt_box: Option, + /// + pub joined: bool, } impl ChannelInfo { pub fn new() -> Result { - Ok(Self { topic: None, salt_box: None }) + Ok(Self { topic: None, salt_box: None, joined: true }) } }