Beispiel #1
0
    def add_autoresponse(self, **options):
        """Method for creating and editing autoresponses.
        Usage: !add autoresponse TRIGGER RESPONSE [options]
        Multiple options available:
        --whisper/--no-whisper
        """

        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            options, phrase = bot.autoresponse_manager.parse_autoresponse_arguments(message)

            if options is False:
                bot.whisper(source.username, 'Invalid autoresponse')
                return False

            options['added_by'] = source.id
            options['edited_by'] = source.id

            autoresponse, new_autoresponse = bot.autoresponse_manager.create_autoresponse(phrase, **options)

            if new_autoresponse is True:
                bot.whisper(source.username, 'Added your autoresponse (ID: {autoresponse.id})'.format(autoresponse=autoresponse))
                AdminLogManager.post('Banphrase added', source, phrase)
                return True

            autoresponse.set(**options)
            autoresponse.data.set(edited_by=options['edited_by'])
            DBManager.session_add_expunge(autoresponse)
            bot.autoresponse_manager.commit()
            bot.whisper(source.username, 'Updated your autoresponse (ID: {autoresponse.id}) with ({what})'.format(autoresponse=autoresponse, what=', '.join([key for key in options if key != 'added_by'])))
            AdminLogManager.post('Banphrase edited', source, phrase)
Beispiel #2
0
    def add_link_blacklist(self, **options):
        bot = options['bot']
        message = options['message']
        source = options['source']

        options, new_links = self.parse_link_blacklist_arguments(message)

        if new_links:
            parts = new_links.split(' ')
            try:
                for link in parts:
                    if len(link) > 1:
                        self.blacklist_url(link, **options)
                        AdminLogManager.post('Blacklist link added', source,
                                             link)
                bot.whisper(source.username, 'Successfully added your links')
                return True
            except:
                log.exception('Unhandled exception in add_link_blacklist')
                bot.whisper(source.username,
                            'Some error occurred while adding your links')
                return False
        else:
            bot.whisper(source.username, 'Usage: !add link blacklist LINK')
            return False
Beispiel #3
0
    def playsound_create(playsound_name: str, **options) -> ResponseReturnValue:
        # Create playsound
        playsound_name = PlaysoundModule.massage_name(playsound_name)

        if not PlaysoundModule.validate_name(playsound_name):
            return (
                {
                    "error": "Invalid Playsound name. The playsound name may only contain lowercase latin letters, 0-9, -, or _. No spaces :rage:"
                },
                400,
            )

        try:
            json_data = request.get_json()
            if not json_data:
                return {"error": "Missing json body"}, 400
            data = CreatePlaysoundSchema().load(json_data)
        except ValidationError as err:
            return {"error": f"Did not match schema: {json.dumps(err.messages)}"}, 400

        with DBManager.create_session_scope() as db_session:
            count = db_session.query(Playsound).filter(Playsound.name == playsound_name).count()
            if count >= 1:
                return "Playsound already exists", 400

            # the rest of the parameters are initialized with defaults
            playsound = Playsound(name=playsound_name, link=data.link)
            db_session.add(playsound)
            log_msg = f"The {playsound_name} playsound has been added"
            AdminLogManager.add_entry("Playsound added", options["user"], log_msg)

            return "OK", 200
Beispiel #4
0
    def remove_link_whitelist(self, bot, source, message, **rest):
        if not message:
            bot.whisper(source, "Usage: !remove link whitelist ID")
            return False

        id = None
        try:
            id = int(message)
        except ValueError:
            pass

        link = self.db_session.query(WhitelistedLink).filter_by(
            id=id).one_or_none()

        if link:
            self.whitelisted_links.remove(link)
            self.db_session.delete(link)
            self.db_session.commit()
        else:
            bot.whisper(source, "No link with the given id found")
            return False

        AdminLogManager.post("Whitelist link removed", source, link.domain)
        bot.whisper(
            source, f"Successfully removed whitelisted link with id {link.id}")
