Search code examples
pythonparsingirc

Python & parsing IRC messages


What's the best way to parse messages received from an IRC server with Python according to the RFC? I simply want some kind of list/whatever, for example:

:test!~test@test.com PRIVMSG #channel :Hi!

becomes this:

{ "sender" : "test!~test@test.com", "target" : "#channel", "message" : "Hi!" }

And so on?

(Edit: I want to parse IRC messages in general, not just PRIVMSG's)


Solution

  • Look at Twisted's implementation http://twistedmatrix.com/

    Unfortunately I'm out of time, maybe someone else can paste it here for you.

    Edit

    Well I'm back, and strangely no one has pasted it yet so here it is:

    http://twistedmatrix.com/trac/browser/trunk/twisted/words/protocols/irc.py#54

    def parsemsg(s):
        """Breaks a message from an IRC server into its prefix, command, and arguments.
        """
        prefix = ''
        trailing = []
        if not s:
           raise IRCBadMessage("Empty line.")
        if s[0] == ':':
            prefix, s = s[1:].split(' ', 1)
        if s.find(' :') != -1:
            s, trailing = s.split(' :', 1)
            args = s.split()
            args.append(trailing)
        else:
            args = s.split()
        command = args.pop(0)
        return prefix, command, args
    
    parsemsg(":test!~test@test.com PRIVMSG #channel :Hi!")
    # ('test!~test@test.com', 'PRIVMSG', ['#channel', 'Hi!']) 
    

    This function closely follows the EBNF described in the IRC RFC.