Пример #1
0
def which_remote() -> typing.Tuple[typing.Optional[str], typing.Optional[str]]:
    '''
    Fetch desired remote session details using ``menu``

    Returns:
        username
        host
    '''
    # default config
    known_users: typing.List[str] = CONFIG['remote']['users']
    known_hosts: typing.List[str] = CONFIG['remote']['hosts']

    # temporary config
    if 'remote' in DATA:
        known_users += DATA['remote']['users']
        known_hosts += DATA['remote']['hosts']

    user: str = menu(opts=known_users, prompt='user') or os.environ['USER']
    host: str = menu(opts=known_hosts, prompt='host') or 'localhost'
    if user == os.environ['USER'] and host == 'localhost':
        # mind changed
        return None, None
    if user not in known_users:
        add_id(user)
    if host not in known_hosts:
        add_id(host, new='host')
    return user, host
Пример #2
0
def generate_password(user_idx: str = None) -> int:
    '''
    Args:
        user_idx: user-requested password index

    Returns:
        error

    Generate password interactively and copy it to ``wl-copy``
    '''
    ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
    if user_idx is None:
        user_idx = menu(opts=[], prompt='password for')
    if user_idx is None:
        return 1
    pass_gen = shell.process_comm('pass',
                                  'generate',
                                  user_idx,
                                  p_name='generating password')
    if pass_gen is None:
        return 1
    pass_text = pass_gen.split('\n')
    pass_text.remove('')
    pass_plain = ansi_escape.sub('', pass_text[-1])
    shell.process_comm('wl-copy',
                       '-o',
                       '-n',
                       '-f',
                       pass_plain.strip("\n"),
                       p_name='grabbing password')
    return 0
Пример #3
0
def graphical() -> bool:
    '''
    Get remote interface type: Graphical? Default: False

    Returns:
        Whether graphical interface is demanded [experimental]
    '''
    return menu(opts=['No', 'Yes'], prompt='Graphical') == 'Yes'
Пример #4
0
def remote_app() -> str:
    '''
    Get remote app, also remember it

    Returns:
        Graphical Remote App
    '''
    known_apps = []
    if 'apps' in DATA:
        known_apps += DATA['apps']
    app = menu(opts=known_apps, prompt='application name')
    if app not in known_apps:
        add_id(app, new='app')
    return app
Пример #5
0
def grab_password() -> typing.Optional[str]:
    '''
    Select password interactively and copy it to ``wl-copy``

    flush ``wl-paste`` after 45 sec to forget it

    Returns:
        if user entered an unknown password index, return it
        else, ``None``
    '''
    prefix = os.environ.get('PASSWORD_STORE_DIR',
                            f'{os.environ["HOME"]}/.password-store')
    password_files = []
    for base_path, _, leaves in os.walk(prefix):
        for file_name in leaves:
            _, f_ext = os.path.splitext(file_name)
            if f_ext == '.gpg':
                f_path = os.path.join(base_path, file_name.replace('.gpg', ''))
                password_files.append(os.path.relpath(f_path, prefix))
    user_idx = menu(opts=password_files, prompt='password for')
    if user_idx is None:
        return None
    if user_idx not in password_files:
        return user_idx
    # Password is known
    pass_text = shell.process_comm('pass',
                                   'show',
                                   user_idx,
                                   p_name='reading password db')
    if pass_text is None:
        return None
    shell.process_comm('wl-copy',
                       '-o',
                       '-n',
                       '-f',
                       pass_text.strip("\n"),
                       p_name='grabbing password',
                       timeout=-1)
    sleep(45)  # block for 45 seconds
    shell.process_comm('wl-paste', p_name='throwing password', timeout=-1)
    return None
Пример #6
0
def password(subcmd: int = 0) -> int:
    '''
    Handle password requests

    Args:
        subcmd: actions {0,1,2}
            0: grab password else offer generate (default)
            1: grab password else fail
            2: generate password
    Returns:
        error code

    '''
    subcmd %= 2
    user_idx = None
    if subcmd < 2:
        user_idx = grab_password()
        if not user_idx:
            return 1
    if ((not subcmd % 2)
            and (menu(opts=['No', 'Yes'], prompt='Create?') == 'Yes')):
        return generate_password(user_idx=user_idx)
    return 0