fix recent messages by handling valueless and escaped irc tags

The recent-messages API now serializes empty IRCv3 tags as bare keys
(e.g. `emotes`) instead of `emotes=`, which threw a RangeError in the
tag parser and silently dropped every recent message. Parse tags per the
IRCv3 spec: treat bare and empty values as equivalent, split on the first
`=` so values may contain `=`, and unescape values in a single pass
(\: \s \ \r \n). Also guard user extraction against prefixes without
`!`, and isolate per-message parse failures in _handleIRCData so one bad
message can no longer abort an entire batch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tommy Chow 2026-06-08 21:53:53 -07:00
parent 2b81f2cb31
commit c27a37d52f
4 changed files with 144 additions and 20 deletions

View file

@ -9,6 +9,19 @@ const basicPrivmsg =
'tmi-sent-ts=1234567890123;turbo=0;user-id=67890;user-type= '
':testuser!testuser@testuser.tmi.twitch.tv PRIVMSG #channel :Hello World';
/// Exercises the full IRCv3 tag escape set plus a value that itself contains
/// '='. Uses a raw string so the backslashes are literal wire bytes.
const messageWithEscapedTagChars =
r'@a-space=one\stwo;a-semi=left\:right;a-slash=a\\b;a-equals=k=v;'
r'a-double=x\\sy;display-name=EscapeAll;mod=0 '
r':escapeall!escapeall@escapeall.tmi.twitch.tv PRIVMSG #channel :hi';
/// A message whose prefix is not the usual `nick!user@host` form (no '!').
/// The parser should fall back to the login tag instead of throwing.
const messageNoBangPrefix =
'@login=fallbackuser;mod=0;id=nobang-1 '
':some.server.example PRIVMSG #channel :server style';
/// A chat message with emotes in the tags
/// Note: emote positions are 0-indexed byte positions in the message
/// "Kappa LUL" - Kappa is at 0-4, LUL is at 6-8
@ -138,6 +151,16 @@ const historicalMessage =
'tmi-sent-ts=1234567890123;turbo=0;user-id=12121;user-type= '
':histuser!histuser@histuser.tmi.twitch.tv PRIVMSG #channel :Historical message';
/// A historical message as serialized by the recent-messages API, which emits
/// empty tags as bare keys (e.g. `emotes`, `flags`) instead of the `key=` form
/// that live Twitch IRC uses.
const historicalMessageBareTags =
'@badges=raging-wolf-helm/1;emotes;first-msg=0;tmi-sent-ts=1234567890123;'
'flags;mod=0;returning-chatter=0;rm-received-ts=1234567890456;color=#1E90FF;'
'id=hist-bare-123;room-id=12345;historical=1;user-type;turbo=0;subscriber=0;'
'badge-info;display-name=BareUser;user-id=18181 '
':bareuser!bareuser@bareuser.tmi.twitch.tv PRIVMSG #channel :Bare tag message';
/// A message from a shared chat session (source room different from current)
const sharedChatMessage =
'@badge-info=;badges=;color=#00BFFF;display-name=SharedUser;emotes=;'

View file

@ -27,6 +27,27 @@ void main() {
expect(msg.message, 'test message');
});
test('unescapes the full IRCv3 tag escape set', () {
final msg = IRCMessage.fromString(messageWithEscapedTagChars);
expect(msg.tags['a-space'], 'one two');
expect(msg.tags['a-semi'], 'left;right');
expect(msg.tags['a-slash'], r'a\b');
// Only the first '=' splits key/value; later '=' stays in the value.
expect(msg.tags['a-equals'], 'k=v');
// `\\` consumes both backslashes; the following 's' is a literal letter,
// not a `\s` space escape (single-pass unescaping).
expect(msg.tags['a-double'], r'x\sy');
});
test('falls back to login tag when prefix has no "!"', () {
// An unexpected prefix shape must not throw and drop the message.
final msg = IRCMessage.fromString(messageNoBangPrefix);
expect(msg.user, 'fallbackuser');
expect(msg.message, 'server style');
});
test('handles message with no color tag', () {
final msg = IRCMessage.fromString(messageNoColor);
@ -223,6 +244,20 @@ void main() {
expect(msg.tags['historical'], '1');
});
test('parses historical message with bare (valueless) tags', () {
// The recent-messages API serializes empty tags as bare keys (e.g.
// `emotes`, `flags`) instead of `key=`. This previously threw a
// RangeError, silently dropping all recent messages.
final msg = IRCMessage.fromString(historicalMessageBareTags);
expect(msg.tags['historical'], '1');
expect(msg.tags['display-name'], 'BareUser');
expect(msg.message, 'Bare tag message');
// Bare tags should be absent from the map, not crash parsing.
expect(msg.tags.containsKey('emotes'), isFalse);
expect(msg.tags.containsKey('flags'), isFalse);
});
test('parses shared chat message with source room', () {
final msg = IRCMessage.fromString(sharedChatMessage);