Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | None | 0 0
  1. #!/usr/bin/python2
  2. from twisted.words.protocols import irc
  3. from twisted.internet import reactor, protocol
  4. import pynotify
  5. from cgi import escape as cgi_escape
  6.  
  7. try:
  8.     pynotify.init("Markup")
  9. except Exception, error:
  10.     print "Failed to init python-notify 'Markup'"
  11.     print error
  12.  
  13. class Notifier:
  14.     '''
  15.    An independent logger class (because separation of application
  16.    and protocol logic is a good thing).
  17.    '''
  18.     def __init__(self):
  19.         self.colors = {
  20.              'normal':      '#FFFFFF',
  21.              'query':       '#FF0044',
  22.              'hilight':     '#9393DD',
  23.              'disconnect':  '#CC9393',
  24.              'connect':     '#7F9F7F'
  25.              }
  26.         self.on=True
  27.         pass
  28.     def sanitize(self, str):
  29.         return cgi_escape(str)
  30.  
  31.     def notify(self, channel, message, type='normal', time=4):
  32.         notification = pynotify.Notification(
  33.                 '<span color=\'%s\'>%s</span>\n%s' %
  34.                 (self.colors[type], channel, self.sanitize(self.trim(message,50))), '')
  35.         notification.set_timeout(time*1000)
  36.         notification.show()
  37.  
  38.     def notify_on(self):
  39.         self.on=True
  40.  
  41.     def notify_off(self):
  42.         self.on=False
  43.        
  44.     def trim(self, str, length):
  45.         if len(str) > length:
  46.             return str[:length-3] + '...'
  47.         else:
  48.             return str + (' ' * (length - len(str)))
  49.  
  50. class IRCBot(irc.IRCClient):
  51.     '''A logging IRC bot.'''
  52.    
  53.     nickname = 'tb'
  54.     password = 'pass'
  55.     nick = 'HarryD'
  56.  
  57.     def connectionMade(self):
  58.         irc.IRCClient.connectionMade(self)
  59.         self.n = self.factory.notifyer
  60.    
  61.     def connectionLost(self, reason):
  62.         irc.IRCClient.connectionLost(self, reason)
  63.  
  64.     def signedOn(self):
  65.         '''Called when bot has succesfully signed on to server.'''
  66.         self.n.notify('Connected', 'You are connected to the server')
  67.  
  68.     def privmsg(self, user, channel, msg):
  69.         '''This will get called when the bot receives a message.'''
  70.         user = user.split('!', 1)[0]
  71.         if msg.lower().find(self.nick.lower()) != -1:
  72.             self.n.notify(channel, '< %s> %s' % (user, msg), 'hilight', 0)
  73.         elif channel == self.nick:
  74.             self.n.notify(user, '%s' % msg, 'query', 0)
  75.         else:
  76.             self.n.notify(channel, '< %s> %s' % (user, msg))
  77.  
  78.     def action(self, user, channel, msg):
  79.         '''This will get called when the bot sees someone do an action.'''
  80.         user = user.split('!', 1)[0]
  81.     # irc callbacks
  82.  
  83.     def irc_NICK(self, prefix, params):
  84.         '''Called when an IRC user changes their nickname.'''
  85.         old_nick = prefix.split('!')[0]
  86.         new_nick = params[0]
  87.  
  88.     def irc_QUIT(self, prefix, params):
  89.         nick = prefix.split('!')[0]
  90.         self.n.notify('Disconnected:', nick, 'disconnect', 3)
  91.  
  92.     def irc_JOIN(self, prefix, params):
  93.         nick = prefix.split('!')[0]
  94.         self.n.notify('Connected:', nick, 'connect', 3)
  95.  
  96.     def irc_PART(self, prefix, params):
  97.         nick = prefix.split('!')[0]
  98.         self.n.notify('Disconnected:', nick, 'disconnect', 3)
  99.  
  100. #    def lineReceived(self, line):
  101. #        print line
  102.  
  103.     # For fun, override the method that determines how a nickname is changed on
  104.     # collisions. The default method appends an underscore.
  105.     def alterCollidedNick(self, nickname):
  106.         '''
  107.        Generate an altered version of a nickname that caused a collision in an
  108.        effort to create an unused related name for subsequent registration.
  109.        '''
  110.         return nickname + '^'
  111.  
  112.  
  113.  
  114. class IRCBotFactory(protocol.ClientFactory):
  115.     '''A factory for LogBots.
  116.  
  117.    A new protocol instance will be created each time we connect to the server.
  118.    '''
  119.  
  120.     # the class of the protocol to build when new connection is made
  121.     protocol = IRCBot
  122.    
  123.     def __init__(self, password, nick, notifyer):
  124.         self.notifyer = notifyer
  125.     def clientConnectionLost(self, connector, reason):
  126.         '''If we get disconnected, reconnect to server.'''
  127.         connector.connect()
  128.  
  129.     def clientConnectionFailed(self, connector, reason):
  130.         print 'connection failed:', reason
  131.         reactor.stop()
  132.  
  133.  
  134. if __name__ == '__main__':
  135.     # initialize logging
  136.     n = Notifier()
  137.     for i in range(2776, 2780):
  138.         # create factory protocol and application
  139.         f = IRCBotFactory('pass', 'HarryD', n)
  140.         # connect factory to this host and port
  141.         reactor.connectTCP('harry.is', i, f)
  142.  
  143.     # run bot
  144.     reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement