Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- import re
- from twisted.internet import protocol, reactor
- from twisted.words.protocols import oscar
- USERNAME = 'beewojima'
- PASSWORD = 'monkyshines'
- VERSION = '0.1'
- # Makes the bot speak the arguments
- def cmd_say(chat, args):
- if len(args) < 1:
- chat.sendMessage('<b>Command requires 1 or more arguments</b>')
- else:
- msg = ''
- for arg in args: msg += arg + ' '
- chat.sendMessage(msg.strip())
- # Adds an operator
- def cmd_op(chat, args):
- if len(args) != 1:
- chat.sendMessage('<b>Command requires 1 argument</b>')
- else:
- user = args[0]
- if user not in ops:
- ops.append(user)
- chat.sendMessage('<b>Added operator: ' + user + '</b>')
- # Lists the current operators
- def cmd_ops(chat, args):
- if len(args) != 0:
- chat.sendMessage('<b>Command does not take any arguments</b>')
- else:
- msg = '<b>Operators: '
- for op in ops: msg += op + ' '
- msg += '</b>'
- chat.sendMessage(msg)
- ops = ['treewojima']
- commands = { 'say': cmd_say,
- 'op': cmd_op,
- 'ops': cmd_ops }
- class ChatBot(oscar.BOSConnection):
- capabilities = [oscar.CAP_CHAT]
- def initDone(self):
- # Once init/connection is complete, set twisted callbacks
- self.requestSelfInfo().addCallback(self._gotSelfInfo)
- self.requestSSI().addCallback(self._gotBuddyList)
- def _gotSelfInfo(self, user):
- # Find out who we connected as
- print 'Connected as: ', user.name
- self.name = user.name
- def _gotBuddyList(self, l):
- # Get everything ready
- self.activateSSI()
- self.setProfile("""ChatBot""")
- self.setIdleTime(0)
- self.clientReady()
- def receiveChatInvite(self, user, message, exchange, fullName, instance,
- shortName, inviteTime):
- # Automatically join any chat we're invited to
- self.joinChat(exchange, fullName, instance).addCallback(self.chatJoined)
- def chatJoined(self, chat):
- # Upon joining, print out a little banner and the currently active ops
- print 'Joined chat room ', chat.name
- chat.sendMessage('<b>Nerdlinger v' + VERSION + ' loaded')
- cmd_ops(chat, [])
- def chatReceiveMessage(self, chat, user, message):
- # If the user who spoke isn't an op, don't do anything
- if user.name not in ops: return
- # If it's not a command, skip it
- msg = self._stripTags(message).strip()
- if msg[0] != '!': return
- # Properly format all the arguments passed, including the command itself
- args = msg[1:].split(' ')
- print 'Command: ', args[0]
- print 'Arguments: ', args[1:]
- # Dispatch through the command dictionary
- if args[0] not in commands:
- chat.sendMessage('<b>Command not recognized</b>')
- else:
- commands[args[0]](chat, args[1:])
- # Strips HTML tags from a string
- def _stripTags(self, value):
- return re.sub(r'<[^>]*?>', '', value)
- class ChatBotOA(oscar.OscarAuthenticator):
- BOSClass = ChatBot
- hostport = ('login.oscar.aol.com', 5190)
- protocol.ClientCreator(reactor, ChatBotOA, USERNAME, PASSWORD, 0).connectTCP(*hostport)
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement