Exemplo n.º 1
0
    def initialize_plugins():
        import sys
        from os import path, listdir
        from json import loads

        # Clear plugins, plugins_help, and commands tables on launch.
        DeleteDB.delete_all_commands(db_conn=get_memory_db())
        DeleteDB.delete_all_plugins_help(db_conn=get_memory_db())
        DeleteDB.delete_all_plugins(db_conn=get_memory_db())

        # Import global aliases into the database.
        UtilityDB.import_aliases_to_db(
            db_conn=get_memory_db(),
            csv_path=f'{dir_utils.get_main_dir()}/cfg/global_aliases.csv')

        global_settings.bot_plugins = {}
        # Load Core Plugins
        rprint("######### Initializing Core Plugins #########")
        sys.path.insert(0, f'{dir_utils.get_main_dir()}/plugins/core')
        all_imports = [
            name
            for name in listdir(f'{dir_utils.get_main_dir()}/plugins/core')
            if path.isdir(
                path.join(f'{dir_utils.get_main_dir()}/plugins/core', name))
            and name != "__pycache__"
        ]
        for p_file in all_imports:
            if not path.exists(
                    f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/metadata.ini'
            ):
                rprint(
                    f"{p_file} plugin does not contain a metadata.ini file. Skipping initialization..."
                )
                log(
                    WARNING,
                    f"{p_file} plugin does not contain a metadata.ini file. Skipping initialization..."
                )
                continue
            # Import the core plugin class.
            global_settings.bot_plugins[p_file] = __import__(
                f'{p_file}.{p_file}', fromlist=['*']).Plugin

            # Register plugin command callbacks.
            plugin_metadata = PluginUtilityService.process_metadata(
                f'plugins/core/{p_file}')
            plugin_cmds = loads(
                plugin_metadata.get(C_PLUGIN_INFO, P_PLUGIN_CMDS))
            for plugin_command in plugin_cmds:
                global_settings.cmd_callbacks.register_command(
                    f'{plugin_command}', p_file, f'{plugin_command}_clbk')
                global_settings.mtd_callbacks.register_callback(
                    f'{plugin_command}_clbk',
                    getattr(global_settings.bot_plugins[p_file],
                            f'cmd_{plugin_command}', None))
                dprint(
                    f"Registered plugin command: "
                    f"{plugin_command}:{global_settings.cmd_callbacks.get_command(plugin_command)[1]}:cmd_{plugin_command}"
                )
            # Initialize the core plugin class instance.
            global_settings.bot_plugins[p_file] = global_settings.bot_plugins[
                p_file]()

            # Import core plugin into the database.
            InsertDB.insert_new_plugin(db_conn=get_memory_db(),
                                       plugin_name=p_file,
                                       ignore_file_save=True)
            # Import core plugin user privileges into the database.
            UtilityDB.import_privileges_to_db(
                db_conn=get_memory_db(),
                csv_path=
                f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/privileges.csv'
            )
            # Import plugin aliases into the database.
            UtilityDB.import_aliases_to_db(
                db_conn=get_memory_db(),
                csv_path=
                f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/aliases.csv'
            )
            # Import plugin help into the database.
            UtilityDB.import_help_to_db(
                db_conn=get_memory_db(),
                html_path=
                f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/help.html')
        sys.path.pop(0)
        rprint("######### Core Plugins Initialized #########")
        # Load Extension Plugins
        rprint("######### Initializing Extension Plugins #########")
        sys.path.insert(0, f'{dir_utils.get_main_dir()}/plugins/extensions')
        all_imports = [
            name for name in listdir(
                f'{dir_utils.get_main_dir()}/plugins/extensions')
            if path.isdir(
                path.join(f'{dir_utils.get_main_dir()}/plugins/extensions',
                          name)) and name != "__pycache__"
        ]
        for p_file in all_imports:
            if not path.exists(
                    f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/metadata.ini'
            ):
                rprint(
                    f"{p_file} plugin does not contain a metadata.ini file. Skipping initialization..."
                )
                log(
                    WARNING,
                    f"{p_file} plugin does not contain a metadata.ini file. Skipping initialization..."
                )
                continue
            # Import the core plugin class.
            global_settings.bot_plugins[p_file] = __import__(
                f'{p_file}.{p_file}', fromlist=['*']).Plugin

            # Register plugin command callbacks.
            plugin_metadata = PluginUtilityService.process_metadata(
                f'plugins/extensions/{p_file}')
            plugin_cmds = loads(
                plugin_metadata.get(C_PLUGIN_INFO, P_PLUGIN_CMDS))
            for plugin_command in plugin_cmds:
                global_settings.cmd_callbacks.register_command(
                    f'{plugin_command}', p_file, f'{plugin_command}_clbk')
                global_settings.mtd_callbacks.register_callback(
                    f'{plugin_command}_clbk',
                    getattr(global_settings.bot_plugins[p_file],
                            f'cmd_{plugin_command}', None))
                dprint(
                    f"Registered plugin command: "
                    f"{plugin_command}:{global_settings.cmd_callbacks.get_command(plugin_command)[1]}:cmd_{plugin_command}"
                )
            # Initialize the core plugin class instance.
            global_settings.bot_plugins[p_file] = global_settings.bot_plugins[
                p_file]()
            # Import core plugin into the database.
            InsertDB.insert_new_plugin(db_conn=get_memory_db(),
                                       plugin_name=p_file,
                                       ignore_file_save=True)
            # Import core plugin user privileges into the database.
            UtilityDB.import_privileges_to_db(
                db_conn=get_memory_db(),
                csv_path=
                f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/privileges.csv'
            )
            # Import plugin aliases into the database.
            UtilityDB.import_aliases_to_db(
                db_conn=get_memory_db(),
                csv_path=
                f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/aliases.csv'
            )
            # Import plugin help into the database.
            UtilityDB.import_help_to_db(
                db_conn=get_memory_db(),
                html_path=
                f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/help.html'
            )
        save_memory_db_to_file()
        sys.path.pop(0)
        rprint("######### Extension Plugins Initialized #########")
Exemplo n.º 2
0
def clear_aliases():
    if DeleteDB.delete_all_aliases(db_conn=get_memory_db()):
        log(INFO, "All aliases were removed from the registered aliases.", origin=L_ALIASES, print_mode=PrintMode.VERBOSE_PRINT.value)
        return True
    log(INFO, "Could not remove all aliases from registered aliases.", origin=L_ALIASES, print_mode=PrintMode.VERBOSE_PRINT.value)
    return False
Exemplo n.º 3
0
    def initialize_plugins_safe():
        import sys
        from os import path, listdir
        from json import loads
        from JJMumbleBot.lib.resources.strings import C_PLUGIN_SETTINGS, P_PLUG_SAFE
        from hashlib import md5
        if not global_settings.cfg:
            from JJMumbleBot.lib.errors import ExitCodes, ConfigError
            from JJMumbleBot.lib.utils import runtime_utils
            runtime_utils.exit_bot_error(ExitCodes.CONFIG_ERROR)
            raise ConfigError(
                'There was an error loading the global config for initializing safe mode plugins.'
            )

        # Check database metadata for changes.
        # If the version is different, or plugin checksum is modified,
        # clear plugins, plugins_help, and commands tables on launch.
        if global_settings.cfg.getboolean(C_MAIN_SETTINGS,
                                          P_DB_INTEGRITY,
                                          fallback=True):
            log(INFO,
                "######### Checking Database Integrity",
                origin=L_STARTUP,
                print_mode=PrintMode.REG_PRINT.value)

            all_core_plugins = [
                name
                for name in listdir(f'{dir_utils.get_main_dir()}/plugins/core')
                if path.isdir(
                    path.join(f'{dir_utils.get_main_dir()}/plugins/core',
                              name)) and name != "__pycache__"
            ]
            all_ext_plugins = [
                name for name in listdir(
                    f'{dir_utils.get_main_dir()}/plugins/extensions')
                if path.isdir(
                    path.join(f'{dir_utils.get_main_dir()}/plugins/extensions',
                              name)) and name != "__pycache__"
            ]

            filenames = []
            for core_plugin in all_core_plugins:
                glob_files = glob.glob(
                    f'{dir_utils.get_main_dir()}/plugins/core/{core_plugin}/**/*',
                    recursive=True)
                glob_files = [
                    f for f in glob_files
                    if path.isfile(f) and "__pycache__" not in f
                ]
                filenames.extend(glob_files)
            for ext_plugin in all_ext_plugins:
                glob_files = glob.glob(
                    f'{dir_utils.get_main_dir()}/plugins/extensions/{ext_plugin}/**/*',
                    recursive=True)
                glob_files = [
                    f for f in glob_files
                    if path.isfile(f) and "__pycache__" not in f
                ]
                filenames.extend(glob_files)

            hash_func = md5()  # nosec
            for fn in filenames:
                if path.isfile(fn):
                    hash_func.update(open(fn, "rb").read())
            plugins_checksum = hash_func.hexdigest()

            integrity_check = UtilityDB.check_database_metadata(
                db_conn=get_memory_db(),
                version=META_VERSION,
                plugins_checksum=plugins_checksum)
            if integrity_check is False:
                log(WARNING,
                    "Database integrity mismatch identified! Creating database backup and rebuilding database...",
                    origin=L_STARTUP,
                    print_mode=PrintMode.REG_PRINT.value)
                db_backup = BotServiceHelper.backup_database()
                if db_backup:
                    log(INFO,
                        f"Created internal database backup @ {db_backup}",
                        origin=L_DATABASE,
                        print_mode=PrintMode.REG_PRINT.value)
                DeleteDB.delete_all_commands(db_conn=get_memory_db())
                DeleteDB.delete_all_plugins_help(db_conn=get_memory_db())
                DeleteDB.delete_all_plugins(db_conn=get_memory_db())

            log(INFO,
                "######### Database Integrity Verified",
                origin=L_STARTUP,
                print_mode=PrintMode.REG_PRINT.value)

        # Import custom aliases into the database.
        log(INFO,
            "######### Importing Custom Aliases",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)
        UtilityDB.import_aliases_to_db(
            db_conn=get_memory_db(),
            csv_path=f'{dir_utils.get_main_dir()}/cfg/custom_aliases.csv',
            update_if_exists=False)
        log(INFO,
            "######### Imported Custom Aliases",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)

        # Import custom command permissions into the database.
        log(INFO,
            "######### Importing Custom Command Permissions",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)
        UtilityDB.import_privileges_to_db(
            db_conn=get_memory_db(),
            csv_path=f'{dir_utils.get_main_dir()}/cfg/custom_permissions.csv',
            update_if_exists=False)
        log(INFO,
            "######### Imported Custom Command Permissions",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)

        # Import custom user privileges into the database.
        log(INFO,
            "######### Importing Custom User Privileges",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)
        UtilityDB.import_user_privileges_to_db(
            db_conn=get_memory_db(),
            csv_path=
            f'{dir_utils.get_main_dir()}/cfg/custom_user_privileges.csv',
            update_if_exists=False)
        log(INFO,
            "######### Imported Custom User Privileges",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)

        global_settings.bot_plugins = {}
        safe_mode_plugins = loads(
            global_settings.cfg.get(C_PLUGIN_SETTINGS, P_PLUG_SAFE))

        # Load Core Plugins
        log(INFO,
            "######### Initializing Core Plugins - Safe Mode",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)
        sys.path.insert(0, f'{dir_utils.get_main_dir()}/plugins/core')
        all_imports = [
            name
            for name in listdir(f'{dir_utils.get_main_dir()}/plugins/core')
            if path.isdir(
                path.join(f'{dir_utils.get_main_dir()}/plugins/core', name))
            and name != "__pycache__"
        ]
        for p_file in all_imports:
            if p_file in safe_mode_plugins:
                if not path.exists(
                        f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/metadata.ini'
                ):
                    log(WARNING,
                        f"{p_file} plugin does not contain a metadata.ini file. Skipping initialization...",
                        origin=L_STARTUP,
                        print_mode=PrintMode.REG_PRINT.value)
                    continue
                # Import the core plugin class.
                global_settings.bot_plugins[p_file] = __import__(
                    f'{p_file}.{p_file}', fromlist=['*']).Plugin

                # Register plugin command callbacks.
                plugin_metadata = PluginUtilityService.process_metadata(
                    f'plugins/core/{p_file}')
                plugin_cmds = loads(
                    plugin_metadata.get(C_PLUGIN_INFO, P_PLUGIN_CMDS))
                for plugin_command in plugin_cmds:
                    global_settings.cmd_callbacks.register_command(
                        f'{plugin_command}', p_file, f'{plugin_command}_clbk')
                    global_settings.mtd_callbacks.register_callback(
                        f'{plugin_command}_clbk',
                        getattr(global_settings.bot_plugins[p_file],
                                f'cmd_{plugin_command}', None))
                    log(INFO,
                        f"Registered plugin command: {plugin_command}:{global_settings.cmd_callbacks.get_command(plugin_command)[1]}:cmd_{plugin_command}",
                        origin=L_STARTUP,
                        print_mode=PrintMode.VERBOSE_PRINT.value)
                # Initialize the core plugin class instance.
                global_settings.bot_plugins[
                    p_file] = global_settings.bot_plugins[p_file]()
                # Import core plugin into the database.
                InsertDB.insert_new_plugin(db_conn=get_memory_db(),
                                           plugin_name=p_file,
                                           ignore_file_save=True)
                # Import core plugin user privileges into the database.
                UtilityDB.import_privileges_to_db(
                    db_conn=get_memory_db(),
                    csv_path=
                    f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/privileges.csv'
                )
                # Import plugin aliases into the database.
                UtilityDB.import_aliases_to_db(
                    db_conn=get_memory_db(),
                    csv_path=
                    f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/aliases.csv'
                )
                # Import plugin help into the database.
                UtilityDB.import_help_to_db(
                    db_conn=get_memory_db(),
                    html_path=
                    f'{dir_utils.get_main_dir()}/plugins/core/{p_file}/help.html'
                )
                # Create directory for user modifiable plugin-specific data and configs.
                dir_utils.make_directory(
                    f'{dir_utils.get_plugin_data_dir()}/{p_file}')
        sys.path.pop(0)
        log(INFO,
            "######### Core Plugins Initialized - Safe Mode",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)

        # Load Extension Plugins
        log(INFO,
            "######### Initializing Extension Plugins - Safe Mode",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)
        sys.path.insert(0, f'{dir_utils.get_main_dir()}/plugins/extensions')
        all_imports = [
            name for name in listdir(
                f'{dir_utils.get_main_dir()}/plugins/extensions')
            if path.isdir(
                path.join(f'{dir_utils.get_main_dir()}/plugins/extensions',
                          name)) and name != "__pycache__"
        ]
        for p_file in all_imports:
            if p_file in safe_mode_plugins:
                if not path.exists(
                        f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/metadata.ini'
                ):
                    log(WARNING,
                        f"{p_file} plugin does not contain a metadata.ini file. Skipping initialization...",
                        origin=L_STARTUP,
                        print_mode=PrintMode.REG_PRINT.value)
                    continue
                # Import the core plugin class.
                global_settings.bot_plugins[p_file] = __import__(
                    f'{p_file}.{p_file}', fromlist=['*']).Plugin

                # Register plugin command callbacks.
                plugin_metadata = PluginUtilityService.process_metadata(
                    f'plugins/extensions/{p_file}')
                plugin_cmds = loads(
                    plugin_metadata.get(C_PLUGIN_INFO, P_PLUGIN_CMDS))
                for plugin_command in plugin_cmds:
                    global_settings.cmd_callbacks.register_command(
                        f'{plugin_command}', p_file, f'{plugin_command}_clbk')
                    global_settings.mtd_callbacks.register_callback(
                        f'{plugin_command}_clbk',
                        getattr(global_settings.bot_plugins[p_file],
                                f'cmd_{plugin_command}', None))
                    log(INFO,
                        f"Registered plugin command: {plugin_command}:{global_settings.cmd_callbacks.get_command(plugin_command)[1]}:cmd_{plugin_command}",
                        origin=L_STARTUP,
                        print_mode=PrintMode.VERBOSE_PRINT.value)
                # Initialize the core plugin class instance.
                global_settings.bot_plugins[
                    p_file] = global_settings.bot_plugins[p_file]()
                # Import core plugin into the database.
                InsertDB.insert_new_plugin(db_conn=get_memory_db(),
                                           plugin_name=p_file,
                                           ignore_file_save=True)
                # Import core plugin user privileges into the database.
                UtilityDB.import_privileges_to_db(
                    db_conn=get_memory_db(),
                    csv_path=
                    f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/privileges.csv'
                )
                # Import plugin aliases into the database.
                UtilityDB.import_aliases_to_db(
                    db_conn=get_memory_db(),
                    csv_path=
                    f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/aliases.csv'
                )
                # Import plugin help into the database.
                UtilityDB.import_help_to_db(
                    db_conn=get_memory_db(),
                    html_path=
                    f'{dir_utils.get_main_dir()}/plugins/extensions/{p_file}/help.html'
                )
                # Create directory for user modifiable plugin-specific data and configs.
                dir_utils.make_directory(
                    f'{dir_utils.get_plugin_data_dir()}/{p_file}')
        save_memory_db_to_file()
        sys.path.pop(0)
        log(INFO,
            "######### Extension Plugins Initialized - Safe Mode",
            origin=L_STARTUP,
            print_mode=PrintMode.REG_PRINT.value)
Exemplo n.º 4
0
def remove_from_aliases(alias):
    if DeleteDB.delete_alias(db_conn=get_memory_db(), alias_name=alias):
        log(INFO, f"Removed [{alias}] from registered aliases.", origin=L_ALIASES, print_mode=PrintMode.VERBOSE_PRINT.value)
        return True
    log(INFO, f"Could not remove [{alias}] from registered aliases.", origin=L_ALIASES, print_mode=PrintMode.VERBOSE_PRINT.value)
    return False