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

View File

@ -1,7 +1,7 @@
use tokio::io::{AsyncWrite, AsyncWriteExt};
use lavina_core::prelude::Str;
use proto_irc::response::SendResponseBody;
use proto_irc::response::SendResponse;
/// ERR_NOSUCHNICK (401)
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<()> {
writer.write_all(b"401 ").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<()> {
writer.write_all(b"431").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<()> {
writer.write_all(b"402 ").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::repo::Storage;
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::response::{RplWhoIsUser311, RPL_ENDOFWHOIS_318};
@ -16,15 +16,17 @@ pub mod response;
impl Handler for Whois {
async fn handle(
&self,
server_name: &Str,
client: &Str,
server_name: Str,
client: Str,
writer: &mut (impl AsyncWrite + Unpin),
storage: &mut Storage,
mut storage: Storage,
) -> anyhow::Result<()> {
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) => {
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 => {
IrcResponseMessage::empty_tags(
@ -40,15 +42,15 @@ impl Handler for Whois {
}
async fn handle_nick_target(
nick: &Str,
nick: Str,
// todo: implement logic with target
_target: Option<&Str>,
server_name: &Str,
client: &Str,
_target: Option<Str>,
server_name: Str,
client: Str,
writer: &mut (impl AsyncWrite + Unpin),
storage: &mut Storage,
mut storage: Storage,
) -> 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(
Some(server_name.clone()),
RplWhoIsUser311::new(

View File

@ -1,7 +1,7 @@
use tokio::io::{AsyncWrite, AsyncWriteExt};
use lavina_core::prelude::Str;
use proto_irc::response::SendResponseBody;
use proto_irc::response::SendResponse;
struct RplWhoisCertfp276;
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<()> {
writer.write_all(b"311 ").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<()> {
writer.write_all(b"318 ").await?;
writer.write_all(self.client.as_bytes()).await?;

View File

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

View File

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