Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
 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, ' '))
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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...]')
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
def help_admin(update, mods):
    descs = ['/' + i + ': ' + Permissions.info(i) for i in mods]
    reply(update, 'Admin modules:' + djoin(descs))
Ejemplo n.º 10
0
def read_mod(update, module):
    users = Permissions.module_info(module)
    reply(update, module + ' can be run by:' + djoin(users))
Ejemplo n.º 11
0
def read_group(update, group):
    users = Permissions.group_info(group)
    reply(update, group + ' contains users:' + djoin(users))
Ejemplo n.º 12
0
def read_user(update, user):
    mods = Permissions.user_info(user)
    reply(update, user + ' has access to:' + djoin(mods))
Ejemplo n.º 13
0
def list_module(update, *_):
    reply(update, 'Modules:' + djoin(Permissions.modules()))