summaryrefslogtreecommitdiff
path: root/tw/home/files/passmenu
diff options
context:
space:
mode:
Diffstat (limited to 'tw/home/files/passmenu')
-rwxr-xr-xtw/home/files/passmenu91
1 files changed, 91 insertions, 0 deletions
diff --git a/tw/home/files/passmenu b/tw/home/files/passmenu
new file mode 100755
index 00000000..abeb98fb
--- /dev/null
+++ b/tw/home/files/passmenu
@@ -0,0 +1,91 @@
+#!/bin/sh -e
+
+usage() {
+ cat << EOF
+$(basename "$0") [-c | -p | -a]
+
+ -c, --clip copy the user-selected password to the clipboard
+ -p, --type-pass auto-type the user-selected password only
+ -a, --type-all auto-type the user-selected username <tab> password
+ -h, --help show this help message and exit
+ -v, --version show the program version number and exit
+
+Later options override conflicting earlier ones.
+If no option is given, -c/--clip is the default.
+GNU-style combination of short options (e.g. -pa) is not supported.
+
+Abnormal exit codes:
+ 1 no password selected by user
+ 2 invalid command-line argument
+ 3 internal error
+
+(C) 2019-2022 Timo Wilken; MIT Licence.
+Adapted from https://git.zx2c4.com/password-store/tree/contrib/dmenu/passmenu.
+EOF
+}
+
+version() {
+ # Changelog:
+ # 1.0.0 (2019-??-??) initial version
+ # 1.0.1 (2022-07-17) script formatting; throw away passwords asap
+ # 1.0.2 (2022-10-??) remove fix_xdotool; this should be done on login
+ echo "$(basename "$0") 1.0.2"
+}
+
+password_names() {
+ find "${PASSWORD_STORE_DIR-$HOME/.password-store}" -name '*.gpg' -type f \
+ -printf '%P\n' | sed 's/\.gpg$//'
+}
+
+extract_key() {
+ sed -rn "s/^$1:\\s+(.+)\$/\\1/p"
+}
+
+type_stdin() {
+ tr -d '\n' | xdotool getactivewindow type --clearmodifiers --file -
+}
+
+## Command-line arguments
+mode=clip
+while [ $# -gt 0 ]; do
+ case "$1" in
+ -o|--type-otp) mode=type-otp; shift;;
+ -p|--type-pass) mode=type-pass; shift;;
+ -a|--type-all) mode=type-all; shift;;
+ -c|--clip) mode=clip; shift;;
+ -h|--help) usage; exit;;
+ -v|--version) version; exit;;
+
+ *)
+ echo "$(basename "$0"): unknown option: $1" >&2
+ usage >&2
+ exit 2;;
+ esac
+done
+
+## Password selection menu
+password_name=$(password_names | rofi -dmenu -i -p Password -no-custom)
+[ -n "$password_name" ] || exit 1
+
+## Password typing
+case "$mode" in
+ clip)
+ # Suppress "copied ... to clipboard" notice on stdout.
+ pass show --clip "$password_name" > /dev/null;;
+
+ type-otp)
+ pass otp "$password_name" | type_stdin;;
+
+ type-*)
+ entry=$(pass show "$password_name")
+ if [ "$mode" = type-all ]; then
+ echo "$entry" | extract_key username | type_stdin
+ xdotool getactivewindow key Tab
+ fi
+ echo "$entry" | head -1 | type_stdin
+ unset entry;;
+
+ *)
+ echo "$(basename "$0"): internal error: unknown mode: $mode" >&2
+ exit 3;;
+esac