Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. import re
  4. from twisted.internet import protocol, reactor
  5. from twisted.words.protocols import oscar
  6.  
  7. USERNAME = 'beewojima'
  8. PASSWORD = 'monkyshines'
  9. VERSION = '0.1'
  10.  
  11. # Makes the bot speak the arguments
  12. def cmd_say(chat, args):
  13.     if len(args) < 1:
  14.         chat.sendMessage('<b>Command requires 1 or more arguments</b>')
  15.     else:
  16.         msg = ''
  17.         for arg in args: msg += arg + ' '
  18.         chat.sendMessage(msg.strip())
  19.  
  20. # Adds an operator
  21. def cmd_op(chat, args):
  22.     if len(args) != 1:
  23.         chat.sendMessage('<b>Command requires 1 argument</b>')  
  24.     else:
  25.         user = args[0]
  26.         if user not in ops:
  27.             ops.append(user)
  28.             chat.sendMessage('<b>Added operator: ' + user + '</b>')
  29.  
  30. # Lists the current operators
  31. def cmd_ops(chat, args):
  32.     if len(args) != 0:
  33.         chat.sendMessage('<b>Command does not take any arguments</b>')
  34.     else:
  35.         msg = '<b>Operators: '
  36.         for op in ops: msg += op + ' '
  37.         msg += '</b>'
  38.         chat.sendMessage(msg)
  39.  
  40. ops = ['treewojima']
  41. commands = { 'say': cmd_say,
  42.              'op': cmd_op,
  43.              'ops': cmd_ops }
  44.  
  45. class ChatBot(oscar.BOSConnection):
  46.     capabilities = [oscar.CAP_CHAT]
  47.  
  48.     def initDone(self):
  49.         # Once init/connection is complete, set twisted callbacks
  50.         self.requestSelfInfo().addCallback(self._gotSelfInfo)
  51.         self.requestSSI().addCallback(self._gotBuddyList)
  52.  
  53.     def _gotSelfInfo(self, user):
  54.         # Find out who we connected as
  55.         print 'Connected as: ', user.name
  56.         self.name = user.name
  57.  
  58.     def _gotBuddyList(self, l):
  59.         # Get everything ready
  60.         self.activateSSI()
  61.         self.setProfile("""ChatBot""")
  62.         self.setIdleTime(0)
  63.         self.clientReady()
  64.  
  65.     def receiveChatInvite(self, user, message, exchange, fullName, instance,
  66.                           shortName, inviteTime):
  67.         # Automatically join any chat we're invited to
  68.         self.joinChat(exchange, fullName, instance).addCallback(self.chatJoined)
  69.  
  70.     def chatJoined(self, chat):
  71.         # Upon joining, print out a little banner and the currently active ops
  72.         print 'Joined chat room ', chat.name
  73.         chat.sendMessage('<b>Nerdlinger v' + VERSION + ' loaded')
  74.         cmd_ops(chat, [])
  75.  
  76.     def chatReceiveMessage(self, chat, user, message):
  77.         # If the user who spoke isn't an op, don't do anything
  78.         if user.name not in ops: return
  79.  
  80.         # If it's not a command, skip it
  81.         msg = self._stripTags(message).strip()
  82.         if msg[0] != '!': return
  83.  
  84.         # Properly format all the arguments passed, including the command itself
  85.         args = msg[1:].split(' ')
  86.         print 'Command:   ', args[0]
  87.         print 'Arguments: ', args[1:]
  88.  
  89.         # Dispatch through the command dictionary
  90.         if args[0] not in commands:
  91.             chat.sendMessage('<b>Command not recognized</b>')
  92.         else:    
  93.             commands[args[0]](chat, args[1:])
  94.  
  95.  
  96.     # Strips HTML tags from a string
  97.     def _stripTags(self, value):
  98.         return re.sub(r'<[^>]*?>', '', value)
  99.  
  100. class ChatBotOA(oscar.OscarAuthenticator):
  101.     BOSClass = ChatBot
  102.  
  103. hostport = ('login.oscar.aol.com', 5190)
  104. protocol.ClientCreator(reactor, ChatBotOA, USERNAME, PASSWORD, 0).connectTCP(*hostport)
  105. reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement