forked from lavina/lavina
177 lines
4.6 KiB
Rust
177 lines
4.6 KiB
Rust
|
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 `:`.
|
||
|
pub sender: Option<String>,
|
||
|
pub body: ServerMessageBody,
|
||
|
}
|
||
|
|
||
|
impl ServerMessage {
|
||
|
pub fn write(&self, writer: &mut impl Write) -> std::io::Result<()> {
|
||
|
self.body.write(writer)?;
|
||
|
writer.write(b"\n")?;
|
||
|
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,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
impl ServerMessageBody {
|
||
|
pub fn write(&self, writer: &mut impl Write) -> std::io::Result<()> {
|
||
|
match self {
|
||
|
ServerMessageBody::Notice {
|
||
|
first_target,
|
||
|
rest_targets,
|
||
|
text,
|
||
|
} => {
|
||
|
writer.write(b"NOTICE ")?;
|
||
|
writer.write(&first_target)?;
|
||
|
writer.write(b" :")?;
|
||
|
writer.write(&text)?;
|
||
|
}
|
||
|
ServerMessageBody::Ping { token } => {
|
||
|
writer.write(b"PING ")?;
|
||
|
writer.write(&token)?;
|
||
|
}
|
||
|
ServerMessageBody::Pong { from, token } => {
|
||
|
writer.write(b"PONG ")?;
|
||
|
writer.write(&from)?;
|
||
|
writer.write(b" :")?;
|
||
|
writer.write(&token)?;
|
||
|
}
|
||
|
}
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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::*;
|
||
|
|
||
|
#[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![];
|
||
|
expected.write(&mut bytes).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![];
|
||
|
expected.write(&mut bytes).unwrap();
|
||
|
assert_eq!(bytes, input);
|
||
|
}
|
||
|
}
|