eloonstra / twitchypy-irc

A simple Twitch IRC library written in Python.
https://pypi.org/project/twitchypy-irc/
MIT License
1 stars 0 forks source link

Index error when parsing message from some user. #1

Open eivl opened 11 months ago

eivl commented 11 months ago

During testing, I found this error.

Here are a few details.

image

I was able to get the message string from a previous error from the same type as message as the one in the screenshot above.

"@badge-info=founder/102;badges=moderator/1,founder/0;color=#8A2BE2;display-name=SheepFarmer;emotes=;first-msg=0;flags=;id=f9912e9d-474a-4cba-8897-0eb7bb9391e7;mod=1;reply-parent-display-name=hughmyron17;reply-parent-msg-body=!giveaway;reply-parent-msg-id=500ea2a8-f58f-407c-97a4-4c0e3274f374;reply-parent-user-id=133248066;reply-parent-user-login=hughmyron17;reply-thread-parent-msg-id=500ea2a8-f58f-407c-97a4-4c0e3274f374;reply-thread-parent-user-login=hughmyron17;returning-chatter=0;room-id=56649026;subscriber=1;tmi-sent-ts=1696780419533;turbo=0;user-id=69861108;user-type=mod :sheepfarmer!sheepfarmer@sheepfarmer.tmi.twitch.tv PRIVMSG #quin69 :@hughmyron17 For Quins $1,000 giveaway & more, enter here: http://bit.ly/quin69giveaway"

testable example:

>>> message = "@badge-info=founder/102;badges=moderator/1,founder/0;color=#8A2BE2;display-name=SheepFarmer;emotes=;firs
... t-msg=0;flags=;id=f9912e9d-474a-4cba-8897-0eb7bb9391e7;mod=1;reply-parent-display-name=hughmyron17;reply-parent-msg
... -body=!giveaway;reply-parent-msg-id=500ea2a8-f58f-407c-97a4-4c0e3274f374;reply-parent-user-id=133248066;reply-paren
... t-user-login=hughmyron17;reply-thread-parent-msg-id=500ea2a8-f58f-407c-97a4-4c0e3274f374;reply-thread-parent-user-l
... ogin=hughmyron17;returning-chatter=0;room-id=56649026;subscriber=1;tmi-sent-ts=1696780419533;turbo=0;user-id=698611
... 08;user-type=mod :sheepfarmer!sheepfarmer@sheepfarmer.tmi.twitch.tv PRIVMSG #quin69 :@hughmyron17 For Quins $1,000
... giveaway & more, enter here: http://bit.ly/quin69giveaway"
>>>
>>> name = message.split("PRIVMSG")[0].split("!")[0].split(":")[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

>>>

I don't know enough to talk about a solution, maybe indexing and splitting is not the right tool when the data is not predictable. and many selecting negative indexes would work. name = message.split("PRIVMSG")[0].split("!")[-2].split(":")[1] as an example for discussion.

Since the results are unclear, regex may be the right tool for this.

eivl

eloonstra commented 11 months ago

Thanks for reporting the error and contributing to the project, much appreciated. I'll release a fix as soon as possible.

eivl commented 11 months ago

My pleasure.

eivl commented 11 months ago
    badges = {badge.rstrip("/1") for badge in message.split("badges=")[1].split(";")[0].split(",")}
                                              ~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

another split issue.

i dont have the message that broke this. Adding it here.

I could try to rewrite the parser based on the RFC 1459 spec.

eloonstra commented 11 months ago

I'll be adjusting this as soon as possible. Apologies for the issues you're encountering.

eivl commented 11 months ago

no worries, im just glad i did not have to write my own socket and data parser from scratch :D

here is a message that failed in the parser.

m-id=56649026;subscriber=0;tmi-sent-ts=1696789151006;turbo=0;user-id=400074398;user-type= :faintsmlle!faintsmlle@faintsmlle.tmi.twitch.tv PRIVMSG #quin69 :guys stop saying shit about this being quins audio, this is much better
eivl commented 11 months ago

Im doing a full test to see if it breaks more places.

 @classmethod
    def parse(cls, message: str) -> "TwitchPrivateMessage":
        """
        Parse a Twitch private message.
        :param message: The message to parse.
        """
        try:
            badges = {badge.rstrip("/1") for badge in message.split("badges=")[1].split(";")[0].split(",")}
            color = message.split("color=")[1].split(";")[0]
            display_name = message.split("display-name=")[1].split(";")[0]
            first_message = message.split("emotes=")[1].split(";")[0] == "-1"
            message_id = message.split("id=")[1].split(";")[0]
            mod = message.split("mod=")[1].split(";")[0] == "1"
            broadcaster = "broadcaster" in badges
            returning_chatter = message.split("room-id=")[1].split(";")[0] == "1"
            room_id = int(message.split("room-id=")[1].split(";")[0])
            subscriber = message.split("subscriber=")[1].split(";")[0] == "1"
            turbo = message.split("turbo=")[1].split(";")[0] == "1"
            user_id = int(message.split("user-id=")[1].split(";")[0])
            name = message.split("PRIVMSG")[0].split("!")[-2].split(":")[1]
            channel = message.split("PRIVMSG")[1].split(":")[0].strip().lstrip("#")
            message = ":".join(message.split("PRIVMSG")[1].split(":")[1:]).strip()
        except IndexError:
            print()
            print(message)
            print()
            return cls(
                set(),
                "",
                "",
                False,
                "",
                False,
                False,
                False,
                0,
                False,
                False,
                0,
                "",
                "",
                "",
            )
        return cls(
            badges,
            color,
            display_name,
            first_message,
            message_id,
            mod,
            broadcaster,
            returning_chatter,
            room_id,
            subscriber,
            turbo,
            user_id,
            name,
            channel,
            message,
        )

I just wrap it in a try except and return an empty object back.

And here is another one that failed.

82a1-4d44-afc7-7acd1cbe1baa;mod=0;returning-chatter=0;room-id=56649026;subscriber=0;tmi-sent-ts=1696789889991;turbo=0;user-id=30278702;user-type= :anduinl!anduinl@anduinl.tmi.twitch.tv PRIVMSG #quin69 :Aware 2015

I ran this messages through a RFC 1459 tokenizer, and these lines still fails as it does not have the source. makes me think that twitch does not follow the IRC spec like I expect it to.

eivl commented 11 months ago
a97eba093e39423f9c4c2;color=#B22222;display-name=Khazerox;emotes=;first-msg=0;flags=0-3:P.3;id=9e48a829-3f4a-4bae-b9e2-7b30e9fc5acb;mod=0;returning-chatter=0;room-id=56649026;subscriber=1;tmi-sent-ts=1696790043418;turbo=0;user-id=58797167;user-type= :khazerox!khazerox@khazerox.tmi.twitch.tv PRIVMSG #quin69 :C***

and another one.

Should give you a few ideas how to troubleshoot I hope.