dotfiles/.local/bin/tools/passmenu2

82 lines
1.9 KiB
Plaintext
Raw Normal View History

2020-07-04 14:23:27 +02:00
#!/usr/bin/env bash
shopt -s nullglob globstar
typeit=0
if [[ $1 == "--type" ]]; then
typeit=1
shift
fi
STARTDIR=${PASSWORD_STORE_DIR-~/.password-store}
BASEDIR=$STARTDIR
DONE=0
LEVEL=0
PREVSELECTION=""
SELECTION=""
2023-10-12 15:12:12 +02:00
DEFAULT_USER="alexander@bocken.org"
2020-07-04 14:23:27 +02:00
while [ "$DONE" -eq 0 ] ; do
password_files=( "$STARTDIR"/* )
password_files=( "${password_files[@]#"$STARTDIR"/}" )
password_files=( "${password_files[@]%.gpg}" )
if [ "$LEVEL" -ne 0 ] ; then
password_files=(".." "${password_files[@]}")
fi
entry=$(printf '%s\n' "${password_files[@]}" | grep -v 'mutt'| dmenu -i -p "Select a password:" "$@" -l 15)
echo "entry: $entry"
if [ -z "$entry" ] ; then
DONE=1
exit
fi
if [ "$entry" != ".." ] ; then
PREVSELECTION=$SELECTION
SELECTION="$SELECTION/$entry"
# check if another dir
if [ -d "$STARTDIR/$entry" ] ; then
STARTDIR="$STARTDIR/$entry"
LEVEL=$((LEVEL+1))
else
# not a directory so it must be a real password entry
if [[ $typeit -eq 0 ]]; then
2021-01-12 21:33:32 +01:00
if echo "$SELECTION" | grep -q '^/OTP'; then
pass otp show -c "$SELECTION" > $HOME/.cache/passstring
else
pass show -c "$SELECTION" > $HOME/.cache/passstring
2023-10-12 15:12:12 +02:00
if [ $? -eq 0 ]; then
var=$(pass show "$SELECTION" | awk 'NR == 2 {print $NF}')
if [ -z $var ]; then
var=$DEFAULT_USER
fi
echo $var | xclip -r -i -selection primary
else
exit 1
fi
2021-01-12 21:33:32 +01:00
fi
sed -e "s/^/\"/" -e "s/$/\"/" $HOME/.cache/passstring | xargs -r notify-send "Password" -t 4000
sleep 45
if [ -f $HOME/.cache/passstring ]; then
[ "$(wc -l "$HOME/.cache/passstring" | cut -d' ' -f1 )" -gt 0 ] &&
notify-send "Password" "password cleared from clipboard" -t 4000
rm $HOME/.cache/passstring
fi
2020-07-04 14:23:27 +02:00
else
xdotool - <<<"type --clearmodifiers -- $(pass show "$SELECTION" | head -n 1)"
fi
DONE=1
fi
else
LEVEL=$((LEVEL-1))
SELECTION=$PREVSELECTION
STARTDIR="$BASEDIR/$SELECTION"
fi
done