From cb1131aef6b342429304ce3e3b92e4b3d700d494 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 8 Apr 2024 22:05:37 +0200 Subject: [PATCH] fix IRC message parser --- app/TwitchBot/IRCMessage.php | 4 +- tests/Unit/TwitchBot/IRCMessageTest.php | 51 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/TwitchBot/IRCMessageTest.php diff --git a/app/TwitchBot/IRCMessage.php b/app/TwitchBot/IRCMessage.php index 89aa023..d6cfaec 100644 --- a/app/TwitchBot/IRCMessage.php +++ b/app/TwitchBot/IRCMessage.php @@ -35,7 +35,7 @@ class IRCMessage { $has_host = false; $prefix = explode(' ', $processed, 2); $processed = $prefix[1]; - $prefix = ltrim($prefix[0], ':'); + $prefix = $prefix[0][0] === ':' ? substr($prefix[0], 1) : $prefix[0]; if (strpos($prefix, '!') !== false) { $has_user = true; } @@ -67,7 +67,7 @@ class IRCMessage { while (strlen($processed)) { if ($processed[0] == ':') { - $msg->params[] = ltrim($processed, ':'); + $msg->params[] = substr($processed, 1); break; } $e = explode(' ', $processed, 2); diff --git a/tests/Unit/TwitchBot/IRCMessageTest.php b/tests/Unit/TwitchBot/IRCMessageTest.php new file mode 100644 index 0000000..e5166b2 --- /dev/null +++ b/tests/Unit/TwitchBot/IRCMessageTest.php @@ -0,0 +1,51 @@ +assertEqualsCanonicalizing([ + 'badge-info' => 'subscriber/7', + 'badges' => 'subscriber/6', + 'client-nonce' => '779fa70677ceb5c85331030333796faf', + 'color' => '#FF4500', + 'display-name' => 'Magnohato', + 'emotes' => 'emotesv2_fb9cdf0d7fae484ab2e1d20e59a17ed2:0-11,19-30,38-49,58-69,80-91,100-111,122-133,139-150', + 'first-msg' => '0', + 'flags' => '', + 'id' => '4b6fc216-18de-41d0-b92d-aeb28c690788', + 'mod' => '0', + 'returning-chattea' => '0', + 'room-id' => '30178469', + 'subscriber' => '1', + 'tmi-sent-ts' => '1712602818970', + 'turbo' => '0', + 'user-id' => '58325205', + 'user-type' => '', + ], $msg->tags); + $this->assertEquals('PRIVMSG', $msg->command); + $this->assertEquals('magnohato', $msg->nick); + $this->assertEquals('muftaaKoomst spamt muftaaKoomst muffy muftaaKoomst diesen muftaaKoomst koomster muftaaKoomst sonst muftaaKoomst lose\'ed muftaaKoomst er muftaaKoomst', $msg->getText()); + + $msg = IRCMessage::fromString('@badge-info=subscriber/11;badges=vip/1,subscriber/6;client-nonce=14ed3e2b44129cd87b4ea21b597007a4;color=#8A2BE2;display-name=faker_jr_;emotes=;first-msg=0;flags=;id=7d37c395-acd7-401a-8d8a-f472f6317ee4;mod=0;reply-parent-display-name=Magnohato;reply-parent-msg-body=mein\sgegner\shatte\ssie\snicht;reply-parent-msg-id=9b33a360-0f71-40dc-af17-2d96c09b8d4a;reply-parent-user-id=58325205;reply-parent-user-login=magnohato;reply-thread-parent-display-name=Magnohato;reply-thread-parent-msg-id=9b33a360-0f71-40dc-af17-2d96c09b8d4a;reply-thread-parent-user-id=58325205;reply-thread-parent-user-login=magnohato;returning-chatter=0;room-id=30178469;subscriber=1;tmi-sent-ts=1712602877661;turbo=0;user-id=706291742;user-type=;vip=1 :faker_jr_!faker_jr_@faker_jr_.tmi.twitch.tv PRIVMSG #muftaay :@Magnohato sumSmash'); + $this->assertEquals('PRIVMSG', $msg->command); + $this->assertEquals('faker_jr_', $msg->nick); + $this->assertEquals('@Magnohato sumSmash', $msg->getText()); + + $msg = IRCMessage::fromString('@badge-info=;badges=;color=;display-name=guentherspinx;emote-only=1;emotes=emotesv2_2ba58e42a974424ab706920026fc2569:0-8;first-msg=0;flags=;id=2dbabd7f-afd1-41cd-ad0e-2d4c5a961a46;mod=0;returning-chatter=0;room-id=30178469;subscriber=0;tmi-sent-ts=1712602890083;turbo=0;user-id=110816394;user-type= :guentherspinx!guentherspinx@guentherspinx.tmi.twitch.tv PRIVMSG #muftaay :muftaaHey'); + $this->assertEquals('PRIVMSG', $msg->command); + $this->assertEquals('guentherspinx', $msg->nick); + $this->assertEquals('muftaaHey', $msg->getText()); + + $msg = IRCMessage::fromString('@badge-info=subscriber/7;badges=subscriber/6;client-nonce=86e4b1cdc575fbec97017840447ba70f;color=#FF4500;display-name=Magnohato;emote-only=1;emotes=555555560:0-1;first-msg=0;flags=;id=6df652f0-299f-4924-bcd9-f006d1b0ff97;mod=0;returning-chatter=0;room-id=30178469;subscriber=1;tmi-sent-ts=1712602892032;turbo=0;user-id=58325205;user-type= :magnohato!magnohato@magnohato.tmi.twitch.tv PRIVMSG #muftaay ::D'); + $this->assertEquals('PRIVMSG', $msg->command); + $this->assertEquals('magnohato', $msg->nick); + $this->assertEquals(':D', $msg->getText()); + } + +} -- 2.39.2