rust-irc/irc-proto/src/message.rs

569 lines
18 KiB
Rust
Raw Normal View History

//! A module providing a data structure for messages to and from IRC servers.
use std::borrow::ToOwned;
2018-09-17 13:52:22 +02:00
use std::fmt::{Display, Formatter, Result as FmtResult, Write};
use std::str::FromStr;
2019-10-16 03:12:29 +02:00
use crate::chan::ChannelExt;
use crate::command::Command;
use crate::error;
use crate::error::{MessageParseError, ProtocolError};
use crate::prefix::Prefix;
2018-09-18 07:03:22 +02:00
/// A data structure representing an IRC message according to the protocol specification. It
/// consists of a collection of IRCv3 tags, a prefix (describing the source of the message), and
/// the protocol command. If the command is unknown, it is treated as a special raw command that
/// consists of a collection of arguments and the special suffix argument. Otherwise, the command
2024-03-19 00:21:31 +01:00
/// is parsed into a more useful form as described in [`Command`].
2015-02-03 19:11:33 +01:00
#[derive(Clone, PartialEq, Debug)]
2014-11-02 23:25:45 +01:00
pub struct Message {
/// Message tags as defined by [IRCv3.2](http://ircv3.net/specs/core/message-tags-3.2.html).
/// These tags are used to add extended information to the given message, and are commonly used
/// in IRCv3 extensions to the IRC protocol.
pub tags: Option<Vec<Tag>>,
/// The message prefix (or source) as defined by [RFC 2812](http://tools.ietf.org/html/rfc2812).
2018-09-17 13:52:22 +02:00
pub prefix: Option<Prefix>,
/// The IRC command, parsed according to the known specifications. The command itself and its
/// arguments (including the special suffix argument) are captured in this component.
pub command: Command,
2014-11-02 23:25:45 +01:00
}
impl Message {
/// Creates a new message from the given components.
///
/// # Example
/// ```
2018-03-10 15:59:29 +01:00
/// # extern crate irc_proto;
/// # use irc_proto::Message;
/// # fn main() {
/// let message = Message::new(
2020-01-31 01:12:44 +01:00
/// Some("nickname!username@hostname"), "JOIN", vec!["#channel"]
/// ).unwrap();
/// # }
/// ```
2017-06-19 19:59:26 +02:00
pub fn new(
prefix: Option<&str>,
command: &str,
args: Vec<&str>,
) -> Result<Message, MessageParseError> {
2020-01-31 01:12:44 +01:00
Message::with_tags(None, prefix, command, args)
}
/// Creates a new IRCv3.2 message from the given components, including message tags. These tags
/// are used to add extended information to the given message, and are commonly used in IRCv3
/// extensions to the IRC protocol.
2017-06-19 19:59:26 +02:00
pub fn with_tags(
tags: Option<Vec<Tag>>,
prefix: Option<&str>,
command: &str,
args: Vec<&str>,
) -> Result<Message, error::MessageParseError> {
Ok(Message {
tags,
2018-09-17 13:52:22 +02:00
prefix: prefix.map(|p| p.into()),
2020-01-31 01:12:44 +01:00
command: Command::new(command, args)?,
})
}
2015-06-22 18:03:57 +02:00
/// Gets the nickname of the message source, if it exists.
///
/// # Example
/// ```
2018-03-10 15:59:29 +01:00
/// # extern crate irc_proto;
/// # use irc_proto::Message;
/// # fn main() {
/// let message = Message::new(
2020-01-31 01:12:44 +01:00
/// Some("nickname!username@hostname"), "JOIN", vec!["#channel"]
/// ).unwrap();
/// assert_eq!(message.source_nickname(), Some("nickname"));
/// # }
/// ```
pub fn source_nickname(&self) -> Option<&str> {
2016-02-07 09:01:05 +01:00
// <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
// <servername> ::= <host>
2018-09-17 13:52:22 +02:00
self.prefix.as_ref().and_then(|p| match p {
Prefix::Nickname(name, _, _) => Some(&name[..]),
2019-08-27 15:05:51 +02:00
_ => None,
2017-06-19 19:59:26 +02:00
})
}
/// Gets the likely intended place to respond to this message.
/// If the type of the message is a `PRIVMSG` or `NOTICE` and the message is sent to a channel,
/// the result will be that channel. In all other cases, this will call `source_nickname`.
///
/// # Example
/// ```
2018-03-10 15:59:29 +01:00
/// # extern crate irc_proto;
/// # use irc_proto::Message;
/// # fn main() {
/// let msg1 = Message::new(
2020-01-31 01:12:44 +01:00
/// Some("ada"), "PRIVMSG", vec!["#channel", "Hi, everyone!"]
/// ).unwrap();
/// assert_eq!(msg1.response_target(), Some("#channel"));
/// let msg2 = Message::new(
2020-01-31 01:12:44 +01:00
/// Some("ada"), "PRIVMSG", vec!["betsy", "betsy: hi"]
/// ).unwrap();
/// assert_eq!(msg2.response_target(), Some("ada"));
/// # }
/// ```
pub fn response_target(&self) -> Option<&str> {
match self.command {
Command::PRIVMSG(ref target, _) if target.is_channel_name() => Some(target),
Command::NOTICE(ref target, _) if target.is_channel_name() => Some(target),
2019-08-27 15:05:51 +02:00
_ => self.source_nickname(),
}
}
2014-11-02 23:25:45 +01:00
}
impl From<Command> for Message {
fn from(cmd: Command) -> Message {
2017-06-19 19:59:26 +02:00
Message {
tags: None,
prefix: None,
command: cmd,
}
}
}
impl FromStr for Message {
2018-03-10 15:46:49 +01:00
type Err = ProtocolError;
2017-06-20 20:54:06 +02:00
fn from_str(s: &str) -> Result<Message, Self::Err> {
2017-06-19 19:59:26 +02:00
if s.is_empty() {
2018-03-10 15:46:49 +01:00
return Err(ProtocolError::InvalidMessage {
string: s.to_owned(),
cause: MessageParseError::EmptyMessage,
2019-08-27 15:05:51 +02:00
});
2017-06-19 19:59:26 +02:00
}
let mut state = s;
let tags = if state.starts_with('@') {
let tags = state.find(' ').map(|i| &state[1..i]);
2017-06-19 19:59:26 +02:00
state = state.find(' ').map_or("", |i| &state[i + 1..]);
tags.map(|ts| {
ts.split(';')
.filter(|s| !s.is_empty())
.map(|s: &str| {
let mut iter = s.splitn(2, '=');
let (fst, snd) = (iter.next(), iter.next());
let snd = snd.map(unescape_tag_value);
Tag(fst.unwrap_or("").to_owned(), snd)
2017-06-19 19:59:26 +02:00
})
.collect::<Vec<_>>()
})
} else {
None
};
let prefix = if state.starts_with(':') {
2015-01-09 23:38:46 +01:00
let prefix = state.find(' ').map(|i| &state[1..i]);
2017-06-19 19:59:26 +02:00
state = state.find(' ').map_or("", |i| &state[i + 1..]);
prefix
} else {
None
};
let line_ending_len = if state.ends_with("\r\n") {
"\r\n"
} else if state.ends_with('\r') {
"\r"
} else if state.ends_with('\n') {
"\n"
} else {
""
2019-08-27 15:05:51 +02:00
}
.len();
let suffix = if state.contains(" :") {
2019-08-27 15:05:51 +02:00
let suffix = state
.find(" :")
.map(|i| &state[i + 2..state.len() - line_ending_len]);
2017-06-19 19:59:26 +02:00
state = state.find(" :").map_or("", |i| &state[..i + 1]);
suffix
} else {
state = &state[..state.len() - line_ending_len];
None
};
2015-01-09 23:38:46 +01:00
let command = match state.find(' ').map(|i| &state[..i]) {
Some(cmd) => {
2017-06-19 19:59:26 +02:00
state = state.find(' ').map_or("", |i| &state[i + 1..]);
cmd
}
// If there's no arguments but the "command" starts with colon, it's not a command.
2019-08-27 15:05:51 +02:00
None if state.starts_with(':') => {
return Err(ProtocolError::InvalidMessage {
string: s.to_owned(),
cause: MessageParseError::InvalidCommand,
})
}
// If there's no arguments following the command, the rest of the state is the command.
None => {
let cmd = state;
state = "";
cmd
2019-08-27 15:05:51 +02:00
}
};
2020-01-31 01:12:44 +01:00
let mut args: Vec<_> = state.splitn(14, ' ').filter(|s| !s.is_empty()).collect();
if let Some(suffix) = suffix {
args.push(suffix);
}
Message::with_tags(tags, prefix, command, args).map_err(|e| ProtocolError::InvalidMessage {
string: s.to_owned(),
cause: e,
})
}
}
2015-04-26 06:01:33 +02:00
impl<'a> From<&'a str> for Message {
fn from(s: &'a str) -> Message {
s.parse().unwrap()
}
}
2016-03-18 02:39:58 +01:00
impl Display for Message {
/// Converts a Message into a String according to the IRC protocol.
///
/// # Example
/// ```
/// # extern crate irc_proto;
/// # use irc_proto::Message;
/// # fn main() {
/// let msg = Message::new(
/// Some("ada"), "PRIVMSG", vec!["#channel", "Hi, everyone!"]
/// ).unwrap();
/// assert_eq!(msg.to_string(), ":ada PRIVMSG #channel :Hi, everyone!\r\n");
/// # }
/// ```
2016-03-18 02:39:58 +01:00
fn fmt(&self, f: &mut Formatter) -> FmtResult {
if let Some(ref tags) = self.tags {
f.write_char('@')?;
for (i, tag) in tags.iter().enumerate() {
if i > 0 {
f.write_char(';')?;
}
f.write_str(&tag.0)?;
if let Some(ref value) = tag.1 {
f.write_char('=')?;
escape_tag_value(f, value)?;
}
}
f.write_char(' ')?;
}
if let Some(ref prefix) = self.prefix {
write!(f, ":{} ", prefix)?
}
write!(f, "{}\r\n", String::from(&self.command))
2016-03-18 02:39:58 +01:00
}
}
/// A message tag as defined by [IRCv3.2](http://ircv3.net/specs/core/message-tags-3.2.html).
/// It consists of a tag key, and an optional value for the tag. Each message can contain a number
/// of tags (in the string format, they are separated by semicolons). Tags are used to add extended
/// information to a message under IRCv3.
#[derive(Clone, PartialEq, Debug)]
2016-06-19 06:37:58 +02:00
pub struct Tag(pub String, pub Option<String>);
fn escape_tag_value(f: &mut dyn Write, value: &str) -> FmtResult {
for c in value.chars() {
match c {
';' => f.write_str("\\:")?,
' ' => f.write_str("\\s")?,
'\\' => f.write_str("\\\\")?,
'\r' => f.write_str("\\r")?,
'\n' => f.write_str("\\n")?,
c => f.write_char(c)?,
}
}
Ok(())
}
fn unescape_tag_value(value: &str) -> String {
let mut unescaped = String::with_capacity(value.len());
let mut iter = value.chars();
while let Some(c) = iter.next() {
2021-11-14 20:20:49 +01:00
let r = if c == '\\' {
match iter.next() {
2021-11-14 20:20:49 +01:00
Some(':') => ';',
Some('s') => ' ',
Some('\\') => '\\',
Some('r') => '\r',
Some('n') => '\n',
Some(c) => c,
None => break,
}
} else {
2021-11-14 20:20:49 +01:00
c
};
unescaped.push(r);
}
unescaped
}
#[cfg(test)]
mod test {
use super::{Message, Tag};
2019-10-16 03:12:29 +02:00
use crate::command::Command::{Raw, PRIVMSG, QUIT};
#[test]
fn new() {
let message = Message {
tags: None,
prefix: None,
command: PRIVMSG(format!("test"), format!("Testing!")),
};
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(None, "PRIVMSG", vec!["test", "Testing!"]).unwrap(),
2017-06-19 19:59:26 +02:00
message
)
}
#[test]
fn source_nickname() {
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(None, "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
None
);
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(Some("irc.test.net"), "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
None
);
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(Some("test!test@test"), "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
Some("test")
);
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(Some("test@test"), "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
Some("test")
);
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(Some("test!test@irc.test.com"), "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
Some("test")
);
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(Some("test!test@127.0.0.1"), "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
Some("test")
);
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(Some("test@test.com"), "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
Some("test")
);
2017-06-19 19:59:26 +02:00
assert_eq!(
2020-01-31 01:12:44 +01:00
Message::new(Some("test"), "PING", vec!["data"])
2017-06-19 19:59:26 +02:00
.unwrap()
.source_nickname(),
Some("test")
);
}
#[test]
fn to_string() {
let message = Message {
tags: None,
prefix: None,
command: PRIVMSG(format!("test"), format!("Testing!")),
};
2020-01-31 01:12:44 +01:00
assert_eq!(&message.to_string()[..], "PRIVMSG test Testing!\r\n");
let message = Message {
tags: None,
2018-09-17 13:52:22 +02:00
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Still testing!")),
};
2017-06-19 19:59:26 +02:00
assert_eq!(
&message.to_string()[..],
":test!test@test PRIVMSG test :Still testing!\r\n"
);
}
#[test]
fn from_string() {
let message = Message {
tags: None,
prefix: None,
command: PRIVMSG(format!("test"), format!("Testing!")),
};
assert_eq!(
"PRIVMSG test :Testing!\r\n".parse::<Message>().unwrap(),
message
);
let message = Message {
tags: None,
2018-09-17 13:52:22 +02:00
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Still testing!")),
};
2017-06-19 19:59:26 +02:00
assert_eq!(
":test!test@test PRIVMSG test :Still testing!\r\n"
.parse::<Message>()
.unwrap(),
2017-06-20 20:54:06 +02:00
message
2017-06-19 19:59:26 +02:00
);
let message = Message {
2017-06-19 19:59:26 +02:00
tags: Some(vec![
Tag(format!("aaa"), Some(format!("bbb"))),
Tag(format!("ccc"), None),
Tag(format!("example.com/ddd"), Some(format!("eee"))),
]),
2018-09-17 13:52:22 +02:00
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Testing with tags!")),
};
2017-06-19 19:59:26 +02:00
assert_eq!(
"@aaa=bbb;ccc;example.com/ddd=eee :test!test@test PRIVMSG test :Testing with \
2019-08-27 15:05:51 +02:00
tags!\r\n"
.parse::<Message>()
.unwrap(),
2017-06-20 20:54:06 +02:00
message
2017-06-19 19:59:26 +02:00
)
}
#[test]
fn from_string_atypical_endings() {
let message = Message {
tags: None,
prefix: None,
command: PRIVMSG(format!("test"), format!("Testing!")),
};
assert_eq!(
"PRIVMSG test :Testing!\r".parse::<Message>().unwrap(),
message
);
assert_eq!(
"PRIVMSG test :Testing!\n".parse::<Message>().unwrap(),
message
);
assert_eq!(
"PRIVMSG test :Testing!".parse::<Message>().unwrap(),
message
);
}
#[test]
fn from_and_to_string() {
2019-08-27 15:05:51 +02:00
let message =
"@aaa=bbb;ccc;example.com/ddd=eee :test!test@test PRIVMSG test :Testing with \
tags!\r\n";
assert_eq!(message.parse::<Message>().unwrap().to_string(), message);
}
#[test]
fn to_message() {
let message = Message {
tags: None,
prefix: None,
command: PRIVMSG(format!("test"), format!("Testing!")),
};
2015-04-26 06:11:51 +02:00
let msg: Message = "PRIVMSG test :Testing!\r\n".into();
assert_eq!(msg, message);
let message = Message {
tags: None,
2018-09-17 13:52:22 +02:00
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Still testing!")),
};
2015-04-26 06:11:51 +02:00
let msg: Message = ":test!test@test PRIVMSG test :Still testing!\r\n".into();
assert_eq!(msg, message);
}
#[test]
fn to_message_with_colon_in_arg() {
// Apparently, UnrealIRCd (and perhaps some others) send some messages that include
// colons within individual parameters. So, let's make sure it parses correctly.
let message = Message {
tags: None,
2018-09-17 13:52:22 +02:00
prefix: Some("test!test@test".into()),
command: Raw(
2017-06-19 19:59:26 +02:00
format!("COMMAND"),
2020-01-31 01:12:44 +01:00
vec![format!("ARG:test"), format!("Testing!")],
),
};
2015-04-26 06:11:51 +02:00
let msg: Message = ":test!test@test COMMAND ARG:test :Testing!\r\n".into();
assert_eq!(msg, message);
}
#[test]
fn to_message_no_prefix_no_args() {
let message = Message {
tags: None,
prefix: None,
command: QUIT(None),
};
let msg: Message = "QUIT\r\n".into();
assert_eq!(msg, message);
}
#[test]
2015-03-22 04:08:41 +01:00
#[should_panic]
fn to_message_invalid_format() {
2015-04-26 06:11:51 +02:00
let _: Message = ":invalid :message".into();
}
#[test]
fn to_message_tags_escapes() {
let msg = "@tag=\\:\\s\\\\\\r\\n\\a\\ :test PRIVMSG #test :test\r\n"
.parse::<Message>()
.unwrap();
let message = Message {
tags: Some(vec![Tag("tag".to_string(), Some("; \\\r\na".to_string()))]),
prefix: Some("test".into()),
command: PRIVMSG("#test".to_string(), "test".to_string()),
};
assert_eq!(msg, message);
}
#[test]
fn to_string_tags_escapes() {
let msg = Message {
tags: Some(vec![Tag("tag".to_string(), Some("; \\\r\na".to_string()))]),
prefix: Some("test".into()),
command: PRIVMSG("#test".to_string(), "test".to_string()),
}
.to_string();
2020-01-31 23:11:28 +01:00
let message = "@tag=\\:\\s\\\\\\r\\na :test PRIVMSG #test test\r\n";
assert_eq!(msg, message);
}
#[test]
fn to_message_with_colon_in_suffix() {
2023-04-12 16:18:34 +02:00
let msg = "PRIVMSG #test ::test".parse::<Message>().unwrap();
let message = Message {
tags: None,
prefix: None,
2023-04-12 16:18:34 +02:00
command: PRIVMSG("#test".to_string(), ":test".to_string()),
};
assert_eq!(msg, message);
}
#[test]
fn to_string_with_colon_in_suffix() {
let msg = Message {
tags: None,
prefix: None,
command: PRIVMSG("#test".to_string(), ":test".to_string()),
}
.to_string();
let message = "PRIVMSG #test ::test\r\n";
assert_eq!(msg, message);
}
}