willpower232

willpower232 / passmenu.sh

Last active 1 month ago

Like 0

dmenu script for pass

Revision 6309b94059bf401643295ee70a432529ddb76619

passmenu.sh Raw
1#!/usr/bin/env bash
2
3# 1 - source copy from https://geluk.io/p/passmenu.sh (https://github.com/Baggykiin/pass-winmenu)
4# 2 - make dmenu case insensitive
5# 3 - correct include_username variable
6# 4 - only pick out usernames if they are the start of the sentence, i.e. ignore non-primary usernames listed in the file (mostly for bitbucket)
7# 5 - dependency test and install
8# 6 - error output on bad argument
9
10shopt -s nullglob globstar
11
12typeit=0
13include_username=1
14editit=0
15deps=0
16
17while [[ -n "$1" ]]; do
18 if [[ $1 == "--type" ]]; then
19 typeit=1
20 elif [[ $1 == "--no-username" ]]; then
21 include_username=0
22 elif [[ $1 == "--edit" ]]; then
23 editit=1
24 elif [[ $1 == "--install-dependencies" ]]; then
25 deps=1
26 elif [[ $1 == "--" ]]; then
27 break;
28 else
29 echo "Error: unhandled argument $1." >&2
30 exit 1
31 fi
32 shift
33done
34
35if [[ $deps -eq 1 ]]; then
36 sudo apt install dmenu xclip xdotool -y
37 exit
38fi
39
40if ! [ -x "$(command -v dmenu)" ]; then
41 echo 'Error: dmenu is not installed.' >&2
42 exit 1
43fi
44
45prefix=${PASSWORD_STORE_DIR-~/.password-store}
46password_files=( "$prefix"/**/*.gpg )
47password_files=( "${password_files[@]#"$prefix"/}" )
48password_files=( "${password_files[@]%.gpg}" )
49
50password=$(printf '%s\n' "${password_files[@]}" | dmenu -i "$@")
51
52if [[ $editit -eq 1 ]]; then
53 # Insert the edit command for your preferred terminal here:
54 #termite -e "pass edit '$password' > /dev/null"
55 #xterm "pass edit '$password' > /dev/null"
56 #urxvt -e "pass edit '$password' > /dev/null"
57 #gnome-terminal -- pass edit "$password" > /dev/null
58 # ..etc
59 exit
60fi
61
62[[ -n $password ]] || exit
63
64if ! [ -x "$(command -v xclip)" ]; then
65 echo 'Error: xclip is not installed.' >&2
66 exit 1
67fi
68
69if [[ $typeit -eq 0 ]]; then
70 pass show -c "$password" | xclip
71
72else
73 if ! [ -x "$(command -v xdotool)" ]; then
74 echo 'Error: xdotool is not installed.' >&2
75 exit 1
76 fi
77
78 pwfile=$(pass show "$password")
79 password=$(printf "%s" "$pwfile" | sed -n 1p)
80 username=$(printf "%s" "$pwfile" | grep -Po "^[Uu]ser(name)?: \K(.*)")
81 printf '%s' "$password" | xclip -selection clipboard
82 print '%s' "$password" | xclip
83 if [ -z "$username" ] || [[ $include_username -eq 0 ]]; then
84 printf '%s' "$password" | xdotool type --clearmodifiers --file -
85 else
86 pstr=$(printf '%s\t%s' "$username" "$password")
87 xdotool type "$pstr"
88 fi
89fi
90