forked from lavina/lavina
1
0
Fork 0
lavina/src/protos/irc/server.rs

420 lines
13 KiB
Rust
Raw Normal View History

use tokio::io::AsyncWrite;
use tokio::io::AsyncWriteExt;
use super::*;
/// Server-to-client message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ServerMessage {
/// Optional tags section, prefixed with `@`
pub tags: Vec<Tag>,
/// Optional server name, prefixed with `:`.
2023-02-10 18:47:58 +00:00
pub sender: Option<ByteVec>,
pub body: ServerMessageBody,
}
impl ServerMessage {
pub async fn write_async(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
2023-02-10 18:47:58 +00:00
match &self.sender {
Some(ref sender) => {
writer.write_all(b":").await?;
writer.write_all(sender.as_slice()).await?;
writer.write_all(b" ").await?;
2023-02-10 18:47:58 +00:00
}
None => {}
}
self.body.write_async(writer).await?;
writer.write_all(b"\n").await?;
Ok(())
}
}
pub fn server_message(input: &[u8]) -> IResult<&[u8], ServerMessage> {
let (input, command) = server_message_body(input)?;
let (input, _) = tag(b"\n")(input)?;
let message = ServerMessage {
tags: vec![],
sender: None,
body: command,
};
Ok((input, message))
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ServerMessageBody {
Notice {
first_target: ByteVec,
rest_targets: Vec<ByteVec>,
text: ByteVec,
},
Ping {
token: ByteVec,
},
Pong {
from: ByteVec,
token: ByteVec,
},
2023-02-13 17:08:37 +00:00
PrivateMessage {
target: Recipient,
body: ByteVec,
},
2023-02-13 18:32:52 +00:00
Join(Chan),
2023-02-15 17:54:48 +00:00
Part(Chan),
2023-02-16 23:38:34 +00:00
Error {
reason: ByteVec,
},
2023-02-10 18:47:58 +00:00
N001Welcome {
client: ByteVec,
text: ByteVec,
},
N002YourHost {
client: ByteVec,
text: ByteVec,
},
N003Created {
client: ByteVec,
text: ByteVec,
},
N004MyInfo {
client: ByteVec,
hostname: ByteVec,
softname: ByteVec,
// TODO user modes, channel modes, channel modes with a parameter
},
N005ISupport {
client: ByteVec,
params: ByteVec, // TODO make this a datatype
},
2023-02-16 19:37:17 +00:00
/// Reply to a client's [Mode](crate::protos::irc::client::ClientMessage::Mode) request.
N221UserModeIs {
client: ByteVec,
modes: ByteVec,
},
/// Final reply to a client's [Who](crate::protos::irc::client::ClientMessage::Who) request.
N315EndOfWho {
client: ByteVec,
mask: Recipient,
/// Usually `b"End of WHO list"`
msg: ByteVec,
},
2023-02-13 18:32:52 +00:00
N332Topic {
client: ByteVec,
chat: Chan,
topic: ByteVec,
},
/// A reply to a client's [Who](crate::protos::irc::client::ClientMessage::Who) request.
N352WhoReply {
client: ByteVec,
// chan = *
username: ByteVec,
/// User's hostname
host: ByteVec,
/// Hostname of the server the user is connected to
server: ByteVec,
2023-02-16 18:33:36 +00:00
nickname: ByteVec,
/// Flags
flags: AwayStatus,
hops: u8,
realname: ByteVec,
},
2023-02-13 18:32:52 +00:00
N353NamesReply {
client: ByteVec,
chan: Chan,
members: ByteVec, // TODO make this a non-empty list with prefixes
},
N366NamesReplyEnd {
client: ByteVec,
chan: Chan,
},
2023-02-16 21:49:17 +00:00
N474BannedFromChan {
client: ByteVec,
chan: Chan,
message: ByteVec,
},
}
impl ServerMessageBody {
pub async fn write_async(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> {
match self {
ServerMessageBody::Notice {
first_target,
rest_targets,
text,
} => {
writer.write_all(b"NOTICE ").await?;
writer.write_all(&first_target).await?;
writer.write_all(b" :").await?;
writer.write_all(&text).await?;
}
ServerMessageBody::Ping { token } => {
writer.write_all(b"PING ").await?;
writer.write_all(&token).await?;
}
ServerMessageBody::Pong { from, token } => {
writer.write_all(b"PONG ").await?;
writer.write_all(&from).await?;
writer.write_all(b" :").await?;
writer.write_all(&token).await?;
}
2023-02-13 17:08:37 +00:00
ServerMessageBody::PrivateMessage { target, body } => {
writer.write_all(b"PRIVMSG ").await?;
target.write_async(writer).await?;
writer.write_all(b" :").await?;
writer.write_all(&body).await?;
}
2023-02-13 18:32:52 +00:00
ServerMessageBody::Join(chan) => {
writer.write_all(b"JOIN ").await?;
chan.write_async(writer).await?;
}
2023-02-15 17:54:48 +00:00
ServerMessageBody::Part(chan) => {
writer.write_all(b"PART ").await?;
chan.write_async(writer).await?;
}
2023-02-16 23:38:34 +00:00
ServerMessageBody::Error { reason } => {
writer.write_all(b"ERROR :").await?;
writer.write_all(&reason).await?;
}
2023-02-10 18:47:58 +00:00
ServerMessageBody::N001Welcome { client, text } => {
writer.write_all(b"001 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" :").await?;
writer.write_all(text).await?;
2023-02-10 18:47:58 +00:00
}
ServerMessageBody::N002YourHost { client, text } => {
writer.write_all(b"002 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" :").await?;
writer.write_all(text).await?;
2023-02-10 18:47:58 +00:00
}
ServerMessageBody::N003Created { client, text } => {
writer.write_all(b"003 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" :").await?;
writer.write_all(text).await?;
2023-02-10 18:47:58 +00:00
}
ServerMessageBody::N004MyInfo {
client,
hostname,
softname,
} => {
writer.write_all(b"004 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" ").await?;
writer.write_all(&hostname).await?;
writer.write_all(b" ").await?;
writer.write_all(&softname).await?;
2023-02-16 21:49:17 +00:00
writer.write_all(b" r CFILPQbcefgijklmnopqrstvz").await?;
2023-02-10 18:47:58 +00:00
// TODO remove hardcoded modes
}
ServerMessageBody::N005ISupport { client, params } => {
writer.write_all(b"005 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" ").await?;
writer.write_all(&params).await?;
writer.write_all(b" :are supported by this server").await?;
2023-02-10 18:47:58 +00:00
}
2023-02-16 19:37:17 +00:00
ServerMessageBody::N221UserModeIs { client, modes } => {
writer.write_all(b"221 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" ").await?;
writer.write_all(&modes).await?;
2023-02-16 21:49:17 +00:00
}
ServerMessageBody::N315EndOfWho { client, mask, msg } => {
writer.write_all(b"315 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" ").await?;
mask.write_async(writer).await?;
writer.write_all(b" :").await?;
writer.write_all(&msg).await?;
}
2023-02-13 18:32:52 +00:00
ServerMessageBody::N332Topic {
client,
chat,
topic,
} => {
writer.write_all(b"332 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" ").await?;
chat.write_async(writer).await?;
writer.write_all(b" :").await?;
writer.write_all(&topic).await?;
}
ServerMessageBody::N352WhoReply {
client,
username,
host,
server,
flags,
2023-02-16 18:33:36 +00:00
nickname,
hops,
realname,
} => {
writer.write_all(b"352 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" * ").await?;
writer.write_all(&username).await?;
writer.write_all(b" ").await?;
writer.write_all(&host).await?;
writer.write_all(b" ").await?;
writer.write_all(&server).await?;
writer.write_all(b" ").await?;
match flags {
AwayStatus::Here => writer.write_all(b"H").await?,
AwayStatus::Gone => writer.write_all(b"G").await?,
}
2023-02-16 18:33:36 +00:00
writer.write_all(b" ").await?;
writer.write_all(&nickname).await?;
writer.write_all(b" :").await?;
writer.write_all(hops.to_string().as_bytes()).await?;
writer.write_all(b" ").await?;
writer.write_all(&realname).await?;
}
2023-02-13 18:32:52 +00:00
ServerMessageBody::N353NamesReply {
client,
chan,
members,
} => {
writer.write_all(b"353 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" = ").await?;
chan.write_async(writer).await?;
writer.write_all(b" :").await?;
writer.write_all(&members).await?;
}
ServerMessageBody::N366NamesReplyEnd { client, chan } => {
writer.write_all(b"366 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" ").await?;
chan.write_async(writer).await?;
writer.write_all(b" :End of /NAMES list").await?;
}
2023-02-16 21:49:17 +00:00
ServerMessageBody::N474BannedFromChan {
client,
chan,
message,
} => {
writer.write_all(b"474 ").await?;
writer.write_all(&client).await?;
writer.write_all(b" ").await?;
chan.write_async(writer).await?;
writer.write_all(b" :").await?;
writer.write_all(&message).await?;
}
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AwayStatus {
Here,
Gone,
}
fn server_message_body(input: &[u8]) -> IResult<&[u8], ServerMessageBody> {
alt((
server_message_body_notice,
server_message_body_ping,
server_message_body_pong,
))(input)
}
fn server_message_body_notice(input: &[u8]) -> IResult<&[u8], ServerMessageBody> {
let (input, _) = tag("NOTICE ")(input)?;
let (input, first_target) = receiver(input)?;
let (input, _) = tag(" :")(input)?;
let (input, text) = token(input)?;
let first_target = first_target.to_owned();
let text = text.to_owned();
Ok((
input,
ServerMessageBody::Notice {
first_target,
rest_targets: vec![],
text,
},
))
}
fn server_message_body_ping(input: &[u8]) -> IResult<&[u8], ServerMessageBody> {
let (input, _) = tag("PING ")(input)?;
let (input, token) = token(input)?;
Ok((
input,
ServerMessageBody::Ping {
token: token.to_owned(),
},
))
}
fn server_message_body_pong(input: &[u8]) -> IResult<&[u8], ServerMessageBody> {
let (input, _) = tag("PONG ")(input)?;
let (input, from) = receiver(input)?;
let (input, _) = tag(" :")(input)?;
let (input, token) = token(input)?;
Ok((
input,
ServerMessageBody::Pong {
from: from.to_owned(),
token: token.to_owned(),
},
))
}
#[cfg(test)]
mod test {
use assert_matches::*;
use super::*;
2023-02-12 12:15:31 +00:00
use crate::util::testkit::*;
#[test]
fn test_server_message_notice() {
let input = b"NOTICE * :*** Looking up your hostname...\n";
let expected = ServerMessage {
tags: vec![],
sender: None,
body: ServerMessageBody::Notice {
first_target: b"*".to_vec(),
rest_targets: vec![],
text: b"*** Looking up your hostname...".to_vec(),
},
};
let result = server_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
let mut bytes = vec![];
2023-02-12 12:15:31 +00:00
sync_future(expected.write_async(&mut bytes))
.unwrap()
.unwrap();
assert_eq!(bytes, input);
}
#[test]
fn test_server_message_pong() {
let input = b"PONG server.example :LAG004911\n";
let expected = ServerMessage {
tags: vec![],
sender: None,
body: ServerMessageBody::Pong {
from: b"server.example".to_vec(),
token: b"LAG004911".to_vec(),
},
};
let result = server_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
let mut bytes = vec![];
2023-02-12 12:15:31 +00:00
sync_future(expected.write_async(&mut bytes))
.unwrap()
.unwrap();
assert_eq!(bytes, input);
}
}