summaryrefslogtreecommitdiff
path: root/surf-pass
blob: ee20f52586c3fc241e04b07877eca527e5ac8c87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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