3 커밋 a8f96f8586 ... cc831c17ee

작성자 SHA1 메시지 날짜
  BezierQuadratic cc831c17ee Now it actually works 5 달 전
  BezierQuadratic 8be72b1f47 Added option to add OTP 6 달 전
  BezierQuadratic fb0fb1b4b7 More robust line number-based password line selection 6 달 전
1개의 변경된 파일45개의 추가작업 그리고 5개의 파일을 삭제
  1. 45 5
      neopassmenu.sh

+ 45 - 5
neopassmenu.sh

@@ -17,6 +17,26 @@ checkmenu(){
 	done
 }
 
+fileopener(){
+	editor="$(xdg-mime query default text/plain)"
+
+	[ -f "$HOME/.local/share/applications/${editor}" ] && path="$HOME/.local/share/applications/${editor}"
+
+	[ -z "$path" ] && [ -f "/usr/local/share/applications/${editor}" ] && path="/usr/local/share/applications/${editor}"
+
+	[ -z "$path" ] && [ -f "/usr/share/applications/${editor}" ] && path="/usr/share/applications/${editor}"
+
+	if [ -z "$path" ]; then
+		echo "Text editor cannot be found" >&2
+	fi
+
+	if grep -q "^Terminal=true$" "$path"; then
+		eval "${TERMINAL_EXEC:-xterm -e} $(grep "^Exec=" "$path" | head -n 1 | sed "{s/^Exec=//; s#%.#$1#}")"
+	else
+		eval "$(grep "^Exec=" "$path" | head -n 1 | sed "{s/^Exec=//; s#%.#$1#}")"
+	fi
+}
+
 if [ -n "$WAYLAND_DISPLAY" ]; then
 	x11=false
 	if [ -z "$DMENU_COMMAND" ]; then
@@ -62,7 +82,7 @@ if [ -z "$dmenu" ]; then
 fi
 
 
-usage="$(printf "USAGE:\nneopassmenu [OPTS] [Pass Entry]\n\nOptions:\n-a\t\tGenerate or modify a password\n-c\t\tCopy to clipboard instead of printing to stdout\n-t\t\tSimulate typing instead of printing to stdout\n-m\t\tSelect a specific line from a multiline file\n-o\t\tGet an otp instead of the password file (precedence over -m)\n-h\t\tPrint usage information\n\nCONFIGURATION:\nEnvironement variables:\nDMENU_COMMAND\tSpecify dmenu command (default: dmenu)")"
+usage="$(printf "USAGE:\nneopassmenu [OPTS] [Pass Entry]\n\nOptions:\n-a\t\tGenerate or modify a password\n-c\t\tCopy to clipboard instead of printing to stdout\n-t\t\tSimulate typing instead of printing to stdout\n-m\t\tSelect a specific line from a multiline file\n-o\t\tGet an otp instead of the password file (precedence over -m)\n-h\t\tPrint usage information\n\nCONFIGURATION:\nEnvironement variables:\nDMENU_COMMAND\tSpecify dmenu command (default: dmenu)\nTERMINAL\tSpecify the terminal command (default: xterm -e)")"
 
 cd "${PASSWORD_STORE_DIR:-"$HOME/.password-store"}" || exit 2
 
@@ -138,15 +158,31 @@ if [ -z "$file" ]; then
 fi
 
 if "$add"; then
+	if $otp; then
+		if ! [ -f "${file}.gpg" ]; then
+			echo "Can't add otp to non-existent password"
+			exit 1
+		else
+			echo "Insert OTP uri" > /tmp/pass.txt
+			fileopener /tmp/pass.txt || notify-send "Error $?"
+			if [ -n "$(cat /tmp/pass.txt)" ]; then
+				sed p /tmp/pass.txt | pass otp add "${file}"
+				rm /tmp/pass.txt
+				exit $?
+			fi
+			rm /tmp/pass.txt
+		fi
+		exit 1
+	fi
 	if [ -f "${file}.gpg" ]; then
 		pass "$file" > /tmp/pass.txt
 	fi
 	printf "\n\n%s" "$(dd count=2 if=/dev/random of=/dev/stdout 2>/dev/null ibs=512 obs=512 | uuencode -m /dev/stdout | tail -n +2 | tr -d '\n')" >> /tmp/pass.txt
 	fileopener /tmp/pass.txt || notify-send "Error $?"
 	if [ -n "$(cat /tmp/pass.txt)" ]; then
-		save="$(printf "yes\nno" | $dmenu $prompt_prefix "Save changes? ")"
+		save="$(printf "yes\nno" | $dmenu $prompt_prefix"Save changes? ")"
 		if [ "$save" = "yes" ]; then
-			cat /tmp/pass.txt | pass add -m -f "$file"
+			pass add -m -f "$file" < /tmp/pass.txt
 		fi
 		rm /tmp/pass.txt
 		exit 0
@@ -166,8 +202,12 @@ else
 fi
 
 if $multi && ! $otp; then
-	sel_password="$(echo "$password_lines" | sed 'h; s/^.\{1,2\}//; s/./*/g; x; s/^\(.\{1,2\}\).*/\1/; G; s/\n//' | $dmenu)"
-	password_lines="$(echo "$password_lines" | grep -x "$(echo "$sel_password" | sed 'y/*/./' )")"
+	sel_password="$(echo "$password_lines" | sed '{h; s/^.\{1,2\}//; s/./*/g; x; s/^\(.\{1,2\}\).*/\1/; G; s/\n//}' | nl -b a -w 1 -s " " | $dmenu | cut -d " " -f 1)"
+	if echo "$sel_password" | grep -qE "^[0-9]+$"; then
+		password_lines="$(echo "$password_lines" | sed -n "$(echo "$sel_password" | cut -d " " -f 1){p;q}")"
+	else
+		password_lines=""
+	fi
 
 	# Sed script:
 	# Copy pattern space (line) to hold space. Pattern space: Foo Hold space: Foo