willpower232

willpower232 / passmenu.sh

Last active 1 month ago

Like 0

dmenu script for pass

Revision 1020eda10073725f3adc2c471dcd5b247ce77ca7

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
9shopt -s nullglob globstar
10
11typeit=0
12include_username=1
13editit=0
14deps=0
15
16while [[ -n "$1" ]]; do
17 if [[ $1 == "--type" ]]; then
18 typeit=1
19 elif [[ $1 == "--no-username" ]]; then
20 include_username=0
21 elif [[ $1 == "--edit" ]]; then
22 editit=1
23 elif [[ $1 == "--install-dependencies" ]]; then
24 deps=1
25 elif [[ $1 == "--" ]]; then
26 break;
27 fi
28 shift
29done
30
31if [[ $deps -eq 1 ]]; then
32 sudo apt install dmenu xclip xdotool -y
33 exit
34fi
35
36if ! [ -x "$(command -v dmenu)" ]; then
37 echo 'Error: dmenu is not installed.' >&2
38 exit 1
39fi
40
41prefix=${PASSWORD_STORE_DIR-~/.password-store}
42password_files=( "$prefix"/**/*.gpg )
43password_files=( "${password_files[@]#"$prefix"/}" )
44password_files=( "${password_files[@]%.gpg}" )
45
46password=$(printf '%s\n' "${password_files[@]}" | dmenu -i "$@")
47
48if [[ $editit -eq 1 ]]; then
49 # Insert the edit command for your preferred terminal here:
50 #termite -e "pass edit '$password' > /dev/null"
51 #xterm "pass edit '$password' > /dev/null"
52 #urxvt -e "pass edit '$password' > /dev/null"
53 #gnome-terminal -- pass edit "$password" > /dev/null
54 # ..etc
55 exit
56fi
57
58[[ -n $password ]] || exit
59
60if ! [ -x "$(command -v xclip)" ]; then
61 echo 'Error: xclip is not installed.' >&2
62 exit 1
63fi
64
65if [[ $typeit -eq 0 ]]; then
66 pass show -c "$password" | xclip
67
68else
69 if ! [ -x "$(command -v xdotool)" ]; then
70 echo 'Error: xdotool is not installed.' >&2
71 exit 1
72 fi
73
74 pwfile=$(pass show "$password")
75 password=$(printf "%s" "$pwfile" | sed -n 1p)
76 username=$(printf "%s" "$pwfile" | grep -Po "^[Uu]ser(name)?: \K(.*)")
77 printf '%s' "$password" | xclip -selection clipboard
78 print '%s' "$password" | xclip
79 if [ -z "$username" ] || [[ $include_username -eq 0 ]]; then
80 printf '%s' "$password" | xdotool type --clearmodifiers --file -
81 else
82 pstr=$(printf '%s\t%s' "$username" "$password")
83 xdotool type "$pstr"
84 fi
85fi
86