123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #!/usr/bin/env sh
- multi=false
- clipboard=false
- file=""
- dir=""
- dmenu="${DMENU_COMMAND:-dmenu}"
- usage="$(printf "USAGE:\nneopassmenu [OPTS] [Pass Entry]\n\nOptions:\n-c\t\tCopy to clipboard instead of printing to stdout\n-m\t\tSelect a specific line from a multiline file\n-h\\t\tPrint usage information\n\nCONFIGURATION:\nEnvironement variables:\nDMENU_COMMAND\tSpecify dmenu command (default: dmenu)")"
- cd "${PASSWORD_STORE_DIR:-"$HOME/.password-store"}" || exit 2
- while [ "$#" -gt 0 ]; do
- case "$1" in
- -c)
- clipboard=true
- shift
- ;;
- -m)
- multi=true
- shift
- ;;
- -h)
- echo "$usage"
- exit 0
- ;;
- *)
- if [ -f "$1" ] || [ -f "${1}.gpg" ] || [ -d "$1" ] ; then
- file="$1"
- shift
- elif [ -d "$1" ]; then
- dir="$1"
- shift
- else
- echo "$usage"
- exit 1
- fi
- ;;
- esac
- done
- while true; do
- file="$(printf "..\n%s" "$(find "./$dir" -maxdepth 1)" | sed -e 's#^'"./$dir"'##;s#^/##;s/.gpg$//' -e '/^$/d; /^.gpg-id$/d; /^.git\(attributes\)\{0,1\}$/d' | $dmenu)"
- if [ -z "$dir" ]; then
- newdir="$file"
- else
- newdir="${dir%/}/$file"
- fi
- if [ -z "$file" ]; then
- exit 127
- elif [ "$file" = ".." ]; then
- dir="${dir%/}"
- if echo "$dir" | grep -q "/"; then
- dir="${dir%/*}"
- else
- dir=""
- fi
- elif [ -d "$newdir" ]; then
- dir="$newdir"
- elif [ -f "$newdir" ] || [ -f "${newdir}.gpg" ]; then
- file="$newdir"
- break
- else
- break
- fi
- done
- if ! [ -f "$file" ] && ! [ -f "${file}.gpg" ]; then
- exit 127
- fi
- if $clipboard && ! $multi; then
- pass -c "$file"
- exit 0
- fi
- password_lines="$(pass "$file")"
- if ! $multi; then
- echo "$password_lines"
- exit 0
- fi
- password="$(echo "$password_lines" | sed '{ h; s/^.$//; s/^..//; s/./*/g; x; s/^\(..\).*/\1/; G; s/\n//; }' | $dmenu)"
- # Sed script:
- # Copy pattern space (line) to hold space. Pattern space: Foo Hold space: Foo
- # If the patter space is a single character, delete the character (prevents "a" to be outputted as "aa")
- # Delete the first two characters of the patter space. Pattern Space: o; Hold space: Foo
- # Replace all the characters in the pattern space with the literal "*". Pattern Space: *; Hold space: Foo
- # Swap pattern space with hold space. Pattern Space: Foo Hold space: *
- # Match the first two characters of the pattern space, then all the rest of the characters
- # substitute with the whole match (entire line) with the first submatch (first 2 characters). Pattern Space: Fo Hold Space: *
- # Append newline followed by the Hold Space to the Pattern Space. Pattern Space: Fo\n* Hold Space: *
- # Remove the (supposedly only) newline from the pattern space. Pattern Space: Fo* Hold Space: *
- # Sed automatically prints the pattern space at the end of a script (unless the -n option is given). Output: Fo*
- password="$(echo "$password_lines" | grep -xE "$(echo "$password" | sed 'y/*/./' )")"
- if "$clipboard"; then
- echo "$password" | xclip -selection clipboard -i -
- exit 0
- fi
- echo "$password"
|