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

307 lines
8.0 KiB
Rust
Raw Normal View History

use super::*;
/// Client-to-server command.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ClientMessage {
/// CAP. Capability-related commands.
Capability {
subcommand: CapabilitySubcommand,
},
/// PING <token>
Ping {
2023-04-13 19:15:48 +00:00
token: Box<str>,
},
/// PONG <token>
Pong {
2023-04-13 19:15:48 +00:00
token: Box<str>,
},
2023-02-13 17:08:37 +00:00
/// NICK <nickname>
Nick {
2023-04-13 19:15:48 +00:00
nickname: Box<str>,
},
2023-02-13 17:08:37 +00:00
/// USER <username> 0 * :<realname>
User {
2023-04-13 19:15:48 +00:00
username: Box<str>,
realname: Box<str>,
},
2023-02-13 17:08:37 +00:00
/// JOIN <chan>
2023-02-12 23:31:16 +00:00
Join(Chan),
/// MODE <target>
2023-02-16 19:37:17 +00:00
Mode {
target: Recipient,
},
/// WHO <target>
Who {
target: Recipient, // aka mask
},
/// TOPIC <chan> :<topic>
Topic {
chan: Chan,
2023-04-13 19:15:48 +00:00
topic: Box<str>,
},
Part {
chan: Chan,
2023-04-13 19:15:48 +00:00
message: Box<str>,
},
2023-02-13 17:08:37 +00:00
/// PRIVMSG <target> :<msg>
PrivateMessage {
recipient: Recipient,
2023-04-13 19:15:48 +00:00
body: Box<str>,
},
2023-02-13 17:08:37 +00:00
/// QUIT :<reason>
Quit {
2023-04-13 19:15:48 +00:00
reason: Box<str>,
},
}
2023-04-13 19:15:48 +00:00
pub fn client_message(input: &str) -> IResult<&str, ClientMessage> {
alt((
client_message_capability,
client_message_ping,
client_message_pong,
client_message_nick,
client_message_user,
2023-02-12 23:31:16 +00:00
client_message_join,
client_message_mode,
client_message_who,
client_message_topic,
client_message_part,
2023-02-13 17:08:37 +00:00
client_message_privmsg,
client_message_quit,
))(input)
}
2023-04-13 19:15:48 +00:00
fn client_message_capability(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("CAP ")(input)?;
let (input, subcommand) = capability_subcommand(input)?;
Ok((input, ClientMessage::Capability { subcommand }))
}
2023-04-13 19:15:48 +00:00
fn client_message_ping(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("PING ")(input)?;
let (input, token) = token(input)?;
Ok((
input,
ClientMessage::Ping {
2023-04-13 19:15:48 +00:00
token: token.into(),
},
))
}
2023-04-13 19:15:48 +00:00
fn client_message_pong(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("PONG ")(input)?;
let (input, token) = token(input)?;
Ok((
input,
ClientMessage::Pong {
2023-04-13 19:15:48 +00:00
token: token.into(),
},
))
}
2023-04-13 19:15:48 +00:00
fn client_message_nick(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("NICK ")(input)?;
let (input, nickname) = receiver(input)?;
Ok((
input,
ClientMessage::Nick {
2023-04-13 19:15:48 +00:00
nickname: nickname.into(),
},
))
}
2023-04-13 19:15:48 +00:00
fn client_message_user(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("USER ")(input)?;
let (input, username) = receiver(input)?;
let (input, _) = tag(" ")(input)?;
2023-02-14 18:28:49 +00:00
let (input, _) = receiver(input)?;
let (input, _) = tag(" ")(input)?;
let (input, _) = receiver(input)?;
let (input, _) = tag(" :")(input)?;
let (input, realname) = token(input)?;
Ok((
input,
ClientMessage::User {
2023-04-13 19:15:48 +00:00
username: username.into(),
realname: realname.into(),
},
))
}
2023-04-13 19:15:48 +00:00
fn client_message_join(input: &str) -> IResult<&str, ClientMessage> {
2023-02-12 23:31:16 +00:00
let (input, _) = tag("JOIN ")(input)?;
let (input, chan) = chan(input)?;
Ok((input, ClientMessage::Join(chan)))
}
2023-04-13 19:15:48 +00:00
fn client_message_mode(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("MODE ")(input)?;
2023-02-16 19:37:17 +00:00
let (input, target) = recipient(input)?;
2023-02-16 21:49:17 +00:00
Ok((input, ClientMessage::Mode { target }))
}
2023-04-13 19:15:48 +00:00
fn client_message_who(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("WHO ")(input)?;
let (input, target) = recipient(input)?;
Ok((input, ClientMessage::Who { target }))
}
2023-04-13 19:15:48 +00:00
fn client_message_topic(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("TOPIC ")(input)?;
let (input, chan) = chan(input)?;
2023-04-13 19:15:48 +00:00
let (input, _) = tag(" :")(input)?;
let (input, topic) = token(input)?;
2023-04-13 19:15:48 +00:00
let topic = topic.into();
Ok((input, ClientMessage::Topic { chan, topic }))
}
2023-04-13 19:15:48 +00:00
fn client_message_part(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("PART ")(input)?;
let (input, chan) = chan(input)?;
2023-04-13 19:15:48 +00:00
let (input, _) = tag(" :")(input)?;
let (input, message) = token(input)?;
2023-04-13 19:15:48 +00:00
let message = message.into();
Ok((input, ClientMessage::Part { chan, message }))
}
2023-04-13 19:15:48 +00:00
fn client_message_privmsg(input: &str) -> IResult<&str, ClientMessage> {
2023-02-13 17:08:37 +00:00
let (input, _) = tag("PRIVMSG ")(input)?;
let (input, recipient) = recipient(input)?;
let (input, _) = tag(" :")(input)?;
let (input, body) = token(input)?;
2023-04-13 19:15:48 +00:00
let body = body.into();
2023-02-13 17:08:37 +00:00
Ok((input, ClientMessage::PrivateMessage { recipient, body }))
}
2023-04-13 19:15:48 +00:00
fn client_message_quit(input: &str) -> IResult<&str, ClientMessage> {
let (input, _) = tag("QUIT :")(input)?;
let (input, reason) = token(input)?;
Ok((
input,
ClientMessage::Quit {
2023-04-13 19:15:48 +00:00
reason: reason.into(),
},
))
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CapabilitySubcommand {
/// CAP LS {code}
List { code: [u8; 3] },
/// CAP END
End,
}
2023-04-13 19:15:48 +00:00
fn capability_subcommand(input: &str) -> IResult<&str, CapabilitySubcommand> {
alt((capability_subcommand_ls, capability_subcommand_end))(input)
}
2023-04-13 19:15:48 +00:00
fn capability_subcommand_ls(input: &str) -> IResult<&str, CapabilitySubcommand> {
let (input, _) = tag("LS ")(input)?;
let (input, code) = take(3usize)(input)?;
Ok((
input,
CapabilitySubcommand::List {
2023-04-13 19:15:48 +00:00
code: code.as_bytes().try_into().unwrap(),
},
))
}
2023-04-13 19:15:48 +00:00
fn capability_subcommand_end(input: &str) -> IResult<&str, CapabilitySubcommand> {
let (input, _) = tag("END")(input)?;
Ok((input, CapabilitySubcommand::End))
}
#[cfg(test)]
mod test {
use assert_matches::*;
use super::*;
#[test]
fn test_client_message_cap_ls() {
2023-04-13 19:15:48 +00:00
let input = "CAP LS 302";
let expected = ClientMessage::Capability {
subcommand: CapabilitySubcommand::List { code: *b"302" },
};
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
}
#[test]
fn test_client_message_cap_end() {
2023-04-13 19:15:48 +00:00
let input = "CAP END";
let expected = ClientMessage::Capability {
subcommand: CapabilitySubcommand::End,
};
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
}
#[test]
fn test_client_message_ping() {
2023-04-13 19:15:48 +00:00
let input = "PING 1337";
let expected = ClientMessage::Ping {
2023-04-13 19:15:48 +00:00
token: "1337".into(),
};
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
}
#[test]
fn test_client_message_pong() {
2023-04-13 19:15:48 +00:00
let input = "PONG 1337";
let expected = ClientMessage::Pong {
2023-04-13 19:15:48 +00:00
token: "1337".into(),
};
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
}
#[test]
fn test_client_message_nick() {
2023-04-13 19:15:48 +00:00
let input = "NICK SomeNick";
let expected = ClientMessage::Nick {
2023-04-13 19:15:48 +00:00
nickname: "SomeNick".into(),
};
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
}
#[test]
fn test_client_message_user() {
2023-04-13 19:15:48 +00:00
let input = "USER SomeNick 8 * :Real Name";
let expected = ClientMessage::User {
2023-04-13 19:15:48 +00:00
username: "SomeNick".into(),
realname: "Real Name".into(),
};
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
}
#[test]
fn test_client_message_part() {
2023-04-13 19:15:48 +00:00
let input = "PART #chan :Pokasiki !!!";
let expected = ClientMessage::Part {
2023-04-13 19:15:48 +00:00
chan: Chan::Global("chan".into()),
message: "Pokasiki !!!".into(),
};
let result = client_message(input);
assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result));
}
}