-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassmenu
More file actions
executable file
·169 lines (139 loc) · 4.23 KB
/
Copy pathpassmenu
File metadata and controls
executable file
·169 lines (139 loc) · 4.23 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
shopt -s nullglob globstar
#####################################################################
# Parse arguments
#####################################################################
typeit=0
typeDelay=30
enter=0
tab=1
browse=0
clipTime=45
xclipArgs="-selection clipboard"
declare -a get=("password")
while [ -n "$1" ]; do
case "$1" in
--get) IFS=':' read -ra get <<< "$2"; shift 2 ;;
--browse) browse=1; shift ;;
--type) typeit=1; shift ;;
--enter) enter=1; shift ;;
--no-tab) tab=0; shift ;;
--delay) typeDelay="$2"; shift 2;;
--clip-time) clipTime="$2"; shift 2;;
--xclip-args) xclipArgs="$2"; shift 2;;
--) shift; break ;;
*) break ;;
esac
done
#####################################################################
# Helper functions
#####################################################################
function findPasswordFiles() {
prefix=${PASSWORD_STORE_DIR-~/.password-store}
password_files=( "$prefix"/**/*.gpg )
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )
printf '%s\n' "${password_files[@]}"
}
function getKeys() {
# 1. Replace password value with key
# 2. Remove invalid lines
# 3. Remove values
echo "$@" | sed \
-e '1c\password' \
-e '/^[^:]*\:.*$/!d' \
-e 's/^\([^:]*\)\:.*$/\1/'
}
function getValue() {
if [[ "$1" == "password" ]]; then
echo "$2" | { IFS= read -r pass; printf %s "$pass"; }
else
echo "$2" | sed -n "s/^$1:[[:blank:]]*\\(.*\\)$/\\1/p"
fi
}
function typeText() {
echo -n "$@" | xdotool type --clearmodifiers --delay "$typeDelay" --file -
}
function typeEnter() {
xdotool key --delay "$typeDelay" Return
}
function typeTab() {
xdotool key --delay "$typeDelay" Tab
}
function clipboard() {
string="$*"
oldClip="$(xclip $xclipArgs -o 2>/dev/null)"
echo -n "$string" | xclip $xclipArgs
{
sleep "$clipTime"
if [ "$(xclip $xclipArgs -o 2>/dev/null)" == "$string" ]; then
echo -n "$oldClip" | xclip $xclipArgs
fi
} &
}
#####################################################################
# Select and unlock password file
#####################################################################
password=$(findPasswordFiles | dmenu "$@")
[[ -n $password ]] || exit
lines="$(pass show "$password" 2>/dev/null)" || exit 1
#####################################################################
# Browse custom values with dmenu
#####################################################################
if [[ $browse -eq 1 ]]; then
keys="$(getKeys "$lines")"
key="password"
values=""
if [[ $typeit -eq 0 ]]; then
prompt="Copy to clipboard: "
else
prompt="Write to focused input: "
fi
while true; do
key="$(echo -e "$keys" | IFS=$'\n' dmenu "$@" -p "$prompt")"
[[ -n "$key" ]] || break
[[ -n "$value" ]] && values="$values"$'\n'
value="$(getValue "$key" "$lines")"
if [[ $typeit -eq 1 ]]; then
typeText "$value"
if [[ $tab -eq 1 ]]; then
typeTab
fi
else
prompt="$prompt $key"
values="$values$value"
fi
done
if [[ $typeit -eq 0 ]]; then
clipboard "$values"
fi
#####################################################################
# Get password and/or custom values with arguments
#####################################################################
else
values=""
for key in "${get[@]}"; do
if [ "$key" == "password" ]; then
value="$(echo "$lines" | head -n1 )"
else
value="$(getValue "$key" "$lines")"
fi
[[ -n "$values" ]] && values="$values"$'\n'
values="$values$value"
done
if [[ $typeit -eq 0 ]]; then
clipboard "$values"
else
vc=0 # value counter
while IFS=$'\n' read -r value || [[ -n "$value" ]]; do
(( vc++ ))
if [[ $vc -gt 1 && $tab -eq 1 ]]; then
typeTab
fi
typeText "$value"
done <<< "$values"
if [[ $enter -eq 1 ]]; then
typeEnter
fi
fi
fi