~palb91/bwm

98931a6da5b4636150f2384bcb4a143b725934d3 — Pierre-Albéric TROUPLIN 6 months ago ed82eeb main
feat: qutebrowser flag, xdotool -> xvkbd and more
1 files changed, 65 insertions(+), 35 deletions(-)

M bwm
M bwm => bwm +65 -35
@@ 14,6 14,7 @@ cmd=wtype
asset=pass
url=
selection=
qutebrowser=false


# /!\ Dirty hack to force the use of a specific keymap /!\


@@ 34,7 35,10 @@ Options:
    -u, --user  Type the username instead of the password.
    -b, --both  Type the username and the password separated by a <Tab>.
    -o, --otp   Type the otp code if exists.
    -a, --all   Type the username, the password and the otp code separated by a
                <Tab> (no check on otp existing for the moment).
    -c, --copy  Copy instead of type.
        --qb    Qutebrowser mode (start insert mode before).
    -h, --help  Print this help.

Argument:


@@ 45,41 49,45 @@ HELP
    exit "${1:-0}"
}


die() {
    printf '%s\n\n' "${*}" >&2
    help 1
}


check_dep() {
    if [[ -z "$(command -v "${1}")" ]]; then
    if [ -z "$(command -v "${1}")" ]; then
        printf '%s is not installed\n' "${1}" >&2
        exit 1
    fi
}


menu() {
    column -ts$'\t' -o' :: ' -R2 \
        | bemenu -pselect:       \
        | sed 's/ .*// ; q'
    column -ts$'\t' -R2 | bemenu -pselect:
}


use_Xwayland() {
    query='recurse(.nodes[]?, .floating_nodes[]?) | select(.focused).shell'
    [[ "$(swaymsg -t get_tree | jq -r "${query}")" = 'xwayland' ]]
    [ "$(swaymsg -t get_tree | jq -r "${query}")" = 'xwayland' ]
}


# Parse arguments, check dependancies
parse_args() {
    while [[ "${#}" -gt 0 ]]; do
    while [ "${#}" -gt 0 ]; do
        case "${1}" in
            -h|--help) help                     ;;
            -c|--copy) cmd=wl-copy              ;;
            -u|--user) asset=user               ;;
            -b|--both) asset=both               ;;
            -o|--otp)  asset=totp               ;;
            -cu)       cmd=wl-copy asset=user   ;;
            -co)       cmd=wl-copy asset=totp   ;;
            -a|--all)  asset=all                ;;
            -cu|-uc)   cmd=wl-copy asset=user   ;;
            -co|-oc)   cmd=wl-copy asset=totp   ;;
            --qb)      qutebrowser=true         ;;
            -*)        die "Wrong option: ${1}" ;;
            *)         url="${1}"; break        ;;
        esac


@@ 87,15 95,19 @@ parse_args() {
        shift
    done

    [[ "${#}" -gt 1 ]] \
        && die "Wrong number of arguments."
    if [ "${#}" -gt 1 ]; then
        die "Wrong number of arguments."

    [[ "${cmd}" = 'wl-copy' ]] && [[ "${asset}" = 'both' ]] \
        && die 'Cannot copy both username and password.'
    elif [ "${cmd}" = 'wl-copy' ]; then
        if [ "${asset}" = 'both' ] || [ "${asset}" = 'all' ]; then
            die 'Cannot copy both username and password.'
        fi
    fi

    # Use xdotool for Xwayland windows
    [[ "${cmd}" = 'wtype' ]] && use_Xwayland \
        && cmd=xdotool
    # Use xvkbd for Xwayland windows
    if [ "${cmd}" = 'wtype' ] && use_Xwayland; then
        cmd=xvkbd
    fi

    check_dep "${cmd}"
}


@@ 107,30 119,31 @@ get_credentials() {


    # Exit if not entry in bitwarden
    if [[ "${entries}" = '[]' ]]; then
    if [ "${entries}" = '[]' ]; then
        printf 'No credentials in bitwarden...\n' >&2
        exit 2
    fi


    # Gather matching entries
    _jq='if length == 1 then
           .[0] | .id + "\t" + .name + "\t" + .login.username
         else
           sort_by(.favorite == false)
               | .[]
               | .id + "\t" + .name + "\t" + .login.username
         end'
    _jq='
    if length == 1 then
      .[0].id
    else
      sort_by(.favorite == false)
           | .[]
           | .folder + "\t" + .name + "\t" + .login.username + 300 * " " + .id
    end'

    mapfile -t match < <(printf '%s' "${entries}" | jq -r "${_jq}")

    # Get id, filter if there are several matchs
    id="${match[0]%% *}"
    if [[ "${#match[@]}" -gt 1 ]]; then
    id="${match[0]}"
    if [ "${#match[@]}" -gt 1 ]; then
        check_dep bemenu
        id="$(printf "%s\n" "${match[@]}" | menu)"
    fi
    id="${id%%$'\t'*}"
    id="${id##* }"


    # Get corresponding json ojbect


@@ 139,7 152,7 @@ get_credentials() {


    # Exit if no values
    if [[ -z "${selection}" ]]; then
    if [ -z "${selection}" ]; then
        printf 'Wrong selection. Exiting...\n' >&2
        exit 3
    fi


@@ 158,24 171,41 @@ get_credentials() {
}


# Run wtype, xdotool or wl-copy with asked content
# Run wtype, xvkbd or wl-copy with asked content
type_or_copy() {
    set -- "${credentials[${asset}]}"

    if [[ "${cmd}" = 'wtype' ]]; then
        [[ "${asset}" = 'both' ]] \
            && set -- "${credentials[user]}" -k Tab "${credentials[pass]}"
    if [ "${cmd}" = 'wtype' ]; then
        if [ "${asset}" = 'both' ]; then
            set -- "${credentials[user]}" -k Tab "${credentials[pass]}"

        elif [ "${asset}" = 'all' ]; then
            set -- "${credentials[user]}" -k Tab \
                   "${credentials[pass]}" -k Tab \
                   "${credentials[totp]}"
        fi

        # Add a short delay before typing
        set -- -s 150 "${@}"

    elif [[ "${cmd}" = 'xdotool' ]]; then
        # Qutebrowser mode, press i and remove it
        if "${qutebrowser}"; then
            set -- 'i' -k BackSpace "${@}"
        fi

    elif [ "${cmd}" = 'xvkbd' ]; then
        setxkbmap "${keymap[@]}"

        [[ "${asset}" = 'both' ]] \
            && set -- "${credentials[user]}"$'\t'"${credentials[pass]}"
        if [ "${asset}" = 'both' ]; then
            set -- "i\b${credentials[user]}\t${credentials[pass]}"

        elif [ "${asset}" = 'all' ]; then
            set -- "i\b${credentials[user]}"
            set -- "${*}\t${credentials[pass]}"
            set -- "${*}\t${credentials[totp]}"
        fi

        set -- sleep 0.15 type "${@}"
        set -- -no-keypad -text "\D2${*}"
    fi

    exec "${cmd}" "${@}"