aboutsummaryrefslogtreecommitdiff
path: root/dpass
blob: 99654b2636b826606cf38ce031e7792302fa27e1 (plain) (blame)
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
#!/bin/sh

pass_store_dir="${PASSWORD_STORE_DIR:-$HOME/.pass}"
menu="wmenu"

note()
{
	: "${1:?}"

	command -v notify-send > /dev/null &&
		notify-send "󰟵  dpass" "$1"
}

die()
{
	: "${1:?}"

	note "$1"

	printf "\033[31;1merr: %b\033[0m\n" "$1"
	exit "${2:-1}"
}

dep_check()
{
	: "${1:?}"

	for dep; do
		command -v "$dep" 1>/dev/null ||
			die "$dep not found, please install it" 127
	done

	unset dep
}

search()
{
	# usage: search directory
	pass_path="${1:?}"

	while [ -d "$pass_path" ]
	do
		pass_path="${pass_path}/$(printf '%s\n' "$pass_path"/* |
			sed 's/.*\///g' |
			"$menu" -l 25 -p "󰟵 ${pass_path##*/} ")"

		[ "$?" != 0 ] &&
			die "noting selected"
	done

	pass_path="${pass_path#"${pass_store_dir}"/}"
	pass_path="${pass_path%.gpg}"
}

main()
{
	pass_path=

	[ -z "$WAYLAND_DISPLAY" ] &&
		menu="dmenu"

	dep_check "$menu" "pass"

	case "$1" in
	-o)
		search "$pass_store_dir"
		pass "$pass_path"
		;;
	-c)
		if [ -z "$WAYLAND_DISPLAY" ]
		then
			dep_check "xclip"
		else
			dep_check "wl-copy"
		fi

		search "$pass_store_dir"
		pass --clip "$pass_path"
		note "password copied to clipboard, will clear in 45 seconds."
		;;
	-h|--help)
		cat <<- EOF
			Usage: dpass command
			dmenu wrapper for the standard unix password manager
			Commands:
			  -h	show this help cruft
			  -o	print the password to stdout
			  -c	coppy the password to clipboard
			  -t	type the password (default behaviour)
		EOF
		;;
	""|-t)
		if [ -z "$WAYLAND_DISPLAY" ]
		then
			dep_check "xdotool"
			search "$pass_store_dir"
			xdotool type --delay 20 "$(pass "$pass_path")"
		else
			dep_check "wtype"
			search "$pass_store_dir"
			wtype -d 20 "$(pass "$pass_path")"
		fi
		;;
	*)
		die "${0##*/} $1, invalid usage"
		;;
	esac
}

main "$@"