From 5110b04b3ae3b5d902917f269f6cab8107cb13d0 Mon Sep 17 00:00:00 2001 From: Phillip Tennen Date: Sat, 6 Apr 2024 09:34:22 +0100 Subject: [PATCH] Trailing messages may omit the leading colon --- src/irc/response_parser.rs | 71 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/src/irc/response_parser.rs b/src/irc/response_parser.rs index df00a5f..01c134e 100644 --- a/src/irc/response_parser.rs +++ b/src/irc/response_parser.rs @@ -571,7 +571,10 @@ impl ResponseParser { } fn parse_trailing_message(tokenizer: &mut Tokenizer) -> String { - tokenizer.match_str(":"); + // The colon is only included if necessary to disambiguate the message components + if tokenizer.peek() == Some(':') { + tokenizer.match_str(":"); + } tokenizer.read_to_str(IRC_LINE_DELIMITER).expect("Failed to read a message") } @@ -830,7 +833,7 @@ impl ResponseParser { ) }, IrcCommandName::Join => { - let channel = tokenizer.read_to_str(IRC_LINE_DELIMITER).expect("Failed to read a channel name"); + let channel = Self::parse_trailing_message(&mut tokenizer); if channel.contains(" ") { // Only clients can specify multiple channels panic!("Multiple channels mentioned, servers should not send multiple channels?") @@ -1270,6 +1273,23 @@ mod test { ) } + #[test] + fn test_parse_mode2() { + let msg = parse_line(":coulomb.oftc.net MODE #zzzz13 +nt\r\n"); + assert_eq!(msg.origin, Some("coulomb.oftc.net".to_string())); + assert_eq!(msg.command_name, IrcCommandName::Mode); + assert_eq!( + msg.command, + IrcCommand::Mode( + ModeParams::new( + // TODO(PT): This should be a UserOrChannel? + &Nickname("#zzzz13".to_string()), + "+nt" + ) + ) + ) + } + #[test] fn test_parse_ping() { let msg = parse_line("PING :copper.libera.chat\r\n"); @@ -1362,6 +1382,22 @@ mod test { ) } + #[test] + fn test_names2() { + let msg = parse_line(":coulomb.oftc.net 353 phillipt = #zzzz13 :@phillipt\r\n"); + assert_eq!(msg.origin, Some("coulomb.oftc.net".to_string())); + assert_eq!(msg.command_name, IrcCommandName::Names); + assert_eq!( + msg.command, + IrcCommand::Names( + NamesParameters::new( + "#zzzz13".to_string(), + vec![":@phillipt".to_string()], + ) + ) + ) + } + #[test] fn test_end_of_names() { let msg = parse_line(":coulomb.oftc.net 366 phillip-testing2 #edk2 :End of /NAMES list\r\n"); @@ -1378,6 +1414,22 @@ mod test { ) } + #[test] + fn test_end_of_names2() { + let msg = parse_line(":coulomb.oftc.net 366 phillipt #zzzz13 :End of /NAMES list.\r\n"); + assert_eq!(msg.origin, Some("coulomb.oftc.net".to_string())); + assert_eq!(msg.command_name, IrcCommandName::EndOfNames); + assert_eq!( + msg.command, + IrcCommand::EndOfNames( + EndOfNamesParameters::new( + "#zzzz13".to_string(), + "End of /NAMES list.".to_string(), + ) + ) + ) + } + #[test] fn test_topic() { let msg = parse_line(":coulomb.oftc.net 332 phillip-testing2 #edk2 :EDK II/OVMF\r\n"); @@ -1410,4 +1462,19 @@ mod test { ) ) } + + #[test] + fn test_join() { + let msg = parse_line(":phillipt!~phillipt@86.11.226.171 JOIN :#zzzz13\r\n"); + assert_eq!(msg.origin, Some("phillipt!~phillipt@86.11.226.171".to_string())); + assert_eq!(msg.command_name, IrcCommandName::Join); + assert_eq!( + msg.command, + IrcCommand::Join( + JoinParameters::new( + &Channel("#zzzz13".to_string()), + ) + ) + ) + } }