Guest User

Untitled

a guest
Feb 1st, 2011
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. from twisted.internet import reactor, protocol
  2. from twisted.words.protocols import irc
  3. from twisted.python import log
  4. import sys, time
  5.  
  6. class MessageLogger:
  7.     def __init__(self, file):
  8.         self.file = file
  9.        
  10.     def log(self, message):
  11.         timestamp = time.strftime("[%D - %H:%M]", time.localtime(time.time()))
  12.         self.file.write('%s %s\n' % (timestamp, message))
  13.         self.file.flush()
  14.  
  15.     def close(self):
  16.         self.file.close()
  17.        
  18. class IRCProtocol(irc.IRCClient):
  19.     nickname = "trex"
  20.     def connectionMade(self):
  21.         irc.IRCClient.connectionMade(self)
  22.         self.logger = MessageLogger(open(self.factory.filename, "a"))
  23.    
  24.     def signedOn(self):
  25.         print 'Success! Connection established.'
  26.         self.join(self.factory.channels)
  27.         print 'Joined channel', self.factory.channels
  28.    
  29.     def userJoined(self, user, channel):
  30.         self.logger.log("%s" % (self.whois(user)))
  31.        
  32. class IRCFactory(protocol.ClientFactory):
  33.     protocol = IRCProtocol
  34.     channels = "#test"
  35.    
  36.     def __init__(self, channel, filename):
  37.         self.channel = channel
  38.         self.filename = filename
  39.  
  40.     def clientConnectionFailed(self, connector, reason):
  41.         print "Connection failed because of %s" % reason
  42.         reactor.stop()
  43.  
  44.     def clientConnectionLost(self, connector, reason):
  45.         print "Connection lost: %s" % reason
  46.         connector.connect()
  47.  
  48. if __name__ == "__main__":
  49.     log.startLogging(sys.stdout)
  50.     host, port = "irc.freenode.net", 6667
  51.     fact = IRCFactory(sys.argv[1],sys.argv[2])
  52.     reactor.connectTCP(host, port, fact)
  53.     reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment