Python & parsing IRC messages
Asked Answered
H

5

8

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)

Hawks answered 30/5, 2009 at 21:51 Comment(0)
T
22

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.

Twelvetone answered 30/5, 2009 at 21:58 Comment(2)
@earthmeLon, which is updated by RFC2812.Iceland
Note that this works conveniently for valid input (nice!), but also works for invalid input (e.g. incorrect prefix values, such as ':@server CMD params'), and makes no effort to validate full formatting scope per tools.ietf.org/html/rfc2812#section-2.3.1Secretin
A
1

You can do it with a simple list comprehension if the format is always like this.

keys = ['sender', 'type', 'target', 'message']
s = ":[email protected] PRIVMSG #channel :Hi!"
dict((key, value.lstrip(':')) for key, value in zip(keys, s.split()))

Result:

{'message': 'Hi!', 'type': 'PRIVMSG', 'sender': '[email protected]', 'target': '#channel'}
Aetna answered 30/5, 2009 at 22:26 Comment(1)
Messages do not always follow this format, and can have greater or fewer parts. RFC1459 2.3Pulverable
A
0

Do you just want to parse IRC Messages in general or do you want just parse PRIVMSGs? However I have a implementation for that.

def parse_message(s):
    prefix   = ''
    trailing = ''
    if s.startswith(':'):
        prefix, s = s[1:].split(' ', 1)
    if ' :' in s:
        s, trailing = s.split(' :', 1)
    args = s.split()
    return prefix, args.pop(0), args, trailing
Anthiathia answered 30/5, 2009 at 22:44 Comment(2)
Why not use list comprehension? map(lambda a:a.lstrip(':'), s.split())Aetna
Prefix and trailing is optional. The trailing may contain spaces and a message can have a couple of parameters. btw. map has nothing to do with a list comprehension which would look like [part.lstrip(';') for part in s.split()]Anthiathia
F
0

If you want to keep to a low-level hacking I second the Twisted answer by Unknown, but first I think you should take a look at the very recently announced Yardbird which is a nice request parsing layer on top of Twisted. It lets you use something similar to Django URL dispatching for handling IRC messages with a side benefit of having the Django ORM available for generating responses, etc.

Fizz answered 31/5, 2009 at 4:37 Comment(0)
C
0

I know it's not Python, but for a regular expression-based approach to this problem, you could take a look at POE::Filter::IRCD, which handles IRC server protocol (see POE::Filter::IRC::Compat for the client protocol additions) parsing for Perl's POE::Component::IRC framework.

Cinerary answered 18/2, 2010 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.