#!/usr/bin/env python
#
# Claire Cavanaugh, 2016
# Written in Python 3. Follows RFC 2812
# The purpose of this project is to create a bot that connects to an IRC server
# and joins a channel to perform various tasks, including:
#
# * Providing the titles of pages linked to through URLs
import re
class Message:
"""Represents an IRC Message, allowing access to the components of it."""
def __init__(self, message=b''):
self.message = message
self.items = {"servername" : None,
"nickname" : None,
"user" : None,
"hostname" : None,
"hostaddr" : None,
"command" : None,
"params" : None,
"middle" : None,
"trailing" : None }
def lex(self):
"""Lex self.message into its components."""
shortname = rb"(?: [a-zA-Z0-9] (?: [a-zA-Z0-9-]* [a-zA-Z0-9])?)"
ip4addr = rb"(?: [0-9]{1,3} (?: \. [0-9]{1,3}){3})"
ip6addr = (rb"(?: (?: [0-9a-fA-F]+ (?: :[0-9a-fA-F]+){7})|"
rb"(?: (?: 0:){5} (?: 0|FFFF):%s))" % ip4addr )
servername = rb"(?P<servername> %s (?: \.%s*)*)" % (shortname, shortname)
hostaddr = rb"(?P<hostaddr> %s | %s )" % (ip4addr, ip6addr)
hostname = rb"(?P<hostname> %s (?: \.%s*)*)" % (shortname, shortname)
nickname = rb"(?P<nickname> []a-zA-Z[\\`_^{|}] []a-zA-Z0-9[\\`_^{|}-]*)"
user = rb"(?P<user> [^\x00\n\r @]+)"
host = rb"(?P<host> %s|%s )" % (hostname, hostaddr)
clientname = rb"(?: %s (?: (?: !%s)? @%s)?)" % (nickname, user, host)
middle = rb"(?: [^ \x00\r\n:] [^ \x00\r\n]*)"
params = (rb"(?P<params>(?: (?P<middle>(?: \ %s){,14})(?P<trailing> \ :[^\x00\r\n]*)?)|"
rb"(?:(?P<m2>(?: \ %s){14}) (?P<t2> \ :? [^\x00\r\n]*)?))?" % (middle, middle))
prefix = rb"(?P<prefix> : (?:%s|%s) \ )?" % (servername, clientname)
prefix2 = rb"(?: "
command = rb"(?P<command> [a-zA-Z]+ | [0-9]{3})"
msg_re = re.compile(prefix + command + params + rb"[\r\n]+", re.X)
matched = msg_re.match(self.message)
items = matched.groupdict()
if items["middle"] is None:
items["middle"] = items.pop("m2")
if items["trailing"] is None:
items["trailing"] = items.pop("t2")
del items["m2"]; del items["t2"]
self.items = items
Comments