#! /bin/bash GENERATION_HINT="Generate with default config" _save_to_clipboard() { # Copy the given password into PRIMARY selection and kill it in 30s echo "$1" | sed -n 1p | tr -d '\n' | xsel -i -t 30000 # Copy the second non-empty line (most of the time, account name) into CLIPBOARD selection echo "$1" | sed -n '1d;/^./{p;q}' | tr -d '\n' | xsel -ib } # Show extra message _show_extra() { EXTRA="$(echo; echo; echo "$1" | sed '1d' | sed '1{/^$/d}')" coproc ( rofi -e "Copied: $2$EXTRA" ) } # Generate a new password if ROFI_DATA is given by last execution if [ -n "${ROFI_DATA}" ]; then PASSWORD="${ROFI_DATA}" export ROFI_DATA= # If custom arguments for password generation is given # Set environment variable if [ ${ROFI_RETV} -eq 2 ]; then extra_arguments="${1}" fi # Generate password in a new process # And Use current script to copy the values info X11 selections coproc ( pass generate ${PASSWORD} ${extra_arguments} $terminal -e pass edit ${PASSWORD} $0 ${PASSWORD} ) exit 0 fi # Give rofi available passwords and exit if [ $# -eq 0 ]; then # Specify rofi prompt as 'Get' echo -e "\0prompt\x1fGet" find $HOME/.password-store -name '*gpg' -printf %P\\n | \ sed 's/.gpg$//' exit 0 fi # Test password exists or not RESULT="$(pass $1)" set -x if [ -n "$RESULT" ]; then _save_to_clipboard "$RESULT" _show_extra "$RESULT" "$1" elif [ -f "$HOME/.password-store/$1.gpg" ]; then # Password exist but command fails, must be something wrong with GPG decryption # Most of the time it is because passphrase is not cached by gpg-agent # Use zenity for passphrase input coproc( RESULT="$( zenity --password | gpg --pinentry-mode loopback --passphrase-fd 0 -d "$HOME/.password-store/$1.gpg")" _save_to_clipboard "$RESULT" _show_extra "$RESULT" "$1" ) else # Password doesn't exist yet # Go to next rofi execution echo -e "\0prompt\x1f$1" echo -e "\0message\x1f\"NUMBER\" for length, \"-n\" to exclude spcecial chars" echo -e "\0data\x1f$1" echo ${GENERATION_HINT} fi