forked from lavina/lavina
1
0
Fork 0
This commit is contained in:
Nikita Vilunov 2023-10-04 17:38:59 +02:00
parent 1373767d7f
commit 16844f5b85
3 changed files with 35 additions and 31 deletions

View File

@ -7,6 +7,7 @@ use nonempty::nonempty;
use nonempty::NonEmpty;
use prometheus::{IntCounter, IntGauge, Registry as MetricsRegistry};
use serde::Deserialize;
use tokio::io::AsyncReadExt;
use tokio::io::{AsyncBufReadExt, AsyncWrite, AsyncWriteExt, BufReader, BufWriter};
use tokio::net::tcp::{ReadHalf, WriteHalf};
use tokio::net::{TcpListener, TcpStream};
@ -96,7 +97,7 @@ async fn handle_registration<'a>(
let mut pass: Option<Str> = None;
let user = loop {
let res = reader.read_until(b'\n', &mut buffer).await;
let res = read_irc_message(reader, &mut buffer).await;
let res = match res {
Ok(len) => {
if len == 0 {
@ -252,7 +253,7 @@ async fn handle_registered_socket<'a>(
loop {
select! {
biased;
len = reader.read_until(b'\n', &mut buffer) => {
len = read_irc_message(reader, &mut buffer) => {
let len = len?;
let len = if len == 0 {
log::info!("EOF, Terminating socket");
@ -291,6 +292,21 @@ async fn handle_registered_socket<'a>(
Ok(())
}
async fn read_irc_message(reader: &mut BufReader<ReadHalf<'_>>, buf: &mut Vec<u8>) -> Result<usize> {
let mut size = 0;
'outer: loop {
let res = reader.read_until(b'\r', buf).await?;
size += res;
let next = reader.read_u8().await?;
buf.push(next);
size += 1;
if next != b'\n' {
continue 'outer;
}
return Ok(size);
}
}
async fn handle_update(
config: &ServerConfig,
user: &RegisteredUser,

View File

@ -1,6 +1,6 @@
use super::*;
use nom::combinator::opt;
use nom::combinator::{all_consuming, opt};
/// Client-to-server command.
#[derive(Clone, Debug, PartialEq, Eq)]
@ -60,8 +60,8 @@ pub enum ClientMessage {
},
}
pub fn client_message(input: &str) -> IResult<&str, ClientMessage> {
alt((
pub fn client_message(input: &str) -> Result<ClientMessage, anyhow::Error> {
let (_, m) = all_consuming(alt((
client_message_capability,
client_message_ping,
client_message_pong,
@ -75,7 +75,8 @@ pub fn client_message(input: &str) -> IResult<&str, ClientMessage> {
client_message_part,
client_message_privmsg,
client_message_quit,
))(input)
)))(input)?;
Ok(m)
}
fn client_message_capability(input: &str) -> IResult<&str, ClientMessage> {
@ -89,24 +90,14 @@ fn client_message_ping(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("PING ")(input)?;
let (input, token) = token(input)?;
Ok((
input,
ClientMessage::Ping {
token: token.into(),
},
))
Ok((input, ClientMessage::Ping { token: token.into() }))
}
fn client_message_pong(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("PONG ")(input)?;
let (input, token) = token(input)?;
Ok((
input,
ClientMessage::Pong {
token: token.into(),
},
))
Ok((input, ClientMessage::Pong { token: token.into() }))
}
fn client_message_nick(input: &str) -> IResult<&str, ClientMessage> {
@ -225,12 +216,7 @@ fn client_message_quit(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("QUIT :")(input)?;
let (input, reason) = token(input)?;
Ok((
input,
ClientMessage::Quit {
reason: reason.into(),
},
))
Ok((input, ClientMessage::Quit { reason: reason.into() }))
}
#[derive(Clone, Debug, PartialEq, Eq)]
@ -292,9 +278,7 @@ mod test {
#[test]
fn test_client_message_ping() {
let input = "PING 1337";
let expected = ClientMessage::Ping {
token: "1337".into(),
};
let expected = ClientMessage::Ping { token: "1337".into() };
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
@ -302,9 +286,7 @@ mod test {
#[test]
fn test_client_message_pong() {
let input = "PONG 1337";
let expected = ClientMessage::Pong {
token: "1337".into(),
};
let expected = ClientMessage::Pong { token: "1337".into() };
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));

View File

@ -104,7 +104,7 @@ mod test {
use assert_matches::*;
use super::*;
use crate::testkit::*;
use crate::{testkit::*, client::client_message};
#[test]
fn test_chan_global() {
@ -153,4 +153,10 @@ mod test {
assert_eq!(bytes.as_slice(), input.as_bytes());
}
#[test]
fn test_aa() {
dbg!(client_message("PING asd"));
panic!()
}
}