def execute(self, message): """ @type message: IRCMessage """ # split on unescaped | chain = re.split(r'(?<!\\)\|', message.Parameters) response = None extraVars = {} for link in chain: link = link.strip() link = re.sub(r'\\\|', r'|', link) if response is not None: if hasattr(response, '__iter__'): return IRCResponse(ResponseType.Say, u"Chain Error: segment before '{}' returned a list".format(link), message.ReplyTo) link = link.replace('$output', response.Response) # replace $output with output of previous command extraVars.update(response.ExtraVars) for var, value in extraVars.iteritems(): link = re.sub(r'\$\b{}\b'.format(re.escape(var)), '{}'.format(value), link) else: # replace $output with empty string if previous command had no output # (or this is the first command in the chain, but for some reason has $output as a param) link = link.replace('$output', '') link = link.replace('$sender', message.User.Name) if message.Channel is not None: link = link.replace('$channel', message.Channel.Name) else: link = link.replace('$channel', message.User.Name) # build a new message out of this 'link' in the chain inputMessage = IRCMessage(message.Type, message.User.String, message.Channel, self.bot.commandChar + link.lstrip(), self.bot) inputMessage.chained = True # might be used at some point to tell commands they're being called from Chain if inputMessage.Command.lower() in self.bot.moduleHandler.mappedTriggers: response = self.bot.moduleHandler.mappedTriggers[inputMessage.Command.lower()].execute(inputMessage) else: return IRCResponse(ResponseType.Say, "'{0}' is not a recognized command trigger".format(inputMessage.Command), message.ReplyTo) if response.Response is not None: # limit response length (chains can get pretty large) response.Response = list(StringUtils.splitUTF8(response.Response.encode('utf-8'), 700))[0] response.Response = unicode(response.Response, 'utf-8') return response
def execute(self, message): """ @type message: IRCMessage """ # TODO: maybe do this in the command handler? # map triggers to commands so we can call them via dict lookup mappedTriggers = {} for command in self.bot.moduleHandler.commands.values(): for trigger in command.triggers: mappedTriggers[trigger] = command # split on unescaped | chain = re.split(r'(?<!\\)\|', message.Parameters) # rebuild the user string... TODO: this should probably be part of the User class if message.User.User is not None: userString = '{0}!{1}@{2}'.format(message.User.Name, message.User.User, message.User.Hostmask) else: userString = message.User.Name response = None for link in chain: if response is not None: link = link.replace('%output%', response.Response) # replace %output% with output of previous command else: # replace %output% with empty string if previous command had no output # (or this is the first command in the chain, but for some reason has %output% as a param) link = link.replace('%output%', '') # build a new message out of this 'link' in the chain inputMessage = IRCMessage(message.Type, userString, message.Channel, self.bot.commandChar + link.lstrip(), self.bot) inputMessage.chained = True # might be used at some point to tell commands they're being called from Chain if inputMessage.Command.lower() in mappedTriggers: response = mappedTriggers[inputMessage.Command.lower()].execute(inputMessage) else: return IRCResponse(ResponseType.Say, "'{0}' is not a recognized command trigger".format(inputMessage.Command), message.ReplyTo) if response.Response is not None: # limit response length (chains can get pretty large) response.Response = list(StringUtils.splitUTF8(response.Response.encode('utf-8'), 700))[0] response.Response = unicode(response.Response, 'utf-8') return response