Beispiel #5
0
    def remove_link_whitelist(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            id = None
            try:
                id = int(message)
            except ValueError:
                pass

            link = self.db_session.query(WhitelistedLink).filter_by(
                id=id).one_or_none()

            if link:
                self.whitelisted_links.remove(link)
                self.db_session.delete(link)
                self.db_session.commit()
            else:
                bot.whisper(source.username, 'No link with the given id found')
                return False

            AdminLogManager.post('Whitelist link removed', source, link.domain)
            bot.whisper(
                source.username,
                'Successfully removed whitelisted link with id {0}'.format(
                    link.id))
        else:
            bot.whisper(source.username, 'Usage: !remove link whitelist ID')
            return False
Beispiel #6
0
    def post(self, row_id, **options):
        args = self.post_parser.parse_args()

        try:
            new_state = int(args["new_state"])
        except (ValueError, KeyError):
            return {"error": "Invalid `new_state` parameter."}, 400

        with DBManager.create_session_scope() as db_session:
            row = db_session.query(Module).filter_by(id=row_id).one_or_none()

            if not row:
                return {"error": "Module with this ID not found"}, 404

            if validate_module(row_id) is False:
                return {"error": "cannot modify module"}, 400

            row.enabled = True if new_state == 1 else False
            db_session.commit()
            payload = {"id": row.id, "new_state": row.enabled}
            AdminLogManager.post("Module toggled", options["user"],
                                 "Enabled" if row.enabled else "Disabled",
                                 row.id)
            SocketClientManager.send("module.update", payload)
            return {"success": "successful toggle", "new_state": new_state}
Beispiel #7
0
    def module_toggle(row_id: str, **options) -> ResponseReturnValue:
        try:
            json_data = request.get_json()
            if not json_data:
                return {"error": "Missing json body"}, 400
            data: ToggleState = ToggleStateSchema().load(json_data)
        except ValidationError as err:
            return {
                "error": f"Did not match schema: {json.dumps(err.messages)}"
            }, 400

        with DBManager.create_session_scope() as db_session:
            row = db_session.query(Module).filter_by(id=row_id).one_or_none()

            if not row:
                return {"error": "Module with this ID not found"}, 404

            if validate_module(row_id) is False:
                return {"error": "cannot modify module"}, 400

            row.enabled = data.new_state
            db_session.commit()
            payload = {"id": row.id, "new_state": data.new_state}
            AdminLogManager.post("Module toggled", options["user"],
                                 "Enabled" if row.enabled else "Disabled",
                                 row.id)
            SocketClientManager.send("module.update", payload)
            log.info(f"new state: {data} - {data.new_state}")
            return {
                "success": "successful toggle",
                "new_state": data.new_state
            }
Beispiel #8
0
    def remove_banphrase(**options):
        message = options["message"]
        bot = options["bot"]
        source = options["source"]

        if message:
            banphrase_id = None
            try:
                banphrase_id = int(message)
            except ValueError:
                pass

            banphrase = bot.banphrase_manager.find_match(
                message=message, banphrase_id=banphrase_id)

            if banphrase is None:
                bot.whisper(source.username,
                            "No banphrase with the given parameters found")
                return False

            AdminLogManager.post("Banphrase removed", source, banphrase.id,
                                 banphrase.phrase)
            bot.whisper(
                source.username,
                "Successfully removed banphrase with id {0}".format(
                    banphrase.id))
            bot.banphrase_manager.remove_banphrase(banphrase)
        else:
            bot.whisper(source.username,
                        "Usage: !remove banphrase (BANPHRASE_ID)")
            return False
Beispiel #9
0
    def permaban_command(bot, source, message, **rest) -> bool:
        if not message:
            return False

        username = message.split(" ")[0]
        with DBManager.create_session_scope() as db_session:
            user = User.find_by_user_input(db_session, username)
            if not user:
                bot.whisper(source, "No user with that name found.")
                return False

            if user.banned:
                bot.whisper(source, "User is already permabanned.")
                return False

            user.banned = True
            bot.ban(
                user,
                reason=
                f"User has been added to the {bot.bot_user.login} banlist. Contact a moderator level 1000 or higher for unban.",
            )
            log_msg = f"{user} has been permabanned"
            bot.whisper(source, log_msg)

            AdminLogManager.add_entry("Permaban added", source, log_msg)

        return True
Beispiel #10
0
    def put(self, playsound_name, **options):
        playsound_name = PlaysoundModule.massage_name(playsound_name)

        if not PlaysoundModule.validate_name(playsound_name):
            return (
                {
                    "error": "Invalid Playsound name. The playsound name may only contain lowercase latin letters, 0-9, -, or _. No spaces :rage:"
                },
                400,
            )

        post_parser = RequestParser()
        post_parser.add_argument("link", required=True)
        args = post_parser.parse_args()

        try:
            link = args["link"]
        except (ValueError, KeyError):
            return {"error": "Invalid `link` parameter."}, 400

        with DBManager.create_session_scope() as db_session:
            count = db_session.query(Playsound).filter(Playsound.name == playsound_name).count()
            if count >= 1:
                return "Playsound already exists", 400

            # the rest of the parameters are initialized with defaults
            playsound = Playsound(name=playsound_name, link=link)
            db_session.add(playsound)
            log_msg = f"The {playsound_name} playsound has been added"
            AdminLogManager.add_entry("Playsound added", options["user"], log_msg)

            return "OK", 200
Beispiel #11
0
    def generic_update(self, bot, source, message, field, api_fn):
        if not message:
            bot.say(f"You must specify a {field} to update to!")
            return

        try:
            api_fn(self.bot.streamer_user_id,
                   message,
                   authorization=bot.bot_token_manager)
        except HTTPError as e:
            if e.response.status_code == 401:
                bot.say(
                    f"Error (bot operator): The bot needs to be re-authenticated to be able to update the {field}."
                )
                return
            elif e.response.status_code == 403:
                bot.say(
                    f"Error: The bot is not a channel editor and was not able to update the {field}."
                )
                return
            else:
                raise e

        log_msg = f'{source} updated the {field} to "{message}"'
        bot.say(log_msg)
        AdminLogManager.add_entry(f"{field.capitalize()} set", source, log_msg)
Beispiel #12
0
    def remove_alias(bot, source, message, event, args):
        """Dispatch method for removing aliases from a command.
        Usage: !remove alias EXISTING_ALIAS_1 EXISTING_ALIAS_2"""
        if message:
            aliases = re.split(r"\|| ", message.lower())
            log.info(aliases)
            if len(aliases) < 1:
                bot.whisper(source.username,
                            "Usage: !remove alias EXISTINGALIAS")
                return False

            num_removed = 0
            commands_not_found = []
            for alias in aliases:
                if alias not in bot.commands:
                    commands_not_found.append(alias)
                    continue

                command = bot.commands[alias]

                # error out on commands that are not from the DB, e.g. module commands like !8ball that cannot have
                # aliases registered. (command.command and command.data are None on those commands)
                if command.data is None or command.command is None:
                    bot.whisper(
                        source.username,
                        "That command cannot have aliases removed from.")
                    return False

                current_aliases = command.command.split("|")
                current_aliases.remove(alias)

                if len(current_aliases) == 0:
                    bot.whisper(
                        source.username,
                        "{0} is the only remaining alias for this command and can't be removed."
                        .format(alias),
                    )
                    continue

                new_aliases = "|".join(current_aliases)
                bot.commands.edit_command(command, command=new_aliases)

                num_removed += 1
                del bot.commands[alias]
                log_msg = "The alias {0} has been removed from {1}".format(
                    alias,
                    new_aliases.split("|")[0])
                AdminLogManager.add_entry("Alias removed", source, log_msg)

            whisper_str = ""
            if num_removed > 0:
                whisper_str = "Successfully removed {0} aliases.".format(
                    num_removed)
            if len(commands_not_found) > 0:
                whisper_str += " Aliases {0} not found".format(
                    ", ".join(commands_not_found))
            if len(whisper_str) > 0:
                bot.whisper(source.username, whisper_str)
        else:
            bot.whisper(source.username, "Usage: !remove alias EXISTINGALIAS")
Beispiel #13
0
    def remove_link_blacklist(self, bot, source, message, **rest) -> bool:
        if self.db_session is None:
            raise ValueError("LinkChecker module db not initialized")

        if not message:
            bot.whisper(source, "Usage: !remove link blacklist ID")
            return False

        id = None
        try:
            id = int(message)
        except ValueError:
            pass

        link = self.db_session.query(BlacklistedLink).filter_by(
            id=id).one_or_none()

        if link:
            self.blacklisted_links.remove(link)
            self.db_session.delete(link)
            self.db_session.commit()
        else:
            bot.whisper(source, "No link with the given id found")
            return False

        AdminLogManager.post("Blacklist link removed", source, link.domain)
        bot.whisper(
            source, f"Successfully removed blacklisted link with id {link.id}")

        return True
Beispiel #14
0
    def level(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            msg_args = message.split(' ')
            if len(msg_args) > 1:
                username = msg_args[0].lower()
                new_level = int(msg_args[1])
                if new_level >= source.level:
                    bot.whisper(
                        source.username,
                        'You cannot promote someone to the same or higher level as you ({0}).'
                        .format(source.level))
                    return False

                # We create the user if the user didn't already exist in the database.
                with bot.users.get_user_context(username) as user:
                    old_level = user.level
                    user.level = new_level

                    log_msg = '{}\'s user level changed from {} to {}'.format(
                        user.username_raw, old_level, new_level)

                    bot.whisper(source.username, log_msg)

                    AdminLogManager.add_entry('Userlevel edited', source,
                                              log_msg)

                    return True

        bot.whisper(source.username, 'Usage: !level USERNAME NEW_LEVEL')
        return False
Beispiel #15
0
    def unpermaban_command(self, bot, source, message, **rest):
        if not message:
            return

        username = message.split(" ")[0]
        with DBManager.create_session_scope() as db_session:
            user = User.find_by_user_input(db_session, username)
            if not user:
                bot.whisper(source, "No user with that name found.")
                return False

            if user.banned is False:
                bot.whisper(source, "User is not permabanned.")
                return False

            user.banned = False
            log_msg = f"{user} is no longer permabanned"
            bot.whisper(source, log_msg)

            AdminLogManager.add_entry("Permaban remove", source, log_msg)

            if self.settings["unban_from_chat"] is True:
                bot.unban(user)

                if self.settings["enable_send_timeout"] is True:
                    bot.timeout(
                        user,
                        1,
                        self.settings["timeout_reason"].format(source=source),
                        once=True)
Beispiel #16
0
    def remove_link_blacklist(self, **options):
        message = options["message"]
        bot = options["bot"]
        source = options["source"]

        if message:
            id = None
            try:
                id = int(message)
            except ValueError:
                pass

            link = self.db_session.query(BlacklistedLink).filter_by(
                id=id).one_or_none()

            if link:
                self.blacklisted_links.remove(link)
                self.db_session.delete(link)
                self.db_session.commit()
            else:
                bot.whisper(source.username, "No link with the given id found")
                return False

            AdminLogManager.post("Blacklist link removed", source, link.domain)
            bot.whisper(
                source.username,
                "Successfully removed blacklisted link with id {0}".format(
                    link.id))
        else:
            bot.whisper(source.username, "Usage: !remove link blacklist ID")
            return False
Beispiel #17
0
    def add_link_blacklist(self, **options):
        bot = options["bot"]
        message = options["message"]
        source = options["source"]

        options, new_links = self.parse_link_blacklist_arguments(message)

        if new_links:
            parts = new_links.split(" ")
            try:
                for link in parts:
                    if len(link) > 1:
                        self.blacklist_url(link, **options)
                        AdminLogManager.post("Blacklist link added", source,
                                             link)
                bot.whisper(source.username, "Successfully added your links")
                return True
            except:
                log.exception("Unhandled exception in add_link_blacklist")
                bot.whisper(source.username,
                            "Some error occurred while adding your links")
                return False
        else:
            bot.whisper(source.username, "Usage: !add link blacklist LINK")
            return False
Beispiel #18
0
    def level(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            msg_args = message.split(' ')
            if len(msg_args) > 1:
                username = msg_args[0].lower()
                new_level = int(msg_args[1])
                if new_level >= source.level:
                    bot.whisper(source.username, 'You cannot promote someone to the same or higher level as you ({0}).'.format(source.level))
                    return False

                # We create the user if the user didn't already exist in the database.
                with bot.users.get_user_context(username) as user:
                    old_level = user.level
                    user.level = new_level

                    log_msg = '{}\'s user level changed from {} to {}'.format(
                            user.username_raw,
                            old_level,
                            new_level)

                    bot.whisper(source.username, log_msg)

                    AdminLogManager.add_entry('Userlevel edited', source, log_msg)

                    return True

        bot.whisper(source.username, 'Usage: !level USERNAME NEW_LEVEL')
        return False
Beispiel #19
0
    def add_banphrase(bot, source, message, **rest):
        """Method for creating and editing banphrases.
        Usage: !add banphrase BANPHRASE [options]
        Multiple options available:
        --length LENGTH
        --perma/--no-perma
        --notify/--no-notify
        """

        if message:
            options, phrase = bot.banphrase_manager.parse_banphrase_arguments(message)

            if options is False:
                bot.whisper(source, "Invalid banphrase")
                return False

            options["added_by"] = source.id
            options["edited_by"] = source.id

            banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(phrase, **options)

            if new_banphrase is True:
                bot.whisper(source, f"Added your banphrase (ID: {banphrase.id})")
                AdminLogManager.post("Banphrase added", source, banphrase.id, banphrase.phrase)
                return True

            banphrase.set(**options)
            banphrase.data.set(edited_by=options["edited_by"])
            DBManager.session_add_expunge(banphrase)
            bot.banphrase_manager.commit()
            bot.whisper(
                source,
                f"Updated your banphrase (ID: {banphrase.id}) with ({', '.join([key for key in options if key != 'added_by'])})",
            )
            AdminLogManager.post("Banphrase edited", source, banphrase.id, banphrase.phrase)
Beispiel #20
0
    def remove_banphrase(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            id = None
            try:
                id = int(message)
            except ValueError:
                pass

            banphrase = bot.banphrase_manager.find_match(message=message,
                                                         id=id)

            if banphrase is None:
                bot.whisper(source.username,
                            'No banphrase with the given parameters found')
                return False

            AdminLogManager.post('Banphrase removed', source, banphrase.phrase)
            bot.whisper(
                source.username,
                'Successfully removed banphrase with id {0}'.format(
                    banphrase.id))
            bot.banphrase_manager.remove_banphrase(banphrase)
        else:
            bot.whisper(source.username,
                        'Usage: !remove banphrase (BANPHRASE_ID)')
            return False
Beispiel #21
0
    def post(self, row_id, **options):
        args = self.post_parser.parse_args()

        try:
            new_state = int(args['new_state'])
        except (ValueError, KeyError):
            return {'error': 'Invalid `new_state` parameter.'}, 400

        with DBManager.create_session_scope() as db_session:
            row = db_session.query(Module).filter_by(id=row_id).one_or_none()

            if not row:
                return {
                        'error': 'Module with this ID not found'
                        }, 404

            if validate_module(row_id) is False:
                return {'error': 'cannot modify module'}, 400

            row.enabled = True if new_state == 1 else False
            db_session.commit()
            payload = {
                    'id': row.id,
                    'new_state': row.enabled,
                    }
            AdminLogManager.post('Module toggled',
                    options['user'],
                    'Enabled' if row.enabled else 'Disabled',
                    row.id)
            SocketClientManager.send('module.update', payload)
            return {'success': 'successful toggle', 'new_state': new_state}
Beispiel #22
0
    def timer_toggle(timer_id: int, **options) -> ResponseReturnValue:
        try:
            json_data = request.get_json()
            if not json_data:
                return {"error": "Missing json body"}, 400
            data: ToggleState = ToggleStateSchema().load(json_data)
        except ValidationError as err:
            return {
                "error": f"Did not match schema: {json.dumps(err.messages)}"
            }, 400

        with DBManager.create_session_scope() as db_session:
            row = db_session.query(Timer).filter_by(id=timer_id).one_or_none()

            if not row:
                return {"error": "Timer with this ID not found"}, 404

            row.enabled = data.new_state
            db_session.commit()
            payload = {"id": row.id, "new_state": data.new_state}
            AdminLogManager.post("Timer toggled", options["user"],
                                 "Enabled" if data.new_state else "Disabled",
                                 row.name)
            SocketClientManager.send("timer.update", payload)
            return {
                "success": "successful toggle",
                "new_state": data.new_state
            }
Beispiel #23
0
    def remove_link_whitelist(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            id = None
            try:
                id = int(message)
            except ValueError:
                pass

            link = self.db_session.query(WhitelistedLink).filter_by(id=id).one_or_none()

            if link:
                self.whitelisted_links.remove(link)
                self.db_session.delete(link)
                self.db_session.commit()
            else:
                bot.whisper(source.username, 'No link with the given id found')
                return False

            AdminLogManager.post('Whitelist link removed', source, link.domain)
            bot.whisper(source.username, 'Successfully removed whitelisted link with id {0}'.format(link.id))
        else:
            bot.whisper(source.username, 'Usage: !remove link whitelist ID')
            return False
Beispiel #24
0
    def modules_edit(module_id, **options):
        module_manager = ModuleManager(None).load(do_reload=False)
        current_module = find(lambda m: m.ID == module_id, module_manager.all_modules)

        if current_module is None:
            return render_template('admin/module_404.html'), 404

        sub_modules = []
        for module in module_manager.all_modules:
            module.db_module = None

        with DBManager.create_session_scope() as db_session:
            for db_module in db_session.query(Module):
                module = find(lambda m: m.ID == db_module.id, module_manager.all_modules)
                if module:
                    module.db_module = db_module
                    if module.PARENT_MODULE == current_module.__class__:
                        sub_modules.append(module)

            if current_module.db_module is None:
                return render_template('admin/module_404.html'), 404

            if request.method == 'POST':
                form_values = {key: value for key, value in request.form.items()}
                res = current_module.parse_settings(**form_values)
                if res is False:
                    return render_template('admin/module_404.html'), 404

                current_module.db_module.settings = json.dumps(res)
                db_session.commit()

                settings = None
                try:
                    settings = json.loads(current_module.db_module.settings)
                except (TypeError, ValueError):
                    pass
                current_module.load(settings=settings)

                payload = {
                        'id': current_module.db_module.id,
                        }

                SocketClientManager.send('module.update', payload)

                AdminLogManager.post('Module edited', options['user'], current_module.NAME)

                return render_template('admin/configure_module.html',
                        module=current_module,
                        sub_modules=sub_modules)
            else:
                settings = None
                try:
                    settings = json.loads(current_module.db_module.settings)
                except (TypeError, ValueError):
                    pass
                current_module.load(settings=settings)

                return render_template('admin/configure_module.html',
                        module=current_module,
                        sub_modules=sub_modules)
Beispiel #25
0
    def set_title(bot, source, message, event, args):
        # XXX: This should be a module
        if message:
            bot.twitchapi.set_title(bot.streamer, message)
            log_msg = '{0} updated the title to "{1}"'.format(source.username_raw, message)
            bot.say(log_msg)

            AdminLogManager.add_entry('Title set', source, log_msg)
Beispiel #26
0
    def set_title(bot, source, message, event, args):
        # XXX: This should be a module
        if message:
            bot.twitchapi.set_title(bot.streamer, message)
            log_msg = '{0} updated the title to "{1}"'.format(source.username_raw, message)
            bot.say(log_msg)

            AdminLogManager.add_entry('Title set', source, log_msg)
Beispiel #27
0
    def add_command(bot, source, message, event, args):
        """Dispatch method for creating commands.
        Usage: !add command ALIAS [options] RESPONSE
        See pajbot/managers/command.py parse_command_arguments for available options
        """

        if not message:
            return False

        # Make sure we got both an alias and a response
        message_parts = message.split()
        if len(message_parts) < 2:
            bot.send_message_to_user(
                source,
                "Usage: !add command ALIAS [options] RESPONSE",
                event,
                method="whisper")
            return False

        options, response = bot.commands.parse_command_arguments(
            message_parts[1:])

        if options is False:
            bot.send_message_to_user(source,
                                     "Invalid command",
                                     event,
                                     method="whisper")
            return False

        options["added_by"] = source.id

        alias_str = message_parts[0].replace("!", "").lower()
        action_type: str = options.get("action_type", "say")
        if response.startswith("/me") or response.startswith(".me"):
            action_type = "me"
            response = " ".join(response.split(" ")[1:])
        action = {"type": action_type, "message": response}

        command, new_command, alias_matched = bot.commands.create_command(
            alias_str, action=action, **options)
        if new_command is True:
            bot.send_message_to_user(source,
                                     f"Added your command (ID: {command.id})",
                                     event,
                                     method="whisper")

            log_msg = f"The !{command.command.split('|')[0]} command has been created"
            AdminLogManager.add_entry("Command created", source, log_msg)
            return True

        # At least one alias is already in use, notify the user to use !edit command instead
        bot.send_message_to_user(
            source,
            f"The alias {alias_matched} is already in use. To edit that command, use !edit command instead of !add command.",
            event,
            method="whisper",
        )
        return False
Beispiel #28
0
    def add_command(bot, source, message, event, args):
        """Dispatch method for creating commands.
        Usage: !add command ALIAS [options] RESPONSE
        Multiple options available:
        --whisper/--no-whisper
        --reply/--no-reply
        --modonly/--no-modonly
        --cd CD
        --usercd USERCD
        --level LEVEL
        --cost COST
        """

        if message:
            # Make sure we got both an alias and a response
            message_parts = message.split()
            if len(message_parts) < 2:
                bot.whisper(source.username, 'Usage: !add command ALIAS [options] RESPONSE')
                return False

            options, response = bot.commands.parse_command_arguments(message_parts[1:])

            options['added_by'] = source.id

            if options is False:
                bot.whisper(source.username, 'Invalid command')
                return False

            alias_str = message_parts[0].replace('!', '').lower()
            type = 'say'
            if options['whisper'] is True:
                type = 'whisper'
            elif options['reply'] is True:
                type = 'reply'
            elif response.startswith('/me') or response.startswith('.me'):
                type = 'me'
                response = ' '.join(response.split(' ')[1:])
            elif options['whisper'] is False or options['reply'] is False:
                type = 'say'
            action = {
                'type': type,
                'message': response,
            }

            command, new_command, alias_matched = bot.commands.create_command(alias_str, action=action, **options)
            if new_command is True:
                bot.whisper(source.username, 'Added your command (ID: {command.id})'.format(command=command))

                log_msg = 'The !{} command has been created'.format(command.command.split('|')[0])
                AdminLogManager.add_entry('Command created',
                        source,
                        log_msg)
                return True

            # At least one alias is already in use, notify the user to use !edit command instead
            bot.whisper(source.username, 'The alias {} is already in use. To edit that command, use !edit command instead of !add command.'.format(alias_matched))
            return False
Beispiel #29
0
    def add_command(bot, source, message, event, args):
        """Dispatch method for creating commands.
        Usage: !add command ALIAS [options] RESPONSE
        Multiple options available:
        --whisper/--no-whisper
        --reply/--no-reply
        --modonly/--no-modonly
        --cd CD
        --usercd USERCD
        --level LEVEL
        --cost COST
        """

        if message:
            # Make sure we got both an alias and a response
            message_parts = message.split()
            if len(message_parts) < 2:
                bot.whisper(source.username, 'Usage: !add command ALIAS [options] RESPONSE')
                return False

            options, response = bot.commands.parse_command_arguments(message_parts[1:])

            options['added_by'] = source.id

            if options is False:
                bot.whisper(source.username, 'Invalid command')
                return False

            alias_str = message_parts[0].replace('!', '').lower()
            type = 'say'
            if options['whisper'] is True:
                type = 'whisper'
            elif options['reply'] is True:
                type = 'reply'
            elif response.startswith('/me') or response.startswith('.me'):
                type = 'me'
                response = ' '.join(response.split(' ')[1:])
            elif options['whisper'] is False or options['reply'] is False:
                type = 'say'
            action = {
                'type': type,
                'message': response,
            }

            command, new_command, alias_matched = bot.commands.create_command(alias_str, action=action, **options)
            if new_command is True:
                bot.whisper(source.username, 'Added your command (ID: {command.id})'.format(command=command))

                log_msg = 'The !{} command has been created'.format(command.command.split('|')[0])
                AdminLogManager.add_entry('Command created',
                        source,
                        log_msg)
                return True

            # At least one alias is already in use, notify the user to use !edit command instead
            bot.whisper(source.username, 'The alias {} is already in use. To edit that command, use !edit command instead of !add command.'.format(alias_matched))
            return False
Beispiel #30
0
 def get(self, timer_id, **options):
     with DBManager.create_session_scope() as db_session:
         timer = db_session.query(Timer).filter_by(id=timer_id).one_or_none()
         if timer is None:
             return {'error': 'Invalid timer ID'}, 404
         AdminLogManager.post('Timer removed', options['user'], timer.name)
         db_session.delete(timer)
         SocketClientManager.send('timer.remove', {'id': timer.id})
         return {'success': 'good job'}
Beispiel #31
0
 def get(self, timer_id, **options):
     with DBManager.create_session_scope() as db_session:
         timer = db_session.query(Timer).filter_by(id=timer_id).one_or_none()
         if timer is None:
             return {"error": "Invalid timer ID"}, 404
         AdminLogManager.post("Timer removed", options["user"], timer.name)
         db_session.delete(timer)
         SocketClientManager.send("timer.remove", {"id": timer.id})
         return {"success": "good job"}
Beispiel #32
0
    def add_alias(bot, source, message, event, args):
        """Dispatch method for adding aliases to already-existing commands.
        Usage: !add alias EXISTING_ALIAS NEW_ALIAS_1 NEW_ALIAS_2 ...
        """

        if message:
            message = message.replace("!", "").lower()
            # Make sure we got both an existing alias and at least one new alias
            message_parts = message.split()
            if len(message_parts) < 2:
                bot.whisper(source, "Usage: !add alias existingalias newalias")
                return False

            existing_alias = message_parts[0]
            new_aliases = re.split(r"\|| ", " ".join(message_parts[1:]))
            added_aliases = []
            already_used_aliases = []

            if existing_alias not in bot.commands:
                bot.whisper(source,
                            f'No command called "{existing_alias}" found')
                return False

            command = bot.commands[existing_alias]

            # error out on commands that are not from the DB, e.g. module commands like !8ball that cannot have
            # aliases registered. (command.command and command.data are None on those commands)
            if command.data is None or command.command is None:
                bot.whisper(source,
                            "That command cannot have aliases added to.")
                return False

            for alias in set(new_aliases):
                if alias in bot.commands:
                    already_used_aliases.append(alias)
                else:
                    added_aliases.append(alias)
                    bot.commands[alias] = command

            if len(added_aliases) > 0:
                new_aliases = f"{command.command}|{'|'.join(added_aliases)}"
                bot.commands.edit_command(command, command=new_aliases)

                bot.whisper(
                    source,
                    f"Successfully added the aliases {', '.join(added_aliases)} to {existing_alias}"
                )
                log_msg = f"The aliases {', '.join(added_aliases)} has been added to {existing_alias}"
                AdminLogManager.add_entry("Alias added", source, log_msg)
            if len(already_used_aliases) > 0:
                bot.whisper(
                    source,
                    f"The following aliases were already in use: {', '.join(already_used_aliases)}"
                )
        else:
            bot.whisper(source, "Usage: !add alias existingalias newalias")
Beispiel #33
0
    def add_alias(bot, source, message, event, args):
        """Dispatch method for adding aliases to already-existing commands.
        Usage: !add alias EXISTING_ALIAS NEW_ALIAS_1 NEW_ALIAS_2 ...
        """

        if message:
            message = message.replace("!", "").lower()
            # Make sure we got both an existing alias and at least one new alias
            message_parts = message.split()
            if len(message_parts) < 2:
                bot.whisper(source.username,
                            "Usage: !add alias existingalias newalias")
                return False

            existing_alias = message_parts[0]
            new_aliases = re.split(r"\|| ", " ".join(message_parts[1:]))
            added_aliases = []
            already_used_aliases = []

            if existing_alias not in bot.commands:
                bot.whisper(
                    source.username,
                    'No command called "{0}" found'.format(existing_alias))
                return False

            command = bot.commands[existing_alias]

            for alias in set(new_aliases):
                if alias in bot.commands:
                    already_used_aliases.append(alias)
                else:
                    added_aliases.append(alias)
                    bot.commands[alias] = command

            if len(added_aliases) > 0:
                new_aliases = "{}|{}".format(command.command,
                                             "|".join(added_aliases))
                bot.commands.edit_command(command, command=new_aliases)

                bot.whisper(
                    source.username,
                    "Successfully added the aliases {0} to {1}".format(
                        ", ".join(added_aliases), existing_alias),
                )
                log_msg = "The aliases {0} has been added to {1}".format(
                    ", ".join(added_aliases), existing_alias)
                AdminLogManager.add_entry("Alias added", source, log_msg)
            if len(already_used_aliases) > 0:
                bot.whisper(
                    source.username,
                    "The following aliases were already in use: {0}".format(
                        ", ".join(already_used_aliases)),
                )
        else:
            bot.whisper(source.username,
                        "Usage: !add alias existingalias newalias")
Beispiel #34
0
 def get(self, banphrase_id, **options):
     with DBManager.create_session_scope() as db_session:
         banphrase = db_session.query(Banphrase).filter_by(id=banphrase_id).one_or_none()
         if banphrase is None:
             return {'error': 'Invalid banphrase ID'}, 404
         AdminLogManager.post('Banphrase removed', options['user'], banphrase.phrase)
         db_session.delete(banphrase)
         db_session.delete(banphrase.data)
         SocketClientManager.send('banphrase.remove', {'id': banphrase.id})
         return {'success': 'good job'}, 200
Beispiel #35
0
 def banphrases_remove(banphrase_id, **options):
     with DBManager.create_session_scope() as db_session:
         banphrase = db_session.query(Banphrase).filter_by(id=banphrase_id).one_or_none()
         if banphrase is None:
             return {"error": "Invalid banphrase ID"}, 404
         AdminLogManager.post("Banphrase removed", options["user"], banphrase.id, banphrase.phrase)
         db_session.delete(banphrase)
         db_session.delete(banphrase.data)
         SocketClientManager.send("banphrase.remove", {"id": banphrase.id})
         return {"success": "good job"}, 200
Beispiel #36
0
    def add_link_whitelist(self, **options):
        bot = options['bot']
        message = options['message']
        source = options['source']

        parts = message.split(' ')
        try:
            for link in parts:
                self.whitelist_url(link)
                AdminLogManager.post('Whitelist link added', source, link)
        except:
            log.exception('Unhandled exception in add_link')
            bot.whisper(source.username, 'Some error occurred white adding your links')
            return False

        bot.whisper(source.username, 'Successfully added your links')
Beispiel #37
0
    def remove_alias(bot, source, message, event, args):
        """Dispatch method for removing aliases from a command.
        Usage: !remove alias EXISTING_ALIAS_1 EXISTING_ALIAS_2"""
        if message:
            aliases = re.split('\|| ', message.lower())
            log.info(aliases)
            if len(aliases) < 1:
                bot.whisper(source.username, 'Usage: !remove alias EXISTINGALIAS')
                return False

            num_removed = 0
            commands_not_found = []
            for alias in aliases:
                if alias not in bot.commands:
                    commands_not_found.append(alias)
                    continue

                command = bot.commands[alias]

                current_aliases = command.command.split('|')
                current_aliases.remove(alias)

                if len(current_aliases) == 0:
                    bot.whisper(source.username, "{0} is the only remaining alias for this command and can't be removed.".format(alias))
                    continue

                new_aliases = '|'.join(current_aliases)
                bot.commands.edit_command(command, command=new_aliases)

                num_removed += 1
                del bot.commands[alias]
                log_msg = 'The alias {0} has been removed from {1}'.format(alias, new_aliases.split('|')[0])
                AdminLogManager.add_entry('Alias removed',
                        source,
                        log_msg)

            whisper_str = ''
            if num_removed > 0:
                whisper_str = 'Successfully removed {0} aliases.'.format(num_removed)
            if len(commands_not_found) > 0:
                whisper_str += ' Aliases {0} not found'.format(', '.join(commands_not_found))
            if len(whisper_str) > 0:
                bot.whisper(source.username, whisper_str)
        else:
            bot.whisper(source.username, 'Usage: !remove alias EXISTINGALIAS')
Beispiel #38
0
    def permaban_command(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            username = message.split(' ')[0].strip().lower()
            with bot.users.get_user_context(username) as user:
                if user.banned:
                    bot.whisper(source.username, 'User is already permabanned.')
                    return False

                user.banned = True
                message = message.lower()
                log_msg = '{} has been permabanned'.format(user.username_raw)
                bot.whisper(source.username, log_msg)

                AdminLogManager.add_entry('Permaban added', source, log_msg)
Beispiel #39
0
    def get(self, command_id, **options):
        with DBManager.create_session_scope() as db_session:
            command = db_session.query(Command).filter_by(id=command_id).one_or_none()
            if command is None:
                return {'error': 'Invalid command ID'}, 404
            if command.level > options['user'].level:
                return {'error': 'Unauthorized'}, 403
            log_msg = 'The !{} command has been removed'.format(command.command.split('|')[0])
            AdminLogManager.add_entry('Command removed',
                    options['user'],
                    log_msg)
            db_session.delete(command.data)
            db_session.delete(command)

        if SocketClientManager.send('command.remove', {'command_id': command_id}) is True:
            return {'success': 'good job'}, 200
        else:
            return {'error': 'could not push update'}, 500
Beispiel #40
0
    def add_alias(bot, source, message, event, args):
        """Dispatch method for adding aliases to already-existing commands.
        Usage: !add alias EXISTING_ALIAS NEW_ALIAS_1 NEW_ALIAS_2 ...
        """

        if message:
            message = message.replace('!', '').lower()
            # Make sure we got both an existing alias and at least one new alias
            message_parts = message.split()
            if len(message_parts) < 2:
                bot.whisper(source.username, 'Usage: !add alias existingalias newalias')
                return False

            existing_alias = message_parts[0]
            new_aliases = re.split('\|| ', ' '.join(message_parts[1:]))
            added_aliases = []
            already_used_aliases = []

            if existing_alias not in bot.commands:
                bot.whisper(source.username, 'No command called "{0}" found'.format(existing_alias))
                return False

            command = bot.commands[existing_alias]

            for alias in set(new_aliases):
                if alias in bot.commands:
                    already_used_aliases.append(alias)
                else:
                    added_aliases.append(alias)
                    bot.commands[alias] = command

            if len(added_aliases) > 0:
                new_aliases = '{}|{}'.format(command.command, '|'.join(added_aliases))
                bot.commands.edit_command(command, command=new_aliases)

                bot.whisper(source.username, 'Successfully added the aliases {0} to {1}'.format(', '.join(added_aliases), existing_alias))
                log_msg = 'The aliases {0} has been added to {1}'.format(', '.join(added_aliases), existing_alias)
                AdminLogManager.add_entry('Alias added',
                        source,
                        log_msg)
            if len(already_used_aliases) > 0:
                bot.whisper(source.username, 'The following aliases were already in use: {0}'.format(', '.join(already_used_aliases)))
        else:
            bot.whisper(source.username, 'Usage: !add alias existingalias newalias')
Beispiel #41
0
    def home(**options):
        latest_logs_raw = AdminLogManager.get_entries()
        cached_users = {}
        with DBManager.create_session_scope() as db_session:
            latest_logs = []
            for log in latest_logs_raw:
                log['user'] = cached_users[log['user_id']] if log['user_id'] in cached_users else db_session.query(User).filter_by(id=log['user_id']).one_or_none()
                latest_logs.append(log)

            return render_template('admin/home.html',
                    latest_logs=latest_logs)
Beispiel #42
0
    def remove_command(bot, source, message, event, args):
        if message:
            id = None
            command = None
            try:
                id = int(message)
            except Exception:
                pass

            if id is None:
                potential_cmd = ''.join(message.split(' ')[:1]).lower().replace('!', '')
                if potential_cmd in bot.commands:
                    command = bot.commands[potential_cmd]
                    log.info('got command: {0}'.format(command))
            else:
                for key, check_command in bot.commands.items():
                    if check_command.id == id:
                        command = check_command
                        break

            if command is None:
                bot.whisper(source.username, 'No command with the given parameters found')
                return False

            if command.id == -1:
                bot.whisper(source.username, 'That command is an internal command, it cannot be removed.')
                return False

            if source.level < 2000:
                if command.action is not None and not command.action.type == 'message':
                    bot.whisper(source.username, 'That command is not a normal command, it cannot be removed by you.')
                    return False

            bot.whisper(source.username, 'Successfully removed command with id {0}'.format(command.id))
            log_msg = 'The !{} command has been removed'.format(command.command.split('|')[0])
            AdminLogManager.add_entry('Command removed',
                    source,
                    log_msg)
            bot.commands.remove_command(command)
        else:
            bot.whisper(source.username, 'Usage: !remove command (COMMAND_ID|COMMAND_ALIAS)')
Beispiel #43
0
    def unpermaban_command(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            username = message.split(' ')[0].strip().lower()
            with bot.users.find_context(username) as user:
                if not user:
                    bot.whisper(source.username, 'No user with that name found.')
                    return False

                if user.banned is False:
                    bot.whisper(source.username, 'User is not permabanned.')
                    return False

                user.banned = False
                message = message.lower()
                log_msg = '{} is no longer permabanned'.format(user.username_raw)
                bot.whisper(source.username, log_msg)

                AdminLogManager.add_entry('Permaban remove', source, log_msg)
Beispiel #44
0
    def add_link_blacklist(self, **options):
        bot = options['bot']
        message = options['message']
        source = options['source']

        options, new_links = self.parse_link_blacklist_arguments(message)

        if new_links:
            parts = new_links.split(' ')
            try:
                for link in parts:
                    if len(link) > 1:
                        self.blacklist_url(link, **options)
                        AdminLogManager.post('Blacklist link added', source, link)
                bot.whisper(source.username, 'Successfully added your links')
                return True
            except:
                log.exception('Unhandled exception in add_link_blacklist')
                bot.whisper(source.username, 'Some error occurred while adding your links')
                return False
        else:
            bot.whisper(source.username, 'Usage: !add link blacklist LINK')
            return False
Beispiel #45
0
    def add_banphrase(self, **options):
        """Method for creating and editing banphrases.
        Usage: !add banphrase BANPHRASE [options]
        Multiple options available:
        --length LENGTH
        --perma/--no-perma
        --notify/--no-notify
        """

        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            options, phrase = bot.banphrase_manager.parse_banphrase_arguments(message)

            if options is False:
                bot.whisper(source.username, 'Invalid banphrase')
                return False

            options['added_by'] = source.id
            options['edited_by'] = source.id

            banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(phrase, **options)

            if new_banphrase is True:
                bot.whisper(source.username, 'Added your banphrase (ID: {banphrase.id})'.format(banphrase=banphrase))
                AdminLogManager.post('Banphrase added', source, phrase)
                return True

            banphrase.set(**options)
            banphrase.data.set(edited_by=options['edited_by'])
            DBManager.session_add_expunge(banphrase)
            bot.banphrase_manager.commit()
            bot.whisper(source.username, 'Updated your banphrase (ID: {banphrase.id}) with ({what})'.format(banphrase=banphrase, what=', '.join([key for key in options if key != 'added_by'])))
            AdminLogManager.post('Banphrase edited', source, phrase)
Beispiel #46
0
    def remove_banphrase(self, **options):
        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            id = None
            try:
                id = int(message)
            except ValueError:
                pass

            banphrase = bot.banphrase_manager.find_match(message=message, id=id)

            if banphrase is None:
                bot.whisper(source.username, 'No banphrase with the given parameters found')
                return False

            AdminLogManager.post('Banphrase removed', source, banphrase.phrase)
            bot.whisper(source.username, 'Successfully removed banphrase with id {0}'.format(banphrase.id))
            bot.banphrase_manager.remove_banphrase(banphrase)
        else:
            bot.whisper(source.username, 'Usage: !remove banphrase (BANPHRASE_ID)')
            return False
Beispiel #47
0
    def commands_create(**options):
        session.pop('command_created_id', None)
        session.pop('command_edited_id', None)
        if request.method == 'POST':
            if 'aliases' not in request.form:
                abort(403)
            alias_str = request.form.get('aliases', '').replace('!', '').lower()
            delay_all = request.form.get('cd', Command.DEFAULT_CD_ALL)
            delay_user = request.form.get('usercd', Command.DEFAULT_CD_USER)
            level = request.form.get('level', Command.DEFAULT_LEVEL)
            cost = request.form.get('cost', 0)

            try:
                delay_all = int(delay_all)
                delay_user = int(delay_user)
                level = int(level)
                cost = int(cost)
            except ValueError:
                abort(403)

            if len(alias_str) == 0:
                abort(403)
            if delay_all < 0 or delay_all > 9999:
                abort(403)
            if delay_user < 0 or delay_user > 9999:
                abort(403)
            if level < 0 or level > 2000:
                abort(403)
            if cost < 0 or cost > 9999999:
                abort(403)

            user = options.get('user', None)

            if user is None:
                abort(403)

            options = {
                'delay_all': delay_all,
                'delay_user': delay_user,
                'level': level,
                'cost': cost,
                'added_by': user.id,
            }

            valid_action_types = ['say', 'me', 'whisper', 'reply']
            action_type = request.form.get('reply', 'say').lower()
            if action_type not in valid_action_types:
                abort(403)

            response = request.form.get('response', '')
            if len(response) == 0:
                abort(403)

            action = {
                'type': action_type,
                'message': response
            }
            options['action'] = action

            command_manager = (
                pajbot.managers.command.CommandManager(
                    socket_manager=None,
                    module_manager=ModuleManager(None).load(),
                    bot=None).load(enabled=None))

            command_aliases = []

            for alias, command in command_manager.items():
                command_aliases.append(alias)
                if command.command and len(command.command) > 0:
                    command_aliases.extend(command.command.split('|'))

            command_aliases = set(command_aliases)

            alias_str = alias_str.replace(' ', '').replace('!', '').lower()
            alias_list = alias_str.split('|')

            alias_list = [alias for alias in alias_list if len(alias) > 0]

            if len(alias_list) == 0:
                return render_template('admin/create_command_fail.html')

            for alias in alias_list:
                if alias in command_aliases:
                    return render_template('admin/create_command_fail.html')

            alias_str = '|'.join(alias_list)

            command = Command(command=alias_str, **options)
            command.data = CommandData(command.id, **options)
            log_msg = 'The !{} command has been created'.format(command.command.split('|')[0])
            AdminLogManager.add_entry('Command created',
                    user,
                    log_msg)
            with DBManager.create_session_scope(expire_on_commit=False) as db_session:
                db_session.add(command)
                db_session.add(command.data)
                db_session.commit()
                db_session.expunge(command)
                db_session.expunge(command.data)

            SocketClientManager.send('command.update', {'command_id': command.id})
            session['command_created_id'] = command.id
            return redirect('/admin/commands/', 303)
        else:
            return render_template('admin/create_command.html')
Beispiel #48
0
    def edit_command(bot, source, message, event, args):
        """Dispatch method for editing commands.
        Usage: !edit command ALIAS [options] RESPONSE
        Multiple options available:
        --whisper/--no-whisper
        --reply/--no-reply
        --modonly/--no-modonly
        --cd CD
        --usercd USERCD
        --level LEVEL
        --cost COST
        """

        if message:
            # Make sure we got both an alias and a response
            message_parts = message.split()
            if len(message_parts) < 2:
                bot.whisper(source.username, 'Usage: !add command ALIAS [options] RESPONSE')
                return False

            options, response = bot.commands.parse_command_arguments(message_parts[1:])

            options['edited_by'] = source.id

            if options is False:
                bot.whisper(source.username, 'Invalid command')
                return False

            alias = message_parts[0].replace('!', '').lower()
            type = 'say'
            if options['whisper'] is True:
                type = 'whisper'
            elif options['reply'] is True:
                type = 'reply'
            elif response.startswith('/me') or response.startswith('.me'):
                type = 'me'
                response = ' '.join(response.split(' ')[1:])
            elif options['whisper'] is False or options['reply'] is False:
                type = 'say'
            action = {
                'type': type,
                'message': response,
            }

            command = bot.commands.get(alias, None)

            if command is None:
                bot.whisper(source.username, 'No command found with the alias {}. Did you mean to create the command? If so, use !add command instead.'.format(alias))
                return False

            old_message = ''
            new_message = ''

            if len(action['message']) > 0:
                options['action'] = action
                old_message = command.action.response
                new_message = action['message']
            elif not type == command.action.subtype:
                options['action'] = {
                    'type': type,
                    'message': command.action.response,
                }
            bot.commands.edit_command(command, **options)
            bot.whisper(source.username, 'Updated the command (ID: {command.id})'.format(command=command))

            if len(new_message) > 0:
                log_msg = 'The !{} command has been updated from "{}" to "{}"'.format(
                        command.command.split('|')[0],
                        old_message,
                        new_message)
            else:
                log_msg = 'The !{} command has been updated'.format(command.command.split('|')[0])

            AdminLogManager.add_entry('Command edited',
                    source,
                    log_msg,
                    data={
                        'old_message': old_message,
                        'new_message': new_message,
                        })
Beispiel #49
0
    def banphrases_create(**options):
        session.pop('banphrase_created_id', None)
        session.pop('banphrase_edited_id', None)
        if request.method == 'POST':
            id = None
            try:
                if 'id' in request.form:
                    id = int(request.form['id'])
                name = request.form['name'].strip()
                permanent = request.form.get('permanent', 'off')
                warning = request.form.get('warning', 'off')
                notify = request.form.get('notify', 'off')
                case_sensitive = request.form.get('case_sensitive', 'off')
                sub_immunity = request.form.get('sub_immunity', 'off')
                remove_accents = request.form.get('remove_accents', 'off')
                length = int(request.form['length'])
                phrase = request.form['phrase']
                operator = request.form['operator'].strip().lower()
            except (KeyError, ValueError):
                abort(403)

            permanent = True if permanent == 'on' else False
            warning = True if warning == 'on' else False
            notify = True if notify == 'on' else False
            case_sensitive = True if case_sensitive == 'on' else False
            sub_immunity = True if sub_immunity == 'on' else False
            remove_accents = True if remove_accents == 'on' else False

            if len(name) == 0:
                abort(403)

            if len(phrase) == 0:
                abort(403)

            if length < 0 or length > 1209600:
                abort(403)

            valid_operators = ['contains', 'startswith', 'endswith', 'exact', 'regex']
            if operator not in valid_operators:
                abort(403)

            user = options.get('user', None)

            if user is None:
                abort(403)

            options = {
                    'name': name,
                    'phrase': phrase,
                    'permanent': permanent,
                    'warning': warning,
                    'notify': notify,
                    'case_sensitive': case_sensitive,
                    'sub_immunity': sub_immunity,
                    'remove_accents': remove_accents,
                    'length': length,
                    'added_by': user.id,
                    'edited_by': user.id,
                    'operator': operator,
                    }

            if id is None:
                banphrase = Banphrase(**options)
                banphrase.data = BanphraseData(banphrase.id, added_by=options['added_by'])

            with DBManager.create_session_scope(expire_on_commit=False) as db_session:
                if id is not None:
                    banphrase = db_session.query(Banphrase).options(joinedload(Banphrase.data)).filter_by(id=id).one_or_none()
                    if banphrase is None:
                        return redirect('/admin/banphrases/', 303)
                    banphrase.set(**options)
                    banphrase.data.set(edited_by=options['edited_by'])
                    log.info('Updated banphrase ID {} by user ID {}'.format(banphrase.id, options['edited_by']))
                    AdminLogManager.post('Banphrase edited', user, banphrase.phrase)
                else:
                    db_session.add(banphrase)
                    db_session.add(banphrase.data)
                    log.info('Added a new banphrase by user ID {}'.format(options['added_by']))
                    AdminLogManager.post('Banphrase added', user, banphrase.phrase)

            SocketClientManager.send('banphrase.update', {'id': banphrase.id})
            if id is None:
                session['banphrase_created_id'] = banphrase.id
            else:
                session['banphrase_edited_id'] = banphrase.id
            return redirect('/admin/banphrases/', 303)
        else:
            return render_template('admin/create_banphrase.html')
Beispiel #50
0
    def post(self, command_id, **extra_args):
        args = pajbot.utils.remove_none_values(self.post_parser.parse_args())
        if len(args) == 0:
            return {
                    'error': 'Missing parameter to edit.'
                    }, 400

        valid_names = [
                'enabled',
                'level',
                'delay_all',
                'delay_user',
                'cost',
                'can_execute_with_whisper',
                'sub_only'
                ]

        valid_action_names = [
                'type',
                'message'
                ]

        with DBManager.create_session_scope() as db_session:
            command = db_session.query(Command).options(joinedload(Command.data).joinedload(CommandData.user)).filter_by(id=command_id).one_or_none()
            if command is None:
                return {'error': 'Invalid command ID'}, 404

            if command.level > extra_args['user'].level:
                return {'error': 'Unauthorized'}, 403
            parsed_action = json.loads(command.action_json)
            options = {
                'edited_by': extra_args['user'].id,
            }

            for key in args:
                if key.startswith('data_'):
                    name = key[5:]
                    value = args[key]

                    if name.startswith('action_'):
                        name = name[7:]
                        if name in valid_action_names and name in parsed_action and command.action.type == 'message':
                            value_type = type(parsed_action[name])
                            if value_type is bool:
                                parsed_value = True if value == '1' else False
                            elif value_type is int:
                                try:
                                    parsed_value = int(value)
                                except ValueError:
                                    continue
                            else:
                                parsed_value = value
                            parsed_action[name] = parsed_value
                        command.action_json = json.dumps(parsed_action)
                    else:
                        if name in valid_names:
                            value_type = type(getattr(command, name))
                            if value_type is bool:
                                parsed_value = True if value == '1' else False
                            elif value_type is int:
                                try:
                                    parsed_value = int(value)
                                except ValueError:
                                    continue
                            else:
                                parsed_value = value
                            options[name] = parsed_value

            aj = json.loads(command.action_json)
            old_message = ''
            new_message = ''
            try:
                old_message = command.action.response
                new_message = aj['message']
            except:
                pass

            command.set(**options)
            command.data.set(**options)

            if len(old_message) > 0 and old_message != new_message:
                log_msg = 'The !{} command has been updated from "{}" to "{}"'.format(
                        command.command.split('|')[0],
                        old_message,
                        new_message)
            else:
                log_msg = 'The !{} command has been updated'.format(command.command.split('|')[0])

            AdminLogManager.add_entry('Command edited',
                    extra_args['user'],
                    log_msg,
                    data={
                        'old_message': old_message,
                        'new_message': new_message,
                        })

        if SocketClientManager.send('command.update', {'command_id': command_id}) is True:
            return {'success': 'good job'}, 200
        else:
            return {'error': 'could not push update'}, 500