Skip to content

Commit

Permalink
work around Twitch emote indices bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RAnders00 committed Aug 20, 2020
1 parent 94d9893 commit 6195c31
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/message/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,9 @@ impl IRCMessageParseExt for IRCMessage {
.take(code_length)
.collect::<String>();

// range specified in the emotes tag was out of range for the message text string
if code.chars().count() != code_length {
return Err(make_error());
}
// we intentionally gracefully handle indices that are out of bounds for the
// given string by taking as much as possible until the end of the string.
// This is to work around a Twitch bug: https://github.com/twitchdev/issues/issues/104

emotes.push(Emote {
id: emote_id.to_owned(),
Expand Down
44 changes: 43 additions & 1 deletion src/message/commands/privmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,49 @@ mod tests {
fn test_message_with_bits() {
let src = "@badge-info=;badges=bits/100;bits=1;color=#004B49;display-name=TETYYS;emotes=;flags=;id=d7f03a35-f339-41ca-b4d4-7c0721438570;mod=0;room-id=11148817;subscriber=0;tmi-sent-ts=1594571566672;turbo=0;user-id=36175310;user-type= :tetyys!tetyys@tetyys.tmi.twitch.tv PRIVMSG #pajlada :trihard1";
let irc_message = IRCMessage::parse(src).unwrap();
let msg = PrivmsgMessage::try_from(irc_message.clone()).unwrap();
let msg = PrivmsgMessage::try_from(irc_message).unwrap();
assert_eq!(msg.bits, Some(1));
}

#[test]
fn test_incorrect_emote_index() {
// emote index off by one.
let src = r"@badge-info=subscriber/3;badges=subscriber/3;color=#0000FF;display-name=Linkoping;emotes=25:41-45;flags=17-26:S.6;id=744f9c58-b180-4f46-bd9e-b515b5ef75c1;mod=0;room-id=188442366;subscriber=1;tmi-sent-ts=1566335866017;turbo=0;user-id=91673457;user-type= :linkoping!linkoping@linkoping.tmi.twitch.tv PRIVMSG #queenqarro :Då kan du begära skadestånd och förtal Kappa";
let irc_message = IRCMessage::parse(src).unwrap();
let msg = PrivmsgMessage::try_from(irc_message).unwrap();

assert_eq!(
msg.emotes,
vec![Emote {
id: "25".to_owned(),
char_range: 41..45,
code: "ppa".to_owned(),
}]
);
assert_eq!(
msg.message_text,
"Я не такой красивый. Не урод, но до тебя далеко LUL"
);
}

#[test]
fn test_extremely_incorrect_emote_index() {
// emote index off by more than 1
let src = r"@badge-info=;badges=;color=;display-name=some_1_happy;emotes=425618:49-51;flags=24-28:A.3;id=9eb37414-0952-44cc-b177-ad8007088034;mod=0;room-id=35768443;subscriber=0;tmi-sent-ts=1597921035256;turbo=0;user-id=473035780;user-type= :some_1_happy!some_1_happy@some_1_happy.tmi.twitch.tv PRIVMSG #mocbka34 :Я не такой красивый. Не урод, но до тебя далеко LUL";
let irc_message = IRCMessage::parse(src).unwrap();
let msg = PrivmsgMessage::try_from(irc_message).unwrap();

assert_eq!(
msg.emotes,
vec![Emote {
id: "425618".to_owned(),
char_range: 49..52,
code: "UL".to_owned(),
}]
);
assert_eq!(
msg.message_text,
"Я не такой красивый. Не урод, но до тебя далеко LUL"
);
}
}
5 changes: 5 additions & 0 deletions src/message/twitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub struct Emote {
///
/// As is documented on `Range`, the `start` index of this range is inclusive, while the
/// `end` index is exclusive.
///
/// This is always the exact range of characters that Twitch originally sent.
/// Note that due to [a Twitch bug](https://github.com/twitchdev/issues/issues/104)
/// (that this library intentionally works around), the character range specified here
/// might be out-of-bounds for the original message text string.
pub char_range: Range<usize>,
/// This is the text that this emote replaces, e.g. `Kappa` or `:)`.
pub code: String,
Expand Down

0 comments on commit 6195c31

Please sign in to comment.