示例#1
0
    def _importTriggers(self, message: IRCMessage) -> IRCResponse:
        """import <url> [<trigger(s)>] - import all triggers from the specified URLs, or only the listed triggers"""
        if len(message.parameterList) < 2:
            return IRCResponse(ResponseType.Say, "You didn't give a url to import from!", message.replyTo)

        if len(message.parameterList) > 2:
            onlyListed = True
            importList = message.parameterList[2:]
        else:
            onlyListed = False
            importList = None

        url = message.parameterList[1]
        try:
            response = self.bot.moduleHandler.runActionUntilValue('fetch-url', url)
        except ValueError:
            return IRCResponse(ResponseType.Say, f"'{url}' is not a valid URL", message.replyTo)
        if not response:
            return IRCResponse(ResponseType.Say, f"Failed to open page at {url}", message.replyTo)

        text = response.content
        text = UnicodeDammit(text).unicode_markup
        lines = text.splitlines()
        numTriggers = 0
        for lineNumber, line in enumerate(lines):
            # Skip over blank lines
            if line == "":
                continue
            splitLine = line.split()
            if splitLine[0].lower() != "{}trigger".format(self.bot.commandChar):
                notAlias = f"Line {lineNumber} at {url} does not begin with {self.bot.commandChar}trigger"
                return IRCResponse(ResponseType.Say, notAlias, message.replyTo)
            subCommand = splitLine[1].lower()
            if subCommand != "add":
                return IRCResponse(ResponseType.Say, f"Line {lineNumber} at {url} is not an add command", message.replyTo)

            triggerName = splitLine[2]
            triggerRegexTypePrefix = splitLine[3][0]
            triggerRegexType = self._regexTypePrefixToTypeName(triggerRegexTypePrefix)
            triggerRegex = splitLine[3][2:-1]
            triggerCommand = " ".join(splitLine[4:])

            # Skip over triggers that weren't listed, if any were listed
            if onlyListed and triggerName not in importList:
                continue

            self._actuallyAddTrigger(triggerName=triggerName, regex=triggerRegex, regexType=triggerRegexType, command=triggerCommand, enabled=True)
            numTriggers += 1

        importMessage = f"Imported {numTriggers} trigger(s) from {url}"
        return IRCResponse(ResponseType.Say, importMessage, message.replyTo)
示例#2
0
    def _import(self, message):
        """import <url> [<alias(es)>] - imports all aliases from the given address, or only the listed aliases"""
        if message.User.Name not in GlobalVars.admins:
            return IRCResponse(ResponseType.Say,
                               u"Only my admins may import aliases!",
                               message.ReplyTo)
        if len(message.ParameterList) < 2:
            return IRCResponse(ResponseType.Say,
                               u"You didn't give a url to import from!",
                               message.ReplyTo)

        if len(message.ParameterList) > 2:
            onlyListed = True
            importList = [alias.lower() for alias in message.ParameterList[2:]]
        else:
            onlyListed = False

        url = message.ParameterList[1]
        try:
            page = WebUtils.fetchURL(url)
        except ValueError:
            return IRCResponse(ResponseType.Say,
                               u"'{}' is not a valid URL".format(url),
                               message.ReplyTo)
        if page is None:
            return IRCResponse(ResponseType.Say,
                               u"Failed to open page at {}".format(url),
                               message.ReplyTo)

        text = page.body
        text = UnicodeDammit(text).unicode_markup
        lines = text.splitlines()
        numAliases = 0
        numHelpTexts = 0
        for lineNumber, line in enumerate(lines):
            # Skip over blank lines
            if line == u"":
                continue
            splitLine = line.split()
            if splitLine[0].lower() != u"{}alias".format(self.bot.commandChar):
                return IRCResponse(
                    ResponseType.Say,
                    u"Line {} at {} does not begin with {}alias".format(
                        lineNumber, url,
                        self.bot.commandChar), message.ReplyTo)
            subCommand = splitLine[1].lower()
            if subCommand not in [u"add", u"help"]:
                return IRCResponse(
                    ResponseType.Say,
                    u"Line {} at {} is not an add or help command".format(
                        lineNumber, url), message.ReplyTo)

            aliasName = splitLine[2].lower()
            aliasCommand = splitLine[3:]
            aliasCommand[0] = aliasCommand[0].lower()

            # Skip over aliases that weren't listed, if any were listed
            if onlyListed and aliasName not in importList:
                continue

            if subCommand == u"add":
                self._newAlias(aliasName, aliasCommand)
                numAliases += 1
            elif subCommand == u"help":
                aliasHelp = u" ".join(splitLine[3:])
                self.aliasHelpDict[aliasName] = aliasHelp
                numHelpTexts += 1

        return IRCResponse(
            ResponseType.Say,
            u"Imported {} alias(es) and {} help string(s) from {}".format(
                numAliases, numHelpTexts, url), message.ReplyTo)
示例#3
0
    def _import(self, message):
        """import <url> [<alias(es)>] - imports all aliases from the given address, or only the listed aliases"""
        if len(message.ParameterList) < 2:
            return IRCResponse(ResponseType.Say,
                               u"You didn't give a url to import from!",
                               message.ReplyTo)

        if len(message.ParameterList) > 2:
            onlyListed = True
            importList = [alias.lower() for alias in message.ParameterList[2:]]
        else:
            onlyListed = False

        url = message.ParameterList[1]
        try:
            page = self.bot.moduleHandler.runActionUntilValue('fetch-url', url)
        except ValueError:
            return IRCResponse(ResponseType.Say,
                               u"'{}' is not a valid URL".format(url),
                               message.ReplyTo)
        if page is None:
            return IRCResponse(ResponseType.Say,
                               u"Failed to open page at {}".format(url),
                               message.ReplyTo)

        text = page.body
        text = UnicodeDammit(text).unicode_markup
        lines = text.splitlines()
        numAliases = 0
        numHelpTexts = 0
        for lineNumber, line in enumerate(lines):
            # Skip over blank lines
            if line == u"":
                continue
            splitLine = line.split()
            if splitLine[0].lower() != u"{}alias".format(self.bot.commandChar):
                return IRCResponse(ResponseType.Say,
                                   u"Line {} at {} does not begin with {}alias".format(lineNumber,
                                                                                       url,
                                                                                       self.bot.commandChar),
                                   message.ReplyTo)
            subCommand = splitLine[1].lower()
            if subCommand not in [u"add", u"help"]:
                return IRCResponse(ResponseType.Say,
                                   u"Line {} at {} is not an add or help command".format(lineNumber, url),
                                   message.ReplyTo)

            aliasName = splitLine[2].lower()
            aliasCommand = splitLine[3:]
            aliasCommand[0] = aliasCommand[0].lower()

            # Skip over aliases that weren't listed, if any were listed
            if onlyListed and aliasName not in importList:
                continue

            if subCommand == u"add":
                self._newAlias(aliasName, u" ".join(aliasCommand))
                numAliases += 1
            elif subCommand == u"help":
                aliasHelp = u" ".join(splitLine[3:])
                self._setAliasHelp(aliasName, aliasHelp)
                numHelpTexts += 1

        self._syncAliases()

        return IRCResponse(ResponseType.Say,
                           u"Imported {} alias(es) and {} help string(s) from {}".format(numAliases,
                                                                                         numHelpTexts,
                                                                                         url),
                           message.ReplyTo)
示例#4
0
文件: Alias.py 项目: lunik1/DesertBot
    def _import(self, message):
        """import <url> [<alias(es)>] -\
        imports all aliases from the given address, or only the listed aliases"""
        if len(message.parameterList) < 2:
            return IRCResponse(ResponseType.Say,
                               "You didn't give a url to import from!",
                               message.replyTo)

        if len(message.parameterList) > 2:
            onlyListed = True
            importList = [alias.lower() for alias in message.parameterList[2:]]
        else:
            onlyListed = False
            importList = None

        url = message.parameterList[1]
        try:
            response = self.bot.moduleHandler.runActionUntilValue(
                'fetch-url', url)
        except ValueError:
            return IRCResponse(ResponseType.Say,
                               "'{}' is not a valid URL".format(url),
                               message.replyTo)
        if not response:
            return IRCResponse(ResponseType.Say,
                               "Failed to open page at {}".format(url),
                               message.replyTo)

        text = response.content
        text = UnicodeDammit(text).unicode_markup
        lines = text.splitlines()
        numAliases = 0
        numHelpTexts = 0
        for lineNumber, line in enumerate(lines):
            # Skip over blank lines
            if line == "":
                continue
            splitLine = line.split()
            if splitLine[0].lower() != "{}alias".format(self.bot.commandChar):
                notAlias = "Line {} at {} does not begin with {}alias".format(
                    lineNumber, url, self.bot.commandChar)
                return IRCResponse(ResponseType.Say, notAlias, message.replyTo)
            subCommand = splitLine[1].lower()
            if subCommand not in ["add", "help"]:
                return IRCResponse(
                    ResponseType.Say,
                    "Line {} at {} is not an add or help command".format(
                        lineNumber, url), message.replyTo)

            aliasName = splitLine[2].lower()
            aliasCommand = splitLine[3:]
            aliasCommand[0] = aliasCommand[0].lower()

            # Skip over aliases that weren't listed, if any were listed
            if onlyListed and aliasName not in importList:
                continue

            if subCommand == "add":
                self._newAlias(aliasName, " ".join(aliasCommand))
                numAliases += 1
            elif subCommand == "help":
                aliasHelp = " ".join(splitLine[3:])
                self._setAliasHelp(aliasName, aliasHelp)
                numHelpTexts += 1

        self._syncAliases()

        importMessage = "Imported {} alias(es) and {} help string(s) from {}".format(
            numAliases, numHelpTexts, url)
        return IRCResponse(ResponseType.Say, importMessage, message.replyTo)