def privileges_check(user): # Print and log a critical database access error if the database has not been initialized. if not GS.mumble_db_string: dprint( f"The JJMumbleBot database has not been initialized, but a user privilege check is trying to access it!\nTrying to initialize database..." ) log(CRITICAL, f"The JJMumbleBot database has not been initialized, but a user privilege check is trying to access it!\nTrying to initalize database...", origin=L_DATABASE) return -1 # Retrieve the user information in the database. user_data = GetDB.get_user_data(db_cursor=get_memory_db().cursor(), user_name=user['name']) # Create a new user entry if the user does not already exist in the database. if not user_data: InsertDB.insert_new_user(db_conn=get_memory_db(), username=user['name']) InsertDB.insert_new_permission( db_conn=get_memory_db(), username=user['name'], permission_level=Privileges.DEFAULT.value) # save_memory_db_to_file(get_memory_db()) # Retrieve the user information in the database. user_data = GetDB.get_user_data(db_cursor=get_memory_db().cursor(), user_name=user['name']) return int(user_data['level']) return int(user_data['level'])
def add_to_privileges(username, level): if InsertDB.insert_new_user(db_conn=get_memory_db(), username=username): if InsertDB.insert_new_permission(db_conn=get_memory_db(), username=username, permission_level=int(level)): return True return False
def initialize_plugins(): import sys import os # 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 os.listdir(f'{dir_utils.get_main_dir()}/plugins/core') if os.path.isdir(os.path.join(f'{dir_utils.get_main_dir()}/plugins/core', name)) and name != "__pycache__"] for p_file in all_imports: if not os.path.exists(os.path.join(f'{dir_utils.get_main_dir()}/plugins/core', p_file)): 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. global_settings.bot_plugins[p_file] = __import__(f'{p_file}.{p_file}', fromlist=['*']).Plugin() # 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 os.listdir(f'{dir_utils.get_main_dir()}/plugins/extensions') if os.path.isdir( os.path.join(f'{dir_utils.get_main_dir()}/plugins/extensions', name)) and name != "__pycache__"] for p_file in all_imports: if not os.path.exists(os.path.join(f'{dir_utils.get_main_dir()}/plugins/extensions', p_file)): 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. global_settings.bot_plugins[p_file] = __import__(f'{p_file}.{p_file}', fromlist=['*']).Plugin() # 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 #########")
def add_to_privileges(username, level): if InsertDB.insert_new_user(db_conn=get_memory_db(), username=username): if InsertDB.insert_new_permission(db_conn=get_memory_db(), username=username, permission_level=int(level)): log(INFO, f"A new user: [{username}] has been added to the user privileges.", origin=L_USER_PRIV, print_mode=PrintMode.VERBOSE_PRINT.value) return True return False
def initialize_plugins_safe(): import sys import os import json from JJMumbleBot.lib.resources.strings import C_PLUGIN_SETTINGS, P_PLUG_SAFE 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.') # 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 = {} safe_mode_plugins = json.loads(global_settings.cfg.get(C_PLUGIN_SETTINGS, P_PLUG_SAFE)) # Load Core Plugins rprint("######### Initializing Core Plugins - Safe Mode #########") sys.path.insert(0, f'{dir_utils.get_main_dir()}/plugins/core') all_imports = [name for name in os.listdir(f'{dir_utils.get_main_dir()}/plugins/core') if os.path.isdir( os.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 os.path.exists(os.path.join(f'{dir_utils.get_main_dir()}/plugins/core', p_file)): 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. global_settings.bot_plugins[p_file] = __import__(f'{p_file}.{p_file}', fromlist=['*']).Plugin() # 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') # help_plugin = __import__('help.help') # self.bot_plugins['help'] = help_plugin.help.Plugin(self.bot_plugins) sys.path.pop(0) rprint("######### Core Plugins Initialized - Safe Mode #########") # Load Extension Plugins rprint("######### Initializing Extension Plugins - Safe Mode #########") sys.path.insert(0, f'{dir_utils.get_main_dir()}/plugins/extensions') all_imports = [name for name in os.listdir(f'{dir_utils.get_main_dir()}/plugins/extensions') if os.path.isdir( os.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 os.path.exists(os.path.join(f'{dir_utils.get_main_dir()}/plugins/extensions', p_file)): 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. global_settings.bot_plugins[p_file] = __import__(f'{p_file}.{p_file}', fromlist=['*']).Plugin() # 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 - Safe Mode #########")
def init_database(): if not path.exists(f"{dir_utils.get_main_dir()}/cfg/jjmumblebot.db"): dprint("JJMumbleBot database is missing, creating a new database.", origin=L_DATABASE) log(WARNING, "JJMumbleBot database is missing, creating a new database.", origin=L_DATABASE) with sqlite3.connect( f"{dir_utils.get_main_dir()}/cfg/jjmumblebot.db") as conn: cursor = conn.cursor() # Create the users table. if CreateDB.create_table_users(db_cursor=cursor): dprint(f"Created the users table.", origin=L_DATABASE) log(INFO, f"Created the users table.", origin=L_DATABASE) # Create the permission levels table. if CreateDB.create_table_permission_levels(db_cursor=cursor): dprint(f"Created the permission levels table.", origin=L_DATABASE) log(INFO, f"Created the permission levels table.", origin=L_DATABASE) # Create the permissions table. if CreateDB.create_table_permissions(db_cursor=cursor): dprint(f"Created the user permissions table.", origin=L_DATABASE) log(INFO, f"Created the user permissions table.", origin=L_DATABASE) # Create the plugins table. if CreateDB.create_table_plugins(db_cursor=cursor): dprint(f"Created the plugins table.", origin=L_DATABASE) log(INFO, f"Created the plugins table.", origin=L_DATABASE) # Create the commands table. if CreateDB.create_table_commands(db_cursor=cursor): dprint(f"Created the commands table.", origin=L_DATABASE) log(INFO, f"Created the commands table.", origin=L_DATABASE) # Create the aliases table. if CreateDB.create_table_aliases(db_cursor=cursor): dprint(f"Created the aliases table.", origin=L_DATABASE) log(INFO, f"Created the aliases table.", origin=L_DATABASE) # Create the plugins_help table. if CreateDB.create_table_plugins_help(db_cursor=cursor): dprint(f"Created the plugins_help table.", origin=L_DATABASE) log(INFO, f"Created the plugins_help table.", origin=L_DATABASE) # Create all the standard permission levels. if InsertDB.insert_new_permission_level( db_conn=conn, level_id=Privileges.BLACKLIST.value, level_type=Privileges.BLACKLIST.name, ignore_file_save=True): if InsertDB.insert_new_permission_level( db_conn=conn, level_id=Privileges.DEFAULT.value, level_type=Privileges.DEFAULT.name, ignore_file_save=True): if InsertDB.insert_new_permission_level( db_conn=conn, level_id=Privileges.ELEVATED.value, level_type=Privileges.ELEVATED.name, ignore_file_save=True): if InsertDB.insert_new_permission_level( db_conn=conn, level_id=Privileges.MODERATOR.value, level_type=Privileges.MODERATOR.name, ignore_file_save=True): if InsertDB.insert_new_permission_level( db_conn=conn, level_id=Privileges.ADMINISTRATOR.value, level_type=Privileges.ADMINISTRATOR.name, ignore_file_save=True): if InsertDB.insert_new_permission_level( db_conn=conn, level_id=Privileges.SUPERUSER.value, level_type=Privileges.SUPERUSER.name, ignore_file_save=True): dprint( f"Inserted all permission level entries.", origin=L_DATABASE) log(INFO, f"Inserted all permission level entries.", origin=L_DATABASE) # Create a default super user based on the one provided in the config.ini file. if InsertDB.insert_new_user( db_conn=conn, username=global_settings.cfg[C_CONNECTION_SETTINGS] [P_DEFAULT_SU], ignore_file_save=True): if InsertDB.insert_new_permission( db_conn=conn, username=global_settings.cfg[C_CONNECTION_SETTINGS] [P_DEFAULT_SU], permission_level=Privileges.SUPERUSER.value, ignore_file_save=True): dprint( f"Inserted default super user entry from config.ini file.", origin=L_DATABASE) log(INFO, f"Inserted default super user entry from config.ini file.", origin=L_DATABASE) # Finished initializing the database. dprint( f"A new database has been created, and the default user: '******' has been added as a super user.", origin=L_DATABASE) log(INFO, f"A new database has been created, and the default user: '******' has been added as a super user.", origin=L_DATABASE) save_memory_db(mem_db_conn=conn) return with sqlite3.connect( f"{dir_utils.get_main_dir()}/cfg/jjmumblebot.db") as conn: save_memory_db(conn)
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 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.' ) # 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 = {} safe_mode_plugins = loads( global_settings.cfg.get(C_PLUGIN_SETTINGS, P_PLUG_SAFE)) # Load Core Plugins rprint("######### Initializing Core Plugins - Safe Mode #########") 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' ): 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' ) # help_plugin = __import__('help.help') # self.bot_plugins['help'] = help_plugin.help.Plugin(self.bot_plugins) sys.path.pop(0) rprint("######### Core Plugins Initialized - Safe Mode #########") # Load Extension Plugins rprint( "######### Initializing Extension Plugins - Safe Mode #########") 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( path.join( f'{dir_utils.get_main_dir()}/plugins/extensions', p_file)): 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/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 - Safe Mode #########")
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 #########")
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)
def add_to_aliases(alias, commands): if InsertDB.insert_new_alias(db_conn=get_memory_db(), alias_name=alias, commands=commands): log(INFO, f"Registered new alias: [{alias}] - [{commands}]", origin=L_ALIASES, print_mode=PrintMode.VERBOSE_PRINT.value) return True log(INFO, f"Could not add [{alias}] to registered aliases.", origin=L_ALIASES, print_mode=PrintMode.VERBOSE_PRINT.value) return False