Advertisement
nucular

botbase.py

Nov 3rd, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. """
  2. A useful wrapper with logging etc. for the
  3. twisted.words.protocols.irc.IRCClient class.
  4. """
  5. import sys, time, fnmatch
  6.  
  7. from twisted.words.protocols import irc
  8. from twisted.internet import protocol, reactor
  9.  
  10. DEBUG = 0
  11. INFO = 1
  12. WARNING = 2
  13. ERROR = 3
  14.  
  15. def getHostmask(user):
  16.     host = user.split("@")[1]
  17.     return "*!*@"+host
  18.  
  19. def matchHostmask(user, hostmask):
  20.     return fnmatch.fnmatch(user, hostmask)
  21.  
  22. class Logger(object):
  23.     def __init__(self, level=INFO, stream=sys.stdout,
  24.         timestamp="[%H:%M:%S]"):
  25.         self.level = level
  26.         self.stream = stream
  27.         self.timestamp = timestamp
  28.    
  29.     def log(self, level, text):
  30.         if level < self.level:
  31.             return
  32.         if self.timestamp:
  33.             stamp = time.strftime(self.timestamp, time.localtime(time.time()))
  34.             text = "%s %s" % (stamp, text)
  35.         self.stream.write(text + "\r\n")
  36.         self.stream.flush()
  37.    
  38.     def __call__(self, *args, **kwargs):
  39.         self.log(*args, **kwargs)
  40.  
  41.  
  42. class BotBase(irc.IRCClient):
  43.     nickname = ""
  44.    
  45.     def __init__(self, factory):
  46.         self.factory = factory
  47.         BotBase.nickname = self.factory.nick #ugh
  48.         self.logger = self.factory.logger
  49.    
  50.     # Prototype the following callbacks
  51.    
  52.     def connectionMade(self):
  53.         irc.IRCClient.connectionMade(self)
  54.         self.logger(INFO, "Connection made")
  55.    
  56.     def connectionLost(self, reason):
  57.         irc.IRCClient.connectionLost(self, reason)
  58.         self.logger(ERROR, "Connection lost (%s)" % reason)
  59.    
  60.     def signedOn(self):
  61.         if isinstance(self.factory.channels, str):
  62.             self.join(self.factory.channels)
  63.         else:
  64.             self.logger(INFO, "Joining channels...")
  65.             for chan in self.factory.channels:
  66.                 self.join(chan)
  67.    
  68.     def joined(self, channel):
  69.         self.logger(DEBUG, "Joined %s" % channel)
  70.    
  71.     # privmsg() dispatches a PRIVMSG into privateMsg
  72.     # and channelMsg. Don't prototype.
  73.    
  74.     def privmsg(self, user, channel, msg):
  75.         nick = user.split("!")[0]
  76.        
  77.         if channel == self.nickname:
  78.             self.privateMsg(user, nick, msg)
  79.         else:
  80.             self.channelMsg(user, nick, channel, msg)
  81.            
  82.     # Higher-Level callbacks, prototype too.
  83.    
  84.     def privateMsg(self, user, nick, msg):
  85.         self.logger(DEBUG, "-%s- %s" % (nick, msg))
  86.    
  87.     def channelMsg(self, user, nick, channel, msg):
  88.         self.logger(DEBUG, "{%s} <%s> %s" % (channel, nick, msg))
  89.  
  90.  
  91. class BotBaseFactory(protocol.ClientFactory):
  92.     def __init__(self, protocol, nick, channels, args, logger=Logger()):
  93.         self.protocol = protocol
  94.         self.nick = nick
  95.         self.channels = channels
  96.         self.logger = logger
  97.         self.reconnect = True
  98.         self.args = args
  99.         logger(DEBUG, "BotBaseFactory initialized")
  100.    
  101.     def buildProtocol(self, addr):
  102.         self.logger(DEBUG, "Building Protocol...")
  103.         p = self.protocol(self, *self.args)
  104.         return p
  105.    
  106.     def clientConnectionLost(self, connector, reason):
  107.         if self.reconnect:
  108.             self.logger(INFO, "Reconnecting...")
  109.             connector.connect()
  110.    
  111.     def clientConnectionFailed(self, connector, reason):
  112.         self.logger(ERROR, "Connection failed (%s)" % reason)
  113.         reactor.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement