Răsfoiți Sursa

ircd: Minor cleanup and more verbosity.

parazyd 4 ani în urmă
părinte
comite
5718e22347
4 a modificat fișierele cu 27 adăugiri și 22 ștergeri
  1. 4 3
      bin/ircd/src/crypto.rs
  2. 3 2
      bin/ircd/src/main.rs
  3. 16 16
      bin/ircd/src/server.rs
  4. 4 1
      bin/ircd/src/settings.rs

+ 4 - 3
bin/ircd/src/crypto.rs

@@ -9,6 +9,10 @@ pub fn try_decrypt_message(salt_box: &crypto_box::Box, ciphertext: &str) -> Opti
         Err(_) => return None,
     };
 
+    if bytes.len() < 25 {
+        return None
+    }
+
     // Try extracting the nonce
     let nonce = match bytes[0..24].try_into() {
         Ok(v) => v,
@@ -16,9 +20,6 @@ pub fn try_decrypt_message(salt_box: &crypto_box::Box, ciphertext: &str) -> Opti
     };
 
     // Take the remaining ciphertext
-    if bytes.len() < 25 {
-        return None
-    }
     let message = &bytes[24..];
 
     // Try decrypting the message

+ 3 - 2
bin/ircd/src/main.rs

@@ -107,8 +107,8 @@ async fn process(
         let mut line = String::new();
         futures::select! {
             privmsg = raft_receiver.recv().fuse() => {
-                info!("Receive msg from raft");
                 let mut msg = privmsg?;
+                info!("Received msg from Raft: {:?}", msg);
 
                 let mut smi = seen_msg_id.lock().await;
                 if smi.contains(&msg.id) {
@@ -123,6 +123,7 @@ async fn process(
                     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;
+                            info!("Decrypted received message: {:?}", msg);
                         }
                     }
                 }
@@ -135,7 +136,7 @@ async fn process(
                     warn!("Read line error. Closing stream for {}: {}", peer_addr, e);
                     return Ok(())
                 }
-                info!("Receive msg from IRC server");
+                info!("Received msg from ircd: {:?}", line);
                 let irc_msg = match clean_input(line, &peer_addr) {
                     Ok(m) => m,
                     Err(e) => return Err(e)

+ 16 - 16
bin/ircd/src/server.rs

@@ -83,7 +83,6 @@ 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?;
-                    //self.configured_chans.remove(chan);
                 }
             }
             "TOPIC" => {
@@ -130,13 +129,13 @@ impl IrcServerConnection {
                 }
 
                 let message = &line[substr_idx + 1..];
-                info!("Message {}: {}", channel, message);
+                info!("(Plain) PRIVMSG {} :{}", channel, message);
 
                 let message = 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 message {}: {}", channel, encrypted);
+                        info!("(Encrypted) PRIVMSG {} :{}", channel, encrypted);
                         encrypted
                     } else {
                         message.to_string()
@@ -151,13 +150,14 @@ impl IrcServerConnection {
                     id: random_id,
                     nickname: self.nickname.clone(),
                     channel: channel.to_string(),
-                    message: message.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" => {
@@ -202,21 +202,21 @@ impl IrcServerConnection {
             }
 
             for chan in self.auto_channels.clone() {
-                let topic = if self.configured_chans.contains_key(&chan) {
-                    let c = self.configured_chans.get(&chan).unwrap().clone();
-                    if let Some(topic) = c.topic {
+                if self.configured_chans.contains_key(&chan) {
+                    let chan_info = self.configured_chans.get_mut(&chan).unwrap();
+                    let topic = if let Some(topic) = chan_info.topic.clone() {
                         topic
                     } else {
-                        "".to_string()
-                    }
+                        "n/a".to_string()
+                    };
+                    chan_info.topic = Some(topic.to_string());
+                    autojoin!(chan, topic);
                 } else {
-                    "".to_string()
-                };
-
-                let mut ci = ChannelInfo::new()?;
-                ci.topic = Some(topic.to_string());
-                self.configured_chans.insert(chan.clone(), ci);
-                autojoin!(chan, topic.clone());
+                    let mut chan_info = ChannelInfo::new()?;
+                    chan_info.topic = Some("n/a".to_string());
+                    self.configured_chans.insert(chan.clone(), chan_info);
+                    autojoin!(chan, "n/a");
+                }
             }
         }
 

+ 4 - 1
bin/ircd/src/settings.rs

@@ -89,7 +89,9 @@ pub fn parse_configured_channels(config_file: &PathBuf) -> Result<FxHashMap<Stri
                 let mut channel_info = ChannelInfo::new()?;
 
                 if chan.1.as_table().unwrap().contains_key("topic") {
-                    channel_info.topic = Some(chan.1["topic"].as_str().unwrap().to_string());
+                    let topic = chan.1["topic"].as_str().unwrap().to_string();
+                    info!("Found topic for channel {}: {}", chan.0, topic);
+                    channel_info.topic = Some(topic);
                 }
 
                 if chan.1.as_table().unwrap().contains_key("secret") {
@@ -100,6 +102,7 @@ pub fn parse_configured_channels(config_file: &PathBuf) -> Result<FxHashMap<Stri
                     let public = secret.public_key();
                     let msg_box = crypto_box::Box::new(&public, &secret);
                     channel_info.salt_box = Some(msg_box);
+                    info!("Instantiated NaCl box for channel {}", chan.0);
                 }
 
                 ret.insert(chan.0.to_string(), channel_info);