diff options
Diffstat (limited to 'surf-pass')
-rwxr-xr-x | surf-pass | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/surf-pass b/surf-pass new file mode 100755 index 0000000..ee20f52 --- /dev/null +++ b/surf-pass @@ -0,0 +1,98 @@ +#!/bin/bash +set -e + +usage() { + echo "Usage: $0 XID {NAME|PASS}" >&2 + exit 1 +} + +set_active_value() { + xprop -id "$xid" -f "_SURF_ACTIVE_VALUE" 8s -set "_SURF_ACTIVE_VALUE" "$1" +} + +get_active_value() { + xprop -id "$xid" -notype "_SURF_ACTIVE_VALUE" | sed -nE 's/^_SURF_ACTIVE_VALUE = "(.*)"$/\1/p' +} + +get_uri() { + xprop -id "$xid" -notype "_SURF_URI" | sed -nE 's/^_SURF_URI = "(.*)"$/\1/p' +} + +domain() { + get_uri | awk -F/ '{print $3}' +} + +primary_domain() { + domain | awk -F. '{ print $(NF-1) "." $NF}' +} + +find_pass() { + local postfix="$1" + shift + + array=() + while read -r field; do + local nogpg="${field%.gpg}" + local noprefix="${nogpg#*/.password-store/}" + if [ -n "$noprefix" ]; then + array+=("$noprefix") + fi + done <<<"$(find ~/.password-store/"$postfix" -type f "$@" 2>/dev/null)" +} + +choose_entry() { + if [ "${#array[@]}" -eq 0 ]; then + find_pass + fi + if [ "${#array[@]}" -eq 1 ]; then + echo "${array[0]}" + return + fi + printf '%s\n' "${array[@]}" | dmenu -p 'Pass:' -w "$xid" | head -1 +} + + +pass_name() { + local array + find_pass "$(primary_domain)" + + local entry + entry="$(choose_entry)" + set_active_value "${entry##*/}" +} + +pass_pass() { + local array curname + array=() + curname="$(get_active_value)" + if [ -n "$curname" ]; then + find_pass "$(primary_domain)" -name "$curname" + fi + if [ "${#array[@]}" -eq 0 ]; then + find_pass "$(primary_domain)" + fi + + local entry + entry="$(choose_entry)" + set_active_value "$(pass "$entry")" + set_active_value # Reset value to empty to hide password +} + +####################### + +[ $# -eq 2 ] || usage + + +xid="$1" + +case "$2" in + NAME) + pass_name + ;; + PASS) + pass_pass + ;; + *) + usage + ;; +esac |