[pass] Add PASSWORD_STORE_CLIP_LOOPS option

Jamie Nguyen j at jamielinux.com
Mon Nov 9 17:29:06 CET 2015


>From 290a44a8c7cb4e2593318c2633b521059069bf00 Mon Sep 17 00:00:00 2001
From: Jamie Nguyen <j at jamielinux.com>
Date: Mon, 9 Nov 2015 15:11:30 +0000
Subject: [PATCH] Add PASSWORD_STORE_CLIP_LOOPS option

The xclip `-loop` option specifies the number of pastes to allow before
exiting. The default value is 0, which means unlimited pastes.

A user may want to copy password just once using `-loops 1`, to avoid
accidentally pasting it again somewhere unexpected. In this case, the
user can set PASSWORD_STORE_CLIP_LOOPS=1 (or any positive integer).

One issue is that the previous clipboard contents are not restored until
after PASSWORD_STORE_CLIP_TIME seconds, even if all pastes have been used.
This is mentioned in the man page.
---
 man/pass.1            |  6 ++++++
 src/password-store.sh | 13 ++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index e1fe605..c79288d 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -408,6 +408,12 @@ Overrides the selection passed to \fBxclip\fP, by default \fIclipboard\fP. See
 .BR xclip (1)
 for more info.
 .TP
+.I PASSWORD_STORE_CLIP_LOOPS
+Specifies the number of pastes to allow. By default this is set to 0, which allows
+unlimited pastes (but not beyond \fIPASSWORD_STORE_CLIP_TIME\fP seconds). Note
+that the previous clipboard contents are not restored until after
+\fIPASSWORD_STORE_CLIP_TIME\fP seconds even if all pastes have been used.
+.TP
 .I PASSWORD_STORE_CLIP_TIME
 Specifies the number of seconds to wait before restoring the clipboard, by default
 \fI45\fP seconds.
diff --git a/src/password-store.sh b/src/password-store.sh
index d535a74..1b38847 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -15,6 +15,7 @@ which gpg2 &>/dev/null && GPG="gpg2"
 PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
 X_SELECTION="${PASSWORD_STORE_X_SELECTION:-clipboard}"
 CLIP_TIME="${PASSWORD_STORE_CLIP_TIME:-45}"
+CLIP_LOOPS="${PASSWORD_STORE_CLIP_LOOPS:-0}"
 
 export GIT_DIR="${PASSWORD_STORE_GIT:-$PREFIX}/.git"
 export GIT_WORK_TREE="${PASSWORD_STORE_GIT:-$PREFIX}"
@@ -134,11 +135,11 @@ clip() {
 	local sleep_argv0="password store sleep on display $DISPLAY"
 	pkill -f "^$sleep_argv0" 2>/dev/null && sleep 0.5
 	local before="$(xclip -o -selection "$X_SELECTION" 2>/dev/null | base64)"
-	echo -n "$1" | xclip -selection "$X_SELECTION" || die "Error: Could not copy data to the clipboard"
+	echo -n "$1" | xclip -l $CLIP_LOOPS -selection "$X_SELECTION" || die "Error: Could not copy data to the clipboard"
 	(
 		( exec -a "$sleep_argv0" sleep "$CLIP_TIME" )
 		local now="$(xclip -o -selection "$X_SELECTION" | base64)"
-		[[ $now != $(echo -n "$1" | base64) ]] && before="$now"
+		[[ -n $now ]] && [[ $now != $(echo -n "$1" | base64) ]] && before="$now"
 
 		# It might be nice to programatically check to see if klipper exists,
 		# as well as checking for other common clipboard managers. But for now,
@@ -151,7 +152,13 @@ clip() {
 
 		echo "$before" | base64 -d | xclip -selection "$X_SELECTION"
 	) 2>/dev/null & disown
-	echo "Copied $2 to clipboard. Will clear in $CLIP_TIME seconds."
+	echo -n "Copied $2 to clipboard. Will clear "
+	if [[ $CLIP_LOOPS -gt 0 ]]; then
+		local suffix=""
+		[[ $CLIP_LOOPS -gt 1 ]] && suffix="s"
+		echo -n "after $CLIP_LOOPS paste$suffix, or "
+	fi
+	echo "in $CLIP_TIME seconds."
 }
 tmpdir() {
 	[[ -n $SECURE_TMPDIR ]] && return
-- 
2.5.0



More information about the Password-Store mailing list