Update nickname in protocol responses
Diff
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(-)
@@ -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",
@@ -48,7 +48,20 @@ impl FrameDecoder for Decoder {
}
}
pub struct Encoder;
pub struct Encoder {
server_name: &'static str,
pub nick: Option<String>,
}
impl Encoder {
#[must_use]
pub fn new(server_name: &'static str) -> Self {
Self {
server_name,
nick: None,
}
}
}
impl tokio_util::codec::Encoder<titanirc_types::ServerMessage<'_>> for Encoder {
type Error = std::io::Error;
@@ -58,7 +71,14 @@ impl tokio_util::codec::Encoder<titanirc_types::ServerMessage<'_>> 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(())
}
@@ -36,12 +36,14 @@ impl CommandHandler<NickCommand<'static>> 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());
@@ -45,7 +45,8 @@ impl Handler<Connection> 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);
ctx.add_stream(read);