Merge pull request #41 from angelsl/robustness

User idents and hostnames actually can have dots
This commit is contained in:
Aaron Weiss 2016-02-07 11:29:21 -05:00
commit b0ecd397ad

View file

@ -34,12 +34,14 @@ impl Message {
/// Gets the nickname of the message source, if it exists.
pub fn source_nickname(&self) -> Option<&str> {
// <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
// <servername> ::= <host>
self.prefix.as_ref().and_then(|s|
match (s.find('!'), s.find('@'), s.find('.')) {
(_, _, Some(_)) => None,
(Some(i), _, None) => Some(&s[..i]),
(None, Some(i), None) => Some(&s[..i]),
(None, None, None) => Some(&s)
(Some(i), _, _) => Some(&s[..i]), // nick!user
(None, Some(i), _) => Some(&s[..i]), // nick@host
(None, None, None) => Some(&s), // nick
_ => None // server.name
}
)
}
@ -141,15 +143,31 @@ mod test {
assert_eq!(Message::new(
None, "PING", vec![], Some("data")
).unwrap().source_nickname(), None);
assert_eq!(Message::new(
Some("irc.test.net"), "PING", vec![], Some("data")
).unwrap().source_nickname(), None);
assert_eq!(Message::new(
Some("test!test@test"), "PING", vec![], Some("data")
).unwrap().source_nickname(), Some("test"));
assert_eq!(Message::new(
Some("test@test"), "PING", vec![], Some("data")
).unwrap().source_nickname(), Some("test"));
assert_eq!(Message::new(
Some("test!test@awe.did.you.know.irc.hostnames.have.dots"), "PING", vec![], Some("data")
).unwrap().source_nickname(), Some("test"));
assert_eq!(Message::new(
Some("test!test@127.0.0.1"), "PING", vec![], Some("data")
).unwrap().source_nickname(), Some("test"));
assert_eq!(Message::new(
Some("test@test.com"), "PING", vec![], Some("data")
).unwrap().source_nickname(), Some("test"));
assert_eq!(Message::new(
Some("test"), "PING", vec![], Some("data")
).unwrap().source_nickname(), Some("test"));