pwu (3470B)
1 #!/usr/bin/env bash 2 3 source ~/.config/scripts/properties 4 5 shopt -s nullglob globstar 6 7 typeit=0 8 no_console=0 9 10 while getopts ":tc" arg; do 11 case $arg in 12 t) typeit=1;; 13 c) no_console=1;; 14 esac 15 done 16 17 shift $((OPTIND-1)) 18 19 value=$1 20 21 if [[ -n $WAYLAND_DISPLAY ]]; then 22 dmenu=dmenu-wl 23 xdotool="ydotool type --file -" 24 elif [[ -n $DISPLAY ]]; then 25 dmenu=dmenu 26 xdotool="xdotool type --clearmodifiers --file -" 27 else 28 echo "Error: No Wayland or X11 display detected" >&2 29 exit 1 30 fi 31 32 prefix=${PASSWORD_STORE_DIR-~/.password-store} 33 password_files=( "$prefix"/**/*.gpg ) 34 password_files=( "${password_files[@]#"$prefix"/}" ) 35 password_files=( "${password_files[@]%.gpg}" ) 36 37 function use_file () { 38 local OPTIND 39 quiet=0 40 41 while getopts ":q" arg; do 42 case $arg in 43 q) quiet=1;; 44 esac 45 done 46 shift $((OPTIND-1)) 47 password=$1 48 49 if [[ "$password" == otp/* ]]; then 50 use_otp -f "$password" 51 fi 52 53 creds=$(pass show "${password}") 54 if [[ -z "$creds" ]]; then 55 exit 1 56 fi 57 58 clip_or_type_pass "${password}" "${creds}" 59 60 url=$(echo "${creds}" | sed -n 3p) 61 62 if [[ "$url" ]]; then 63 if [[ $quiet == 0 ]]; then 64 if [[ $typeit -eq 0 ]]; then 65 openUrl=$(printf "yes\nno" | "$dmenu" -fn "$dmenu_font" -i -p "Open $url in browser?") 66 if [[ "$openUrl" == yes ]]; then 67 xdg-open "${url}" >/dev/null 2>&1 & disown 68 fi 69 fi 70 use_otp $password 71 else 72 if [[ $typeit -eq 0 ]]; then 73 xdg-open "${url}" >/dev/null 2>&1 & disown 74 fi 75 use_otp -q $password 76 fi 77 fi 78 exit 1; 79 } 80 81 function use_otp () { 82 local OPTIND 83 quiet=0 84 fast=0 85 86 while getopts ":qf" arg; do 87 case $arg in 88 q) quiet=1;; 89 f) fast=1;; 90 esac 91 done 92 shift $((OPTIND-1)) 93 file=$1 94 95 if [[ "$file" != otp/* ]]; then 96 otp_files=( $( printf '%s\n' "${password_files[@]}" | grep 'otp/*' ) ) 97 for otp_file in "${otp_files[@]}" 98 do 99 if [[ "$otp_file" == "otp/${file}" ]] ; then 100 file="${otp_file}" 101 break; 102 fi 103 done 104 fi 105 106 if [[ $fast == 1 ]]; then 107 clip_or_type_otp "$file" 108 else 109 [[ $no_console == 0 ]] || exit 110 if [[ $quiet == 0 ]]; then 111 read -p "Press enter to continue" 112 copyOtp=$(printf "yes\nno" | "$dmenu" -fn "$dmenu_font" -i -p "Copy OTP to clipboard?") 113 if [[ "$copyOtp" == yes ]]; then 114 clip_or_type_otp "$file" 115 fi 116 else 117 read -p "Press enter to copy OTP to clipboard." 118 clip_or_type_otp "$file" 119 fi 120 fi 121 122 exit 1 123 } 124 125 function clip_or_type_pass () { 126 password=$1 127 creds=$2 128 if [[ $typeit -eq 1 ]]; then 129 echo "${creds}" | sed -n 2p | { IFS= read -r pass; printf %s "$pass"; } | $xdotool 130 xdotool key Tab 131 echo "${creds}" | sed -n 1p | { IFS= read -r pass; printf %s "$pass"; } | $xdotool 132 else 133 pass -c "${password}" 2>/dev/null 134 echo "${creds}" | sed -n 2p | xclip 135 fi 136 } 137 138 function clip_or_type_otp () { 139 password=$1 140 if [[ $typeit -eq 0 ]]; then 141 pass otp -c "$password" 2>/dev/null 142 else 143 pass otp "$password" | { IFS= read -r pass; printf %s "$pass"; } | $xdotool 144 fi 145 } 146 147 148 if [ -n "${value}" ]; then 149 shift 150 let i=0 151 for file in "${password_files[@]/#otp*}" 152 do 153 if [[ "$file" =~ "${value}" ]] ; then 154 argFile="${file}" 155 let i++ 156 break 157 fi 158 done 159 160 if [ "$i" == 1 ]; then 161 use_file -q "${argFile}" 162 else 163 if [ "$i" == 0 ]; then 164 echo "Found no password files for \"${value}\"." 165 fi 166 if [ "$i" -gt 1 ]; then 167 echo "Found more than one possible password file for \"${value}\"." 168 fi 169 fi 170 else 171 value=$(printf '%s\n' "${password_files[@]}" | "$dmenu" -fn "$dmenu_font" -i ) 172 [[ -n $value ]] || exit 173 174 use_file "${value}" 175 fi