Python & parsing IRC messages

tt picture tt · May 30, 2009 · Viewed 9.8k times · Source

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:

:[email protected] PRIVMSG #channel :Hi!

becomes this:

{ "sender" : "[email protected]", "target" : "#channel", "message" : "Hi!" }

And so on?

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

Answer

Unknown picture Unknown · May 30, 2009

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(":[email protected] PRIVMSG #channel :Hi!")
# ('[email protected]', 'PRIVMSG', ['#channel', 'Hi!']) 

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