From 48788fee5f57360b36b13c8ced30913cd292cd36 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Fri, 2 Jan 2015 15:20:26 -0500 Subject: [PATCH] Added get_source_nickname() to Message. --- src/data/message.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/data/message.rs b/src/data/message.rs index 54ed3d5..ab2d83f 100644 --- a/src/data/message.rs +++ b/src/data/message.rs @@ -31,6 +31,12 @@ impl Message { } } + /// Gets the nickname of the message source, if it exists. + #[experimental] + pub fn get_source_nickname(&self) -> Option<&str> { + self.prefix.as_ref().and_then(|s| s.find('!').map(|i| s[..i])) + } + /// Converts a Message into a String according to the IRC protocol. #[experimental] pub fn into_string(&self) -> String { @@ -118,6 +124,17 @@ mod test { assert_eq!(Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("Testing!")), message); } + #[test] + fn get_source_nickname() { + assert_eq!(Message::new(None, "PING", None, None).get_source_nickname(), None); + assert_eq!(Message::new( + Some("irc.test.net"), "PING", None, None + ).get_source_nickname(), None); + assert_eq!(Message::new( + Some("test!test@test"), "PING", None, None + ).get_source_nickname(), Some("test")); + } + #[test] fn into_string() { let message = Message {