Advertisement
clairec

roboclaire

Jul 23rd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Claire Cavanaugh, 2016
  4. # Written in Python 3. Follows RFC 2812
  5.  
  6. # The purpose of this project is to create a bot that connects to an IRC server
  7. # and joins a channel to perform various tasks, including:
  8. #
  9. #     * Providing the titles of pages linked to through URLs
  10.  
  11. import re
  12.  
  13. class Message:
  14.     """Represents an IRC Message, allowing access to the components of it."""
  15.     def __init__(self, message=b''):
  16.         self.message = message
  17.         self.items = {"servername" : None,
  18.                        "nickname" : None,
  19.                        "user" : None,
  20.                        "hostname" : None,
  21.                        "hostaddr" : None,
  22.                        "command" : None,
  23.                        "params" : None,
  24.                        "middle" : None,
  25.                        "trailing" : None }
  26.  
  27.     def lex(self):
  28.         """Lex self.message into its components."""
  29.         shortname = rb"(?: [a-zA-Z0-9] (?: [a-zA-Z0-9-]* [a-zA-Z0-9])?)"
  30.         ip4addr = rb"(?: [0-9]{1,3} (?: \. [0-9]{1,3}){3})"
  31.         ip6addr = (rb"(?: (?: [0-9a-fA-F]+ (?: :[0-9a-fA-F]+){7})|"
  32.                    rb"(?: (?: 0:){5} (?: 0|FFFF):%s))" % ip4addr )
  33.         servername = rb"(?P<servername> %s (?: \.%s*)*)" % (shortname, shortname)
  34.         hostaddr = rb"(?P<hostaddr> %s | %s )" % (ip4addr, ip6addr)
  35.         hostname = rb"(?P<hostname> %s (?: \.%s*)*)" % (shortname, shortname)
  36.         nickname = rb"(?P<nickname> []a-zA-Z[\\`_^{|}] []a-zA-Z0-9[\\`_^{|}-]*)"
  37.         user = rb"(?P<user> [^\x00\n\r @]+)"
  38.         host = rb"(?P<host> %s|%s )" % (hostname, hostaddr)
  39.         clientname = rb"(?: %s (?: (?: !%s)? @%s)?)" % (nickname, user, host)
  40.         middle = rb"(?: [^ \x00\r\n:] [^ \x00\r\n]*)"
  41.         params = (rb"(?P<params>(?: (?P<middle>(?: \ %s){,14})(?P<trailing> \ :[^\x00\r\n]*)?)|"
  42.                   rb"(?:(?P<m2>(?: \ %s){14}) (?P<t2> \ :? [^\x00\r\n]*)?))?" % (middle, middle))
  43.         prefix = rb"(?P<prefix> : (?:%s|%s) \ )?" % (servername, clientname)
  44.         prefix2 = rb"(?: "
  45.         command = rb"(?P<command> [a-zA-Z]+ | [0-9]{3})"
  46.         msg_re = re.compile(prefix + command + params + rb"[\r\n]+", re.X)
  47.         matched = msg_re.match(self.message)
  48.         items = matched.groupdict()
  49.         if items["middle"] is None:
  50.             items["middle"] = items.pop("m2")
  51.         if items["trailing"] is None:
  52.             items["trailing"] = items.pop("t2")
  53.         del items["m2"]; del items["t2"]
  54.         self.items = items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement