Преглед изворни кода

ircd: implement NAMES command

ghassmo пре 4 година
родитељ
комит
87d745852c
3 измењених фајлова са 38 додато и 3 уклоњено
  1. 7 2
      bin/ircd/src/main.rs
  2. 28 0
      bin/ircd/src/server.rs
  3. 3 1
      bin/ircd/src/settings.rs

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

@@ -99,7 +99,7 @@ impl Ircd {
 
 
                             // Try to potentially decrypt the incoming message.
                             // Try to potentially decrypt the incoming message.
                             if conn.configured_chans.contains_key(&msg.channel) {
                             if conn.configured_chans.contains_key(&msg.channel) {
-                                let chan_info = conn.configured_chans.get(&msg.channel).unwrap();
+                                let chan_info = conn.configured_chans.get_mut(&msg.channel).unwrap();
                                 if !chan_info.joined {
                                 if !chan_info.joined {
                                     continue
                                     continue
                                 }
                                 }
@@ -116,8 +116,13 @@ impl Ircd {
 
 
                                     msg.message = decrypted_msg.unwrap();
                                     msg.message = decrypted_msg.unwrap();
                                     info!("Decrypted received message: {:?}", msg);
                                     info!("Decrypted received message: {:?}", msg);
+
                                 }
                                 }
 
 
+                                // add the nickname to the channel's names
+                                if !chan_info.names.contains(&msg.nickname) {
+                                    chan_info.names.push(msg.nickname.clone());
+                                }
                             }
                             }
 
 
                             conn.reply(&msg.to_irc_msg()).await?;
                             conn.reply(&msg.to_irc_msg()).await?;
@@ -144,7 +149,7 @@ impl Ircd {
                     };
                     };
                 }
                 }
             })
             })
-            .detach();
+        .detach();
 
 
         Ok(())
         Ok(())
     }
     }

+ 28 - 0
bin/ircd/src/server.rs

@@ -15,6 +15,7 @@ use crate::{
 
 
 const RPL_NOTOPIC: u32 = 331;
 const RPL_NOTOPIC: u32 = 331;
 const RPL_TOPIC: u32 = 332;
 const RPL_TOPIC: u32 = 332;
+const RPL_NAMEREPLY: u32 = 353;
 
 
 pub struct IrcServerConnection {
 pub struct IrcServerConnection {
     // server stream
     // server stream
@@ -70,6 +71,33 @@ impl IrcServerConnection {
                 // Ignore it for now.
                 // Ignore it for now.
                 self.is_user_init = true;
                 self.is_user_init = true;
             }
             }
+            "NAMES" => {
+                let channels = tokens.next().ok_or(Error::MalformedPacket)?;
+                for chan in channels.split(',') {
+                    if !chan.starts_with('#') {
+                        warn!("{} is not a valid name for channel", chan);
+                        continue
+                    }
+
+                    if self.configured_chans.contains_key(chan) {
+                        let chan_info = self.configured_chans.get(chan).unwrap();
+
+                        if chan_info.names.is_empty() {
+                            continue
+                        }
+
+                        let names_reply = format!(
+                            ":{}!anon@dark.fi {} = {} : {}\r\n",
+                            self.nickname,
+                            RPL_NAMEREPLY,
+                            chan,
+                            chan_info.names.join(" ")
+                        );
+
+                        self.reply(&names_reply).await?;
+                    }
+                }
+            }
             "NICK" => {
             "NICK" => {
                 let nickname = tokens.next().ok_or(Error::MalformedPacket)?;
                 let nickname = tokens.next().ok_or(Error::MalformedPacket)?;
                 self.is_nick_init = true;
                 self.is_nick_init = true;

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

@@ -67,11 +67,13 @@ pub struct ChannelInfo {
     pub salt_box: Option<crypto_box::Box>,
     pub salt_box: Option<crypto_box::Box>,
     /// Flag indicates whether the user has joined the channel or not
     /// Flag indicates whether the user has joined the channel or not
     pub joined: bool,
     pub joined: bool,
+    /// All nicknames which are visible on the channel
+    pub names: Vec<String>,
 }
 }
 
 
 impl ChannelInfo {
 impl ChannelInfo {
     pub fn new() -> Result<Self> {
     pub fn new() -> Result<Self> {
-        Ok(Self { topic: None, salt_box: None, joined: true })
+        Ok(Self { topic: None, salt_box: None, joined: true, names: vec![] })
     }
     }
 }
 }