From 3aa1d3e74b1215be8255c037528d62a4395127f4 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Mon, 1 Feb 2021 02:27:39 +0000 Subject: [PATCH] Update nickname in protocol responses --- Cargo.lock | 2 +- titanirc-codec/src/wire.rs | 24 ++++++++++++++++++++++-- titanirc-server/src/entities/user/commands.rs | 4 +++- titanirc-server/src/server.rs | 3 ++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5d7673..c95ee74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,7 +3,7 @@ [[package]] name = "actix" version = "0.11.0-beta.1" -source = "git+https://github.com/JordanForks/actix#6c04d4eab0b9bf883e85a9b5659ecdc00a609bc4" +source = "git+https://github.com/JordanForks/actix#8799836f986644629bce700c1c17c3f13b0055ba" dependencies = [ "actix-macros 0.1.3", "actix-rt", diff --git a/titanirc-codec/src/wire.rs b/titanirc-codec/src/wire.rs index 0db4539..6da1ea9 100644 --- a/titanirc-codec/src/wire.rs +++ b/titanirc-codec/src/wire.rs @@ -48,7 +48,20 @@ impl FrameDecoder for Decoder { } } -pub struct Encoder; +pub struct Encoder { + server_name: &'static str, + pub nick: Option, +} + +impl Encoder { + #[must_use] + pub fn new(server_name: &'static str) -> Self { + Self { + server_name, + nick: None, + } + } +} impl tokio_util::codec::Encoder> for Encoder { type Error = std::io::Error; @@ -58,7 +71,14 @@ impl tokio_util::codec::Encoder> for Encoder { item: titanirc_types::ServerMessage, dst: &mut BytesMut, ) -> Result<(), Self::Error> { - item.write("my.cool.server", "jordan", dst); + item.write( + &self.server_name, + match &self.nick { + Some(v) => v, + None => "*", + }, + dst, + ); dst.extend_from_slice(b"\r\n"); Ok(()) } diff --git a/titanirc-server/src/entities/user/commands.rs b/titanirc-server/src/entities/user/commands.rs index d8c437e..c2a82fb 100644 --- a/titanirc-server/src/entities/user/commands.rs +++ b/titanirc-server/src/entities/user/commands.rs @@ -36,12 +36,14 @@ impl CommandHandler> for super::User { NickCommand { nick, .. }: NickCommand<'static>, _ctx: &mut Self::Context, ) { + self.nick = Some(std::str::from_utf8(&nick.0[..]).unwrap().to_string()); + (*self.writer.encoder_mut()).nick = self.nick.clone(); + self.writer.write(titanirc_types::Reply::RplWelcome.into()); self.writer.write(titanirc_types::Reply::RplYourHost.into()); self.writer.write(titanirc_types::Reply::RplCreated.into()); self.writer.write(titanirc_types::Reply::RplMyInfo.into()); self.writer.write(titanirc_types::Reply::RplISupport.into()); - self.nick = Some(std::str::from_utf8(&nick.0[..]).unwrap().to_string()); // LUSERS // RPL_UMODEIS // MOTD diff --git a/titanirc-server/src/server.rs b/titanirc-server/src/server.rs index 6a2cf04..e97d74e 100644 --- a/titanirc-server/src/server.rs +++ b/titanirc-server/src/server.rs @@ -45,7 +45,8 @@ impl Handler for Server { User::create(move |ctx| { let (read, write) = tokio::io::split(stream); let read = FramedRead::new(read, titanirc_codec::Decoder); - let write = FramedWrite::new(write, titanirc_codec::Encoder, ctx); + let write = + FramedWrite::new(write, titanirc_codec::Encoder::new("my.cool.server"), ctx); // Make our new `User` handle all events from this socket in `StreamHandler>`. ctx.add_stream(read); -- libgit2 1.7.2