summaryrefslogtreecommitdiff
path: root/passmenu/passmenu
blob: eee435d8671661b91623ecce4ca7953385f5ec7b (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
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
#!/bin/sh

# !!!! UPDATE WITH ROFI GOODNESS

###
# passmenu - dmenu interface for password-store
#
# The popular, commonly used script for this purpose is available at
# <https://git.zx2c4.com/password-store/tree/contrib/dmenu/passmenu>
# (as of 2019-05-20).
# However, that script has been written in bash. This is my own, pure sh
# implementation. I believe it is slower and a lot less efficient. Nevertheless,
# it just works(TM). It is also tailored to my own liking.
#
# The script requires the following binaries in PATH to work:
# - pass (password-store, obviously)
# - dmenu (also rather obvious)
# - xsel (for the ability to copy the username only)
#
# The script has a manual included, but for the sake of redundancy, the options
# are also listed below. It accepts all options dmenu accepts and uses them as
# expected.
#
#     SYNOPSIS: passmenu [-hu] [dmenu options]
#         -h, --help        print usage
#         -u, --username    copy the username, not the password
#
# bertalanp99@ 2019-05-20
###

#################
### FUNCTIONS ###
#################

usage()
{
	printf 'usage: passmenu [-hu]'
}


#################
### VARIABLES ###
#################

prefix=${PASSWORD_STORE_DIR-$HOME/.password-store/}


##############
### SCRIPT ###
##############

### CHECK FOR BINARIES IN PATH ###

for binary in pass dmenu xsel
do
	if ! command -v $binary >/dev/null 2>&1
	then
		echo "this script needs '$binary' in PATH to work"
		exit 1
	fi
done

### PARSE OPTIONS ###

needuser=0
dmenuoptions=
while [ -n "$1" ]
do
	case $1 in
		-b|-f|-i|-v)
			dmenuoptions="$dmenuoptions $1"
			;;

		-l|-m|-p|-fn|-nb|-nf|-sb|-sf|-w)
			dmenuoptions="$dmenuoptions $1 $2"
			shift
			;;

		-c)
			cli=1
			;;

		-h)
			usage
			exit 0
			;;

		-u)
			needuser=1
			;;

		*)
			echo "unknown option -- $1"
			usage
			exit 1
			;;
	esac
	shift
done

### MAIN ###


if [ -n "$cli" ]
then
	password="$(\
	    find "$prefix" -name '*.gpg'          \
		| sed "s|${prefix}\(.*\).gpg|\1|" \
		| fzf)"
	if [ $needuser -eq 0 ]
	then
		pass show "$password" 2>/dev/null \
		    | sed 1q                      \
		    | tr -d '\n'                  \
		    | xsel --input
	else
		pass show "$password" 2>/dev/null \
		    | awk '/login:/{print $2}'   \
		    | tr -d '\n'                  \
		    | xsel --input
	fi
	return
fi

password="$(\
    find "$prefix" -name '*.gpg'          \
        | sed "s|${prefix}\(.*\).gpg|\1|" \
        | rofi -dmenu $dmenuoptions)"

if [ $needuser -eq 0 ]
then
	pass show "$password" 2>/dev/null \
	    | sed 1q                      \
	    | tr -d '\n'                  \
	    | xsel --input
else
	pass show "$password" 2>/dev/null \
	    | awk '/login:/{print $2}'    \
	    | tr -d '\n'                  \
	    | xsel --input
fi