def explore_tree(update, context, args, cmd, chain): try: opt = args[0] res = cmd[opt] chain.append(opt) if isinstance(res, dict): explore_tree(update, context, args[1:], res, chain) else: res(update, context, args[1:], chain) except (IndexError, KeyError): usage = 'usage: /' + mod_name + djoin(chain, ' ') usage += ' <arg1> [args...]\nOptions:' + djoin(cmd.keys()) reply(update, usage)
def invoke(update, context, _): # Ignore unprivileged people who = Permissions.e_whois(update) if not Permissions.is_user(who): return # If /help admin was requested admin_mods = Permissions.admin_modules() if Permissions.is_admin and len(context.args) > 0: if context.args[0] == 'admin': help_admin(update, admin_mods) return # Get descriptions of the loaded modules that the user may invoke mods = set(Loader.loaded()) & set(Permissions.user_modules(who)) mods -= set(['start', 'logout']) mod_list = list(mods - admin_mods) if who != raw_whois(update): mod_list.append('logout') descs = ['/' + i + ': ' + Permissions.info(i) for i in mod_list] # Reply if Permissions.is_admin(who): descs.insert(0, 'Admin: /help admin for admin modules') reply(update, '@' + who + ' may use:' + djoin(descs))
def invoke(update, context, _): usage = 'Usage: /module <command> <args>' try: fn = cmds[context.args[0]] fn(update, context.args[1:]) except (KeyError, IndexError): assert len(cmds.keys()) > 0 msg = usage + '\nCommands:' + djoin(cmds.keys()) reply(update, msg)
def wrapper(update, context, args, chain): try: f(update, *args) if notify_success: reply(update, 'Success') except AssertionError as err: reply(update, 'Error: ' + str(err)) except TypeError: if fn_usage: chain.append(fn_usage) reply(update, 'Usage: /' + mod_name + djoin(chain, ' '))
def unload_module(update, args, *, cmd='unload'): try: mod = args[0] Loader.unload(mod) reply(update, mod + ' successfully unloaded') return True except (AssertionError, IndexError): msg = 'Usage: /module ' + cmd + ' <module>' msg += '\nLoaded modules:' + djoin(Loader.loaded()) reply(update, msg) return False
def invoke(update, context, _): try: what = context.args[0] fn = Loader.pre_protection_fn(what) del context.args[0] fn(update, context) except AssertionError: msg = 'Errpr: No such module.' msg += ' Loaded modules are:' + djoin(Loader.loaded()) reply(update, msg) except IndexError: reply(update, '/sudo <module> [args...]')
def reset_modules(update, _): for i in list(Loader.loaded()): Loader.unload(i) reply(update, 'All modules unloaded. Loading modules.') success = [] for mod in Permissions.modules(): try: Loader.load(mod) success.append(mod) except ModuleNotFoundError as err: reply(update, str(err)) logging.warning(str(err)) if len(success) > 0: msg = 'Successfully loaded:' + djoin(success) reply(update, msg)
def load_module(update, args, *, cmd='load'): mod = '' try: mod = args[0] Loader.load(mod) reply(update, mod + ' successfully loaded') return True except (AssertionError, IndexError) as e: msg = 'Usage: /module ' + cmd + ' <module>' unloaded = [ i for i in Permissions.modules() if i not in Loader.loaded() ] msg += '\nUnloaded modules:' + djoin(unloaded) reply(update, msg) return False except ModuleNotFoundError as err: logging.error('Failed to load module "' + mod + '" with error: ' + str(err)) reply(update, 'Failed to load module.') raise
def help_admin(update, mods): descs = ['/' + i + ': ' + Permissions.info(i) for i in mods] reply(update, 'Admin modules:' + djoin(descs))
def read_mod(update, module): users = Permissions.module_info(module) reply(update, module + ' can be run by:' + djoin(users))
def read_group(update, group): users = Permissions.group_info(group) reply(update, group + ' contains users:' + djoin(users))
def read_user(update, user): mods = Permissions.user_info(user) reply(update, user + ' has access to:' + djoin(mods))
def list_module(update, *_): reply(update, 'Modules:' + djoin(Permissions.modules()))