Skip to content

Commit

Permalink
Trailing messages may omit the leading colon
Browse files Browse the repository at this point in the history
  • Loading branch information
codyd51 committed Apr 6, 2024
1 parent c303b49 commit 5110b04
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions src/irc/response_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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?")
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -1410,4 +1462,19 @@ mod test {
)
)
}

#[test]
fn test_join() {
let msg = parse_line(":[email protected] JOIN :#zzzz13\r\n");
assert_eq!(msg.origin, Some("[email protected]".to_string()));
assert_eq!(msg.command_name, IrcCommandName::Join);
assert_eq!(
msg.command,
IrcCommand::Join(
JoinParameters::new(
&Channel("#zzzz13".to_string()),
)
)
)
}
}

0 comments on commit 5110b04

Please sign in to comment.