def dispatchCommand(self, command, sender, originalMessage, twxml): """Find plugins that can respond to the command or execute all the plugins responding to default""" self.dprint('Dispatching command %s' % command) if command.has_key('to'): """Command is meant for the bot""" self.dprint('This is our command') plg = command['parts'].pop(0).lower() args = '' for arg in command['parts']: args += ' ' + arg.strip() message = '' """Handle a few high level commands: help, mute, unmute""" if plg == 'help': self.dprint('Finding plugins with help') message = 'In a groupchat, all commands start with "%s:" or "!."\nIn private chat, simply type the command.\nThese are the commands available. For help on a command, try -h\n' % self.client.userId for plugin in getPluginsByCapability('help'): self.dprint('plugins %s has help' % plugin) message += plugin.name + '\n' return message elif plg == 'mute': self.mute = True elif plg == 'unmute': self.mute = False return 'At your service.' elif plg == 'default': # this is necessary to prevent users from invoking the catchall (default) plugins message = 'Unknown command. Try help.' else: # find the plugins having the command name for plugin in getPluginsByCapability(plg): if not plugin.private(): self.dprint('Calling plugin %s with %s' % (plg, args)) message += plugin.call(args=args.strip().split(), xmppAdapter=self, client=self.client, message=originalMessage, sender=sender, twxml=twxml, log=log) else: self.dprint( 'Would have called plugins %s, but it is private' % plg) if not message: """Plugins that return no result TODO: give the plugin system tighter integration""" return 'Unknown command. Try help.' return message else: # if the message is not a command, run every plugin with a default capability for plugin in getPluginsByCapability('default'): self.dprint( 'Calling default plugin %s sender:%s, room:%s, message:%s' % (plugin, sender, '', originalMessage)) plugin.default(sender, '', originalMessage, self, self.client) return False
def checkAccess(self, twxml): """Access control plugins""" fromJid = twxml['from'] messageType = twxml.attributes.get('type', 'nothing') if messageType == 'groupchat': # no access control for group chat. # need a way to tell exactly which user sent a message, but is it # posslbe? return fromJid elif messageType == 'presence': pass # do something with these elif messageType == 'nothing': for elmt in twxml.elements(): for child in elmt.children: try: if child.name == 'invite': fromJid = child['from'] log.debug('Invited to a room from %s. Need to see if this user is authorized.' % fromJid) break except AttributeError: pass authorized = False user = fromJid for plugin in getPluginsByCapability('accessControl'): self.dprint('Calling access control plugin %s for %s' % (plugin, user)) authorized = plugin.call(sender = user, xmppAdapter = self, client = self.client, log = log) self.dprint('Done looking for access control plugin.') self.dprint('The user %s authorization result is %s' % (fromJid, authorized)) if authorized: return fromJid else: return False
def checkCommand(self, twistedStanza): """Check the message for a command and dispatch it if it contains one""" fromJid = twistedStanza["from"] messageType = twistedStanza.attributes.get("type", "nothing") for element in twistedStanza.elements(): if element.name == "body": self.logger.debug("Processing chat FROm:%s BODy:%s" % (fromJid, element.toXml())) try: message = element.children[0] except IndexError: continue command = self.findCommand(message, messageType) room = fromJid.split("@", 1)[0] self.logger.debug('RETURNED command "%s"' % command) if command: self.dispatchCommand(command, fromJid, message, twistedStanza, room, messageType) else: for plugin in getPluginsByCapability("default", self): self.logger.debug( "Calling default plugin %s sender:%s, room:%s, message:%s" % (plugin, fromJid, room, message) ) plugin.default( client=self, message=message, sender=fromJid, room=room, twistedStanza=twistedStanza, log=self.pluginLogger, ) break
def checkAccess(self, fromJid, messageType): """Access control plugins""" if messageType == 'groupchat': # no access control for group chat. # need a way to tell exactly which user sent a message, but is it # posslbe? return fromJid elif messageType == 'presence': pass # do something with these authorized = False user = fromJid for plugin in getPluginsByCapability('accessControl', self): self.logger.debug('Calling access control plugin %s for %s' % (plugin, user)) authorized = plugin.call(sender=user, xmppAdapter=self, log=self.pluginLogger) self.logger.debug('The user %s authorization result is %s' % (fromJid, authorized)) # return the first positive response if authorized: return fromJid self.logger.debug('Done looking for access control plugin.') self.logger.debug('The user %s authorization result is %s' % (fromJid, authorized)) return False
def checkCommand(self, twistedStanza): """Check the message for a command and dispatch it if it contains one""" fromJid = twistedStanza['from'] messageType = twistedStanza.attributes.get('type', 'nothing') for element in twistedStanza.elements(): if element.name == 'body': self.logger.debug('Processing chat FROm:%s BODy:%s' % (fromJid, element.toXml())) try: message = element.children[0] except IndexError: continue command = self.findCommand(message, messageType) room = fromJid.split('@', 1)[0] self.logger.debug('RETURNED command "%s"' % command) if command: self.dispatchCommand(command, fromJid, message, twistedStanza, room, messageType) else: for plugin in getPluginsByCapability('default', self): self.logger.debug( 'Calling default plugin %s sender:%s, room:%s, message:%s' % (plugin, fromJid, room, message)) plugin.default(client=self, message=message, sender=fromJid, room=room, twistedStanza=twistedStanza, log=self.pluginLogger) break
def processAlerts(self): """this is called periodically by LoopingCall to wake up the plugins that are not driven by user input""" for plugin in getPluginsByCapability('alert', self): self.logger.debug('Calling alert plugin %s' % plugin) reactor.callInThread(plugin.alert, client=self, log=self.pluginLogger)
def dispatchCommand(self, command, sender, originalMessage, twxml): """Find plugins that can respond to the command or execute all the plugins responding to default""" self.dprint('Dispatching command %s' % command) if command.has_key('to'): """Command is meant for the bot""" self.dprint('This is our command') plg = command['parts'].pop(0).lower() args = '' for arg in command['parts']: args += ' ' + arg.strip() message = '' """Handle a few high level commands: help, mute, unmute""" if plg == 'help': self.dprint('Finding plugins with help') message = 'In a groupchat, all commands start with "%s:" or "!."\nIn private chat, simply type the command.\nThese are the commands available. For help on a command, try -h\n' % self.client.userId for plugin in getPluginsByCapability('help'): self.dprint('plugins %s has help' % plugin) message += plugin.name + '\n' return message elif plg == 'mute': self.mute = True elif plg == 'unmute': self.mute = False return 'At your service.' elif plg == 'default': # this is necessary to prevent users from invoking the catchall (default) plugins message = 'Unknown command. Try help.' else: # find the plugins having the command name for plugin in getPluginsByCapability(plg): if not plugin.private(): self.dprint('Calling plugin %s with %s' % (plg, args)) message += plugin.call(args = args.strip().split(), xmppAdapter = self, client = self.client, message = originalMessage, sender = sender, twxml = twxml, log = log) else: self.dprint('Would have called plugins %s, but it is private' % plg) if not message: """Plugins that return no result TODO: give the plugin system tighter integration""" return 'Unknown command. Try help.' return message else: # if the message is not a command, run every plugin with a default capability for plugin in getPluginsByCapability('default'): self.dprint('Calling default plugin %s sender:%s, room:%s, message:%s' % (plugin, sender, '', originalMessage)) plugin.default(sender, '', originalMessage, self, self.client) return False
def checkAccess(self, twxml): """Access control plugins""" fromJid = twxml['from'] messageType = twxml.attributes.get('type', 'nothing') if messageType == 'groupchat': # no access control for group chat. # need a way to tell exactly which user sent a message, but is it # posslbe? return fromJid elif messageType == 'presence': pass # do something with these elif messageType == 'nothing': for elmt in twxml.elements(): for child in elmt.children: try: if child.name == 'invite': fromJid = child['from'] log.debug( 'Invited to a room from %s. Need to see if this user is authorized.' % fromJid) break except AttributeError: pass authorized = False user = fromJid for plugin in getPluginsByCapability('accessControl'): self.dprint('Calling access control plugin %s for %s' % (plugin, user)) authorized = plugin.call(sender=user, xmppAdapter=self, client=self.client, log=log) self.dprint('Done looking for access control plugin.') self.dprint('The user %s authorization result is %s' % (fromJid, authorized)) if authorized: return fromJid else: return False
def checkAccess(self, fromJid, messageType): """Access control plugins""" if messageType == "groupchat": # no access control for group chat. # need a way to tell exactly which user sent a message, but is it # posslbe? return fromJid elif messageType == "presence": pass # do something with these authorized = False user = fromJid for plugin in getPluginsByCapability("accessControl", self): self.logger.debug("Calling access control plugin %s for %s" % (plugin, user)) authorized = plugin.call(sender=user, xmppAdapter=self, log=self.pluginLogger) self.logger.debug("The user %s authorization result is %s" % (fromJid, authorized)) # return the first positive response if authorized: return fromJid self.logger.debug("Done looking for access control plugin.") self.logger.debug("The user %s authorization result is %s" % (fromJid, authorized)) return False
def dispatchCommand(self, command, sender, originalMessage, twistedStanza, room, messageType): """Find plugins that can respond to the command or execute all the plugins responding to default""" self.logger.debug("Dispatching command %s" % command) """Command is meant for the bot""" self.logger.debug("This is our command") plg = command.pop(0).lower() args = [] for arg in command: args.append(arg.strip()) message = "" """Handle a few high level commands: help, mute, unmute""" if plg == "help": self.logger.debug("Finding plugins with help") message = ( 'In a groupchat, all commands start with "%s:" or "!."\nIn private chat, simply type the command.\nThese are the commands available. For help on a command, try -h\n' % self.username ) pluginNames = [] for plugin in getPluginsByCapability("help", self): self.logger.debug("plugins %s has help" % plugin) try: pluginNames.append(plugin.name) except AttributeError: # baaad plugin, doesn't assign a name for itself pass message += ", ".join(pluginNames) self.sendMessage(message, sender, messageType) return if plg == "default": # this is necessary to prevent users from invoking the catchall (default) plugins self.sendMessage("Unknown command. Try help.", sender, messageType) else: # find the plugins having the command name plugin_arguments = { "args": args, "command": "something", "client": self, "message": originalMessage, "messageType": messageType, "sender": sender, "twxml": twistedStanza, "room": room, "log": self.pluginLogger, } foundCommand = False for plugin in getPluginsByCapability(plg, self): if not plugin.private: foundCommand = True self.logger.debug("Calling plugin %s with %s" % (plg, plugin_arguments)) plugin_arguments["command"] = plg if plugin.threadsafe: self.logger.debug("%s is marked as threadsafe" % plg) reactor.callInThread(plugin.call, **plugin_arguments) else: plugin.call(**plugin_arguments) else: self.logger.debug("Would have called plugins %s, but it is private" % plg) if not foundCommand: self.sendMessage("Unknown command. Try help.", sender, messageType)
def processAlerts(self): """this is called periodically by LoopingCall to wake up the plugins that are not driven by user input""" for plugin in getPluginsByCapability("alert", self): self.logger.debug("Calling alert plugin %s" % plugin) reactor.callInThread(plugin.alert, client=self, log=self.pluginLogger)
def dispatchCommand(self, command, sender, originalMessage, twistedStanza, room, messageType): """Find plugins that can respond to the command or execute all the plugins responding to default""" self.logger.debug('Dispatching command %s' % command) """Command is meant for the bot""" self.logger.debug('This is our command') plg = command.pop(0).lower() args = [] for arg in command: args.append(arg.strip()) message = '' """Handle a few high level commands: help, mute, unmute""" if plg == 'help': self.logger.debug('Finding plugins with help') message = 'In a groupchat, all commands start with "%s:" or "!."\nIn private chat, simply type the command.\nThese are the commands available. For help on a command, try -h\n' % self.username pluginNames = [] for plugin in getPluginsByCapability('help', self): self.logger.debug('plugins %s has help' % plugin) try: pluginNames.append(plugin.name) except AttributeError: # baaad plugin, doesn't assign a name for itself pass message += ', '.join(pluginNames) self.sendMessage(message, sender, messageType) return if plg == 'default': # this is necessary to prevent users from invoking the catchall (default) plugins self.sendMessage('Unknown command. Try help.', sender, messageType) else: # find the plugins having the command name plugin_arguments = { 'args': args, 'command': 'something', 'client': self, 'message': originalMessage, 'messageType': messageType, 'sender': sender, 'twxml': twistedStanza, 'room': room, 'log': self.pluginLogger } foundCommand = False for plugin in getPluginsByCapability(plg, self): if not plugin.private: foundCommand = True self.logger.debug('Calling plugin %s with %s' % (plg, plugin_arguments)) plugin_arguments['command'] = plg if plugin.threadsafe: self.logger.debug('%s is marked as threadsafe' % plg) reactor.callInThread(plugin.call, **plugin_arguments) else: plugin.call(**plugin_arguments) else: self.logger.debug( 'Would have called plugins %s, but it is private' % plg) if not foundCommand: self.sendMessage('Unknown command. Try help.', sender, messageType)