From 5da45239046c7a408a1db8a66cfb673867b52628 Mon Sep 17 00:00:00 2001 From: gempir Date: Mon, 22 Nov 2021 12:44:01 +0100 Subject: [PATCH] Handle empty action messages (#169) * handle empty action messages * add a test for empty action messages Co-authored-by: Rasmus Karlsson --- message.go | 6 +++++- message_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/message.go b/message.go index f334946..53df828 100644 --- a/message.go +++ b/message.go @@ -232,8 +232,12 @@ func parsePrivateMessage(message *ircMessage) Message { text := privateMessage.Message if strings.HasPrefix(text, "\u0001ACTION") && strings.HasSuffix(text, "\u0001") { - privateMessage.Message = text[8 : len(text)-1] privateMessage.Action = true + if len(text) == 8 { + privateMessage.Message = "" + } else { + privateMessage.Message = text[8 : len(text)-1] + } } privateMessage.Emotes = parseEmotes(message.Tags["emotes"], privateMessage.Message) diff --git a/message_test.go b/message_test.go index fcbaf2b..8cb070e 100644 --- a/message_test.go +++ b/message_test.go @@ -4,6 +4,16 @@ import ( "testing" ) +func TestCanPraseBadActionMessageWithoutPanic(t *testing.T) { + testMessage := "@badges=;color=;display-name=pajlada;emotes=;mod=0;room-id=11148817;subscriber=0;tmi-sent-ts=1522855191000;turbo=0;user-id=11148817;user-type= :pajlada!pajlada@pajlada.tmi.twitch.tv PRIVMSG #pajlada :\u0001ACTION\u0001" + + message := ParseMessage(testMessage) + msg := message.(*PrivateMessage) + + assertStringsEqual(t, "11148817", msg.User.ID) + assertStringsEqual(t, "", msg.Message) +} + func TestCantParseNoTagsMessage(t *testing.T) { testMessage := "my test message"