Advertisement
ijontichy

IRCResponse

Sep 3rd, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # -*- coding: utf-8 -*-
  4.  
  5. import time
  6.  
  7. class InvalidIRCResponse(Exception): pass
  8.  
  9. class IRCResponse(object):
  10.  
  11.     def __init__(self, message):
  12.  
  13.         self.creationTime      = time.gmtime()
  14.         self.creationTimestamp = time.strftime("<%H:%M>", self.creationTime)
  15.  
  16.         self.response = message
  17.         self.parseCommand()
  18.  
  19.  
  20.     def parseCommand(self):
  21.  
  22.         source     = ""
  23.         sourceFull = ""
  24.         sourceHost = ""
  25.         sourceUser = ""
  26.         command    = ""
  27.         args       = ""
  28.         message    = ""
  29.  
  30.         if self.response:
  31.  
  32.             if not self.response.startswith(":"):
  33.                 raise InvalidIRCResponse("no starting colon")
  34.  
  35.             message = self.response.lstrip(":")
  36.             msgList = message.partition(":")
  37.  
  38.  
  39.             if message.startswith("PING"):
  40.  
  41.                 source     = msgList[2]
  42.                 sourceFull = msgList[2]
  43.                 sourceHost = msgList[2]
  44.                 sourceUser = msgList[2]
  45.  
  46.                 command = msgList[0].lower().strip()
  47.  
  48.             elif message.startswith("ERROR"):
  49.                 command = msgList[0]
  50.                 msg = msgList[2]
  51.  
  52.             else:
  53.                 msgSplit = msgList[0].split()
  54.  
  55.                 msg = msgList[2]
  56.  
  57.                 try:
  58.                     source  = msgSplit[0]
  59.                 except IndexError:
  60.                     raise InvalidIRCResponse("no source")
  61.  
  62.                 try:
  63.                     command = msgSplit[1].lower().strip()
  64.                 except IndexError:
  65.                     raise InvalidIRCResponse("no command")
  66.  
  67.                 args = msgSplit[2:]
  68.  
  69.  
  70.             if "@" in source:
  71.                 sourceP    = source.partition("@")
  72.                 sourceHost = sourceP[2]
  73.                 sourceU    = sourceP[0].partition("!")
  74.                 source     = sourceU[0]
  75.                 sourceUser = sourceU[2]
  76.  
  77.         self.source     = source
  78.         self.sourceFull = sourceFull
  79.         self.sourceHost = sourceHost
  80.         self.sourceUser = sourceUser
  81.         self.command    = command
  82.         self.args       = args
  83.         self.message    = msg
  84.  
  85.     @property
  86.     def isResponse(self):
  87.         return True
  88.  
  89.  
  90.  
  91.     @property
  92.     def isMessage(self):
  93.         return False
  94.  
  95.     @property
  96.     def canBeMessage(self):
  97.         return (self.command == "privmsg" and self.message)
  98.  
  99.  
  100.  
  101.     @property
  102.     def isCTCPMessage(self):
  103.         return False
  104.  
  105.     @property
  106.     def canBeCTCPMessage(self):
  107.         return (self.canBeMessage and (self.message.startswith("\x01") and
  108.                 self.message.endswith("\x01") ) )
  109.  
  110.  
  111.  
  112.     @property
  113.     def isNotice(self):
  114.         return False
  115.  
  116.     @property
  117.     def canBeNotice(self):
  118.         return (self.command == "notice" and self.message)
  119.  
  120.  
  121.  
  122.     @property
  123.     def isCTCPNotice(self):
  124.         return False
  125.  
  126.     @property
  127.     def canBeCTCPNotice(self):
  128.         return (self.canBeNotice and (self.message.startswith("\x01") and
  129.                 self.message.endswith("\x01") ) )
  130.  
  131.  
  132.  
  133.     def __repr__(self):
  134.         return "{}({})".format(self.__class__.__name__, repr(self.response) )
  135.  
  136.     def __str__(self):
  137.         ret = [self.source, self.command, self.args, self.message]
  138.         return str(ret)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement