forked from lavina/lavina
1
0
Fork 0

- use Str since it is type alias to Arc

- refactored code
This commit is contained in:
homycdev 2024-04-23 21:13:38 +03:00
parent a1c1b3dae6
commit 37fbe989b0
6 changed files with 34 additions and 37 deletions

View File

@ -1,6 +1,6 @@
use lavina_core::prelude::Str; use lavina_core::prelude::Str;
use lavina_core::repo::Storage; use lavina_core::repo::Storage;
use proto_irc::response::SendResponseBody; use proto_irc::response::SendResponse;
use std::future::Future; use std::future::Future;
use tokio::io::AsyncWrite; use tokio::io::AsyncWrite;
@ -9,9 +9,9 @@ pub mod whois;
pub trait Handler { pub trait Handler {
fn handle( fn handle(
&self, &self,
server_name: &Str, server_name: Str,
client: &Str, client: Str,
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
storage: &mut Storage, storage: Storage,
) -> impl Future<Output = anyhow::Result<()>>; ) -> impl Future<Output = anyhow::Result<()>>;
} }

View File

@ -1,7 +1,7 @@
use tokio::io::{AsyncWrite, AsyncWriteExt}; use tokio::io::{AsyncWrite, AsyncWriteExt};
use lavina_core::prelude::Str; use lavina_core::prelude::Str;
use proto_irc::response::SendResponseBody; use proto_irc::response::SendResponse;
/// ERR_NOSUCHNICK (401) /// ERR_NOSUCHNICK (401)
pub struct ErrNoSuchNick401 { pub struct ErrNoSuchNick401 {
@ -33,7 +33,7 @@ impl ErrNoNicknameGiven431 {
} }
} }
impl SendResponseBody for ErrNoSuchNick401 { impl SendResponse for ErrNoSuchNick401 {
async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
writer.write_all(b"401 ").await?; writer.write_all(b"401 ").await?;
writer.write_all(self.client.as_bytes()).await?; writer.write_all(self.client.as_bytes()).await?;
@ -45,7 +45,7 @@ impl SendResponseBody for ErrNoSuchNick401 {
} }
} }
impl SendResponseBody for ErrNoNicknameGiven431 { impl SendResponse for ErrNoNicknameGiven431 {
async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
writer.write_all(b"431").await?; writer.write_all(b"431").await?;
writer.write_all(self.client.as_bytes()).await?; writer.write_all(self.client.as_bytes()).await?;
@ -55,7 +55,7 @@ impl SendResponseBody for ErrNoNicknameGiven431 {
} }
} }
impl SendResponseBody for ErrNoSuchServer402 { impl SendResponse for ErrNoSuchServer402 {
async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
writer.write_all(b"402 ").await?; writer.write_all(b"402 ").await?;
writer.write_all(self.client.as_bytes()).await?; writer.write_all(self.client.as_bytes()).await?;

View File

@ -4,7 +4,7 @@ use tracing::instrument::WithSubscriber;
use lavina_core::prelude::Str; use lavina_core::prelude::Str;
use lavina_core::repo::Storage; use lavina_core::repo::Storage;
use proto_irc::client::command_args::Whois; use proto_irc::client::command_args::Whois;
use proto_irc::response::{IrcResponseMessage, SendResponseBody, SendResponseMessage}; use proto_irc::response::{IrcResponseMessage, SendResponse};
use crate::commands::whois::error::{ErrNoNicknameGiven431, ErrNoSuchNick401}; use crate::commands::whois::error::{ErrNoNicknameGiven431, ErrNoSuchNick401};
use crate::commands::whois::response::{RplWhoIsUser311, RPL_ENDOFWHOIS_318}; use crate::commands::whois::response::{RplWhoIsUser311, RPL_ENDOFWHOIS_318};
@ -16,15 +16,17 @@ pub mod response;
impl Handler for Whois { impl Handler for Whois {
async fn handle( async fn handle(
&self, &self,
server_name: &Str, server_name: Str,
client: &Str, client: Str,
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
storage: &mut Storage, mut storage: Storage,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
match self { match self {
Whois::Nick(nick) => handle_nick_target(nick, None, server_name, client, writer, storage).await?, Whois::Nick(nick) => {
handle_nick_target(nick.clone(), None, server_name, client, writer, storage.clone()).await?
}
Whois::TargetNick(target, nick) => { Whois::TargetNick(target, nick) => {
handle_nick_target(nick, Some(target), server_name, client, writer, storage).await? handle_nick_target(nick.clone(), Some(target.clone()), server_name, client, writer, storage).await?
} }
Whois::EmptyArgs => { Whois::EmptyArgs => {
IrcResponseMessage::empty_tags( IrcResponseMessage::empty_tags(
@ -40,15 +42,15 @@ impl Handler for Whois {
} }
async fn handle_nick_target( async fn handle_nick_target(
nick: &Str, nick: Str,
// todo: implement logic with target // todo: implement logic with target
_target: Option<&Str>, _target: Option<Str>,
server_name: &Str, server_name: Str,
client: &Str, client: Str,
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
storage: &mut Storage, mut storage: Storage,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
if let Some(user) = storage.retrieve_user_by_name(nick).await? { if let Some(user) = storage.retrieve_user_by_name(nick.clone().as_ref()).await? {
IrcResponseMessage::empty_tags( IrcResponseMessage::empty_tags(
Some(server_name.clone()), Some(server_name.clone()),
RplWhoIsUser311::new( RplWhoIsUser311::new(

View File

@ -1,7 +1,7 @@
use tokio::io::{AsyncWrite, AsyncWriteExt}; use tokio::io::{AsyncWrite, AsyncWriteExt};
use lavina_core::prelude::Str; use lavina_core::prelude::Str;
use proto_irc::response::SendResponseBody; use proto_irc::response::SendResponse;
struct RplWhoisCertfp276; struct RplWhoisCertfp276;
struct RplWhoisRegNick307; struct RplWhoisRegNick307;
@ -47,7 +47,7 @@ impl RPL_ENDOFWHOIS_318 {
} }
} }
impl SendResponseBody for RplWhoIsUser311 { impl SendResponse for RplWhoIsUser311 {
async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
writer.write_all(b"311 ").await?; writer.write_all(b"311 ").await?;
writer.write_all(self.client.as_bytes()).await?; writer.write_all(self.client.as_bytes()).await?;
@ -70,7 +70,7 @@ impl SendResponseBody for RplWhoIsUser311 {
} }
} }
impl SendResponseBody for RPL_ENDOFWHOIS_318 { impl SendResponse for RPL_ENDOFWHOIS_318 {
async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
writer.write_all(b"318 ").await?; writer.write_all(b"318 ").await?;
writer.write_all(self.client.as_bytes()).await?; writer.write_all(self.client.as_bytes()).await?;

View File

@ -469,7 +469,7 @@ async fn handle_registered_socket<'a>(
len len
}; };
let incoming = std::str::from_utf8(&buffer[0..len-2])?; let incoming = std::str::from_utf8(&buffer[0..len-2])?;
if let HandleResult::Leave = handle_incoming_message(incoming, &config, &user, &rooms, &mut connection, writer, storage).await? { if let HandleResult::Leave = handle_incoming_message(incoming, &config, &user, &rooms, &mut connection, writer, storage.clone()).await? {
break; break;
} }
buffer.clear(); buffer.clear();
@ -618,7 +618,7 @@ async fn handle_incoming_message(
rooms: &RoomRegistry, rooms: &RoomRegistry,
user_handle: &mut PlayerConnection, user_handle: &mut PlayerConnection,
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
storage: &mut Storage, mut storage: Storage,
) -> Result<HandleResult> { ) -> Result<HandleResult> {
log::debug!("Incoming raw IRC message: '{buffer}'"); log::debug!("Incoming raw IRC message: '{buffer}'");
let parsed = client_message(buffer); let parsed = client_message(buffer);
@ -730,14 +730,13 @@ async fn handle_incoming_message(
}, },
ClientMessage::Whois { arg } => { ClientMessage::Whois { arg } => {
arg.handle( arg.handle(
&config.server_name, config.server_name.clone(),
&user.nickname, user.nickname.clone(),
writer, writer,
storage, storage.clone(),
) )
.await? .await?
} }
ClientMessage::Mode { target } => { ClientMessage::Mode { target } => {
match target { match target {
Recipient::Nick(nickname) => { Recipient::Nick(nickname) => {

View File

@ -5,17 +5,13 @@ use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::prelude::Str; use crate::prelude::Str;
use crate::Tag; use crate::Tag;
pub trait SendResponseBody { pub trait SendResponse {
fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> impl Future<Output = std::io::Result<()>>;
}
pub trait SendResponseMessage<Message> {
fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> impl Future<Output = std::io::Result<()>>; fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> impl Future<Output = std::io::Result<()>>;
} }
/// Server-to-client enum agnostic message /// Server-to-client enum agnostic message
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct IrcResponseMessage<T: SendResponseBody> { pub struct IrcResponseMessage<T> {
/// Optional tags section, prefixed with `@` /// Optional tags section, prefixed with `@`
pub tags: Vec<Tag>, pub tags: Vec<Tag>,
/// Optional server name, prefixed with `:`. /// Optional server name, prefixed with `:`.
@ -23,7 +19,7 @@ pub struct IrcResponseMessage<T: SendResponseBody> {
pub body: T, pub body: T,
} }
impl<T: SendResponseBody> IrcResponseMessage<T> { impl<T> IrcResponseMessage<T> {
pub fn empty_tags(sender: Option<Str>, body: T) -> Self { pub fn empty_tags(sender: Option<Str>, body: T) -> Self {
IrcResponseMessage { IrcResponseMessage {
tags: vec![], tags: vec![],
@ -37,7 +33,7 @@ impl<T: SendResponseBody> IrcResponseMessage<T> {
} }
} }
impl<T: SendResponseBody> SendResponseMessage<IrcResponseMessage<T>> for IrcResponseMessage<T> { impl<T: SendResponse> SendResponse for IrcResponseMessage<T> {
async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { async fn write_response(self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
if let Some(sender) = &self.sender { if let Some(sender) = &self.sender {
writer.write_all(b":").await?; writer.write_all(b":").await?;