m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/contrib/dmenu/passmenu
blob: e5635fd241ddc5a68e61f73b0b8522b6466d5814 (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
#!/usr/bin/env bash

shopt -s nullglob globstar

typeit=0
otp=0
infer_otp=0
otp_pattern=""
finish=0
while [[ $# -gt 0 && $finish -eq 0 ]]; do
    argument="$1"
    case $argument in
        --type) typeit=1 ;;
        --otp) otp=1 ;;
        --infer-otp) infer_otp=1 ;;
        --otp-pattern) shift ; otp_pattern="$1" ;;
        *) finish=1
    esac
    # Don't shift if we're done with passmenu args - rest of args goes to dmenu
    [[ $finish -eq 0 ]] && shift
done

if [[ -n $WAYLAND_DISPLAY ]]; then
	dmenu=dmenu-wl
	xdotool="ydotool type --file -"
elif [[ -n $DISPLAY ]]; then
	dmenu=dmenu
	xdotool="xdotool type --clearmodifiers --file -"
else
	echo "Error: No Wayland or X11 display detected" >&2
	exit 1
fi

prefix=${PASSWORD_STORE_DIR-~/.password-store}
password_files=( "$prefix"/**/*.gpg )
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )

if [[ $otp -eq 1 ]]; then
    filtered_files=()
    for file in "${password_files[@]}"; do
        [[ "$file" =~ $otp_pattern ]] && filtered_files+=("$file")
    done
    password_files=(${filtered_files[@]})
fi

password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" "$@")

[[ -n $password ]] || exit

if [[ $infer_otp -eq 1 ]]; then
    [[ "$password" =~ $otp_pattern ]] && otp=1
fi

otp_arg=''
if [[ $otp -eq 1 ]]; then
    otp_arg='otp'
fi

if [[ $typeit -eq 0 ]]; then
	pass $otp_arg show -c "$password" 2>/dev/null
else
	pass $otp_arg show "$password" | { IFS= read -r pass; printf %s "$pass"; } |
		$xdotool
fi