Exemplo n.º 1
0
    def on_event(self, event, client):
        if ((not self.enabled or self.suspended_until > time.time()) and event.user.id not in self.owner_ids) \
                or not self.running:
            return

        watchers = self.modules.get_event_watchers()
        for w in watchers:
            w(event, client, self)
        
        if not (isinstance(event, MessagePosted) or isinstance(event, MessageEdited) or str(type(event)).find('Chatbot.MockEvent') > -1):
            return

        if event.user.id == self.client.get_me().id:
            return

        if isinstance(event, MessageEdited):
            message = Message(event.message.id, client)
        else:
            message = event.message
        content_source = message.content_source
        content = content_source

        fixed_font = is_fixed_font(content)
        if fixed_font:
            fixed_font = True
            content = fixed_font_to_normal(content)
        content = re.sub(r"^%s\s+" % self.prefix, self.prefix, content)
        content = re.sub("(^[^ \r\n]+)(\r?\n)", r"\1 ", content)
        if not fixed_font:
            stripped_content = re.sub(r"\s+", " ", content)
            stripped_content = stripped_content.strip()
        else:
            stripped_content = content
        parts = stripped_content.split(" ")
        if not parts[0].startswith(self.prefix):
            return

        cmd_args = stripped_content[len(self.prefix):]
        if self.requires_special_arg_parsing(cmd_args.split(" ")[0]):
            cmd_args = content[len(self.prefix):]
        output = self.command(cmd_args, message, event, 0)
        if output is not False and output is not None:
            output_with_reply = ":%i %s" % (message.id, output)
            if len(output_with_reply) > 500 and "\n" not in output_with_reply:
                message.reply("Output would be longer than 500 characters (the limit for single-line messages), so only the first 500 characters are posted now.")
                message.reply(output[:500 - (len(message.id) + 2)])
            else:
                message.reply(output, False)
Exemplo n.º 2
0
    def on_event(self, event, client):
        if (not self.enabled and event.user.id not in self.owner_ids) \
                or not self.running:
            return

        watchers = self.modules.get_event_watchers()
        for w in watchers:
            w(event, client, self)

        if not (isinstance(event, MessagePosted) or isinstance(event, MessageEdited)):
            return

        if event.user.id == self.client.get_me().id:
            return

        message = event.message
        content = Message(event.message.id, client).content_source

        fixed_font = is_fixed_font(content)
        if fixed_font:
            fixed_font = True
            content = fixed_font_to_normal(content)
        content = re.sub(r"^%s\s+" % self.prefix, self.prefix, content)
        content = re.sub("(^[^ \r\n]+)(\r?\n)", r"\1 ", content)
        if not fixed_font:
            stripped_content = re.sub(r"\s+", " ", content)
            stripped_content = stripped_content.strip()
        else:
            stripped_content = content
        parts = stripped_content.split(" ")
        if not parts[0].startswith(self.prefix):
            return

        cmd_args = stripped_content[len(self.prefix):]
        def concurrent_command(self, cmd_args1, message1, event1, content1):
            if self.requires_special_arg_parsing(cmd_args1.split(" ")[0]):
                cmd_args1 = content1[len(self.prefix):]
            output = self.command(cmd_args1, message1, event1)
            if output is not False and output is not None:
                output_with_reply = ":%i %s" % (message1.id, output)
                if len(output_with_reply) > 500 and "\n" not in output_with_reply:
                    message1.reply("Output would be longer than 500 characters (the limit for single-line messages), so only the first 500 characters are posted now.")
                    self.room.send_message(output_with_reply[:500])
                else:
                    self.room.send_message(output_with_reply, False)
        threading.Thread(target=concurrent_command, args=(self, cmd_args, message, event, content)).start()
Exemplo n.º 3
0
    def on_event(self, event, client):
        if (not self.enabled and event.user.id not in self.owner_ids) \
                or not self.running:
            return

        watchers = self.modules.get_event_watchers()
        for w in watchers:
            w(event, client, self)

        if not (isinstance(event, MessagePosted) or isinstance(event, MessageEdited)):
            return

        if event.user.id == self.client.get_me().id:
            return

        message = event.message
        content = Message(event.message.id, client).content_source

        fixed_font = is_fixed_font(content)
        if fixed_font:
            fixed_font = True
            content = fixed_font_to_normal(content)
        content = re.sub(r"^%s\s+" % self.prefix, self.prefix, content)
        content = re.sub("(^[^ \r\n]+)(\r?\n)", r"\1 ", content)
        if not fixed_font:
            stripped_content = re.sub(r"\s+", " ", content)
            stripped_content = stripped_content.strip()
        else:
            stripped_content = content
        parts = stripped_content.split(" ")
        if not parts[0].startswith(self.prefix):
            return

        cmd_args = stripped_content[len(self.prefix):]
        if self.requires_special_arg_parsing(cmd_args.split(" ")[0]):
            cmd_args = content[len(self.prefix):]
        output = self.get_output(cmd_args, message, event)
        if output is not False and output is not None:
            if len(output) > 500:
                message.reply("Output would be longer than 500 characters (the limit), so only the first 500 characters are posted now.")
                self.room.send_message(output[:500])
            else:
                message.reply(output)
Exemplo n.º 4
0
    def on_event(self, event, client):
        if (not self.enabled and event.user.id not in self.owner_ids) \
                or not self.running:
            return

        watchers = self.modules.get_event_watchers()
        for w in watchers:
            w(event, client, self)

        if not (isinstance(event, MessagePosted) or isinstance(event, MessageEdited)):
            return

        if event.user.id == self.client.get_me().id:
            return

        message = event.message
        content = Message(event.message.id, client).content_source

        fixed_font = is_fixed_font(content)
        if fixed_font:
            fixed_font = True
            content = fixed_font_to_normal(content)
        content = re.sub(r"^%s\s+" % self.prefix, self.prefix, content)
        content = re.sub("(^[^ \r\n]+)(\r?\n)", r"\1 ", content)
        if not fixed_font:
            stripped_content = re.sub(r"\s+", " ", content)
            stripped_content = stripped_content.strip()
        else:
            stripped_content = content
        parts = stripped_content.split(" ")
        
        pinged = parts[0].lower().startswith("@" + self.chatbot_name.lower())
        if not pinged and not parts[0].startswith(self.prefix):
            return

        cmd_args = stripped_content[len(self.prefix):] if not pinged else stripped_content[1+len(self.chatbot_name)+1:]

        #DEBUG
        if "--debug" in sys.argv:
            if pinged:
                print("Received command over ping,   stripped_content is '{0}'".format(stripped_content[1 + len(self.chatbot_name) + 1:])) # "@" + self.chatbot_name + " "
                print("Received command over ping,   content is          '{0}'".format(content[1 + len(self.chatbot_name) + 1:])) # "@" + self.chatbot_name + " "
            else:
                print("Received command over prefix, stripped_content is '{0}'".format(stripped_content[len(self.prefix):]))
                print("Received command over prefix, content is          '{0}'".format(content[len(self.prefix):]))
        #DEBUG END

        if self.requires_special_arg_parsing(cmd_args.split(" ")[0]):
            cmd_args = content[len(self.prefix):] if not pinged else content[1+len(self.chatbot_name)+1:]
        output = self.command(cmd_args, message, event)
        if output is not False and output is not None:
            output_with_reply = ":%i %s" % (message.id, output)
            if len(output_with_reply) > 500 and "\n" not in output_with_reply:
                fixed_formatted_reply = self.automatic_fixed_formatting(output_with_reply)
                if not fixed_formatted_reply:
                    #message.reply("Output would be longer than 500 characters (the limit for single-line messages), so only the first 500 characters are posted now.")
                    message.reply("Automatic formatting of output failed or is disabled, printing the first 500 characters (the limit for single-line messages) instead.")
                    self.room.send_message(output_with_reply[:500])
                else:
                    self.room.send_message(fixed_formatted_reply, False)
            else:
                self.room.send_message(output_with_reply, False)
Exemplo n.º 5
0
def test_fixedfontconverter():
    inp = "def test():\r\n        print(\"Test\")\r\n    test()"
    expected = """def test():{0}    print("Test"){0}test()"""\
        .format(os.linesep)
    assert fixed_font_to_normal(inp) == expected