Skip to content

Instantly share code, notes, and snippets.

@aeghn
Last active November 2, 2022 15:53
Show Gist options
  • Save aeghn/15c2c13af27cfc9f0b48debadacbea9b to your computer and use it in GitHub Desktop.
Save aeghn/15c2c13af27cfc9f0b48debadacbea9b to your computer and use it in GitHub Desktop.
rofipass
#!/usr/bin/env bash
shopt -s nullglob globstar
set -o pipefail
export PASSWORD_STORE_DIR="path/to/password-store"
export EDITOR='vim'
PREV_FILE="$HOME/.cache/rofi-pass-cahce"
_rofi() {
rofi -i $@
}
getItems() {
local -a password_files
local prev_item
cd "$PASSWORD_STORE_DIR" || exit
password_files=( **/*.gpg )
PASSWORD_ITEMS=( "${password_files[@]%.gpg}" )
prev_item=$(cat "$PREV_FILE" 2> /dev/null)
if [[ $? -eq 0 ]] && [[ " ${PASSWORD_ITEMS[@]} " =~ " $prev_item " ]]; then
for((i=0; i<${#PASSWORD_ITEMS[@]}; i++)); do
[ "$prev_item" == "${PASSWORD_ITEMS[$i]}" ] && unset "PASSWORD_ITEMS[$i]"
done
PASSWORD_ITEMS=( "$prev_item" "${PASSWORD_ITEMS[@]}" )
fi
}
listItems() {
local -a kb_customs
local rofi_exit
kb_customs=(
-kb-custom-1 "alt+s" # pass
-kb-custom-2 "alt+d" # user
-kb-custom-3 "alt+a" # user,pass
-kb-custom-4 "alt+w" # List attrs
-kb-custom-5 "alt+e" # Edit current item
-kb-custom-6 "alt+r" # Add new item
-kb-custom-7 "alt+q" # Reload GPG agent
-kb-custom-8 "alt+h" # Show help
)
ITEM=$(printf '%s\n' "${PASSWORD_ITEMS[@]}" | _rofi -dmenu -p Pass ${kb_customs[@]})
rofi_exit=$?
[[ "$ITEM" =~ ^[[:space:]]*$ ]] && exit || echo "$ITEM" > "$PREV_FILE"
case $rofi_exit in
10) fillAttr pass ;;
11) fillAttr "USERNAME" ;;
12) fillAttr "USERNAME"
xdotool key Tab
fillAttr pass
reloadAgent
;;
13) listAttrs ;;
14) editItem ;;
15) newItem ;;
16) reloadAgent ;;
17) showHelp ;;
*) exit ;;
esac
}
listAttrs() {
local attrs
attrs=$( pass show "$ITEM" | sed 1d | egrep ":[[:space:]]" | cut -d":" -f1)
if [ -n "$attrs" ] ; then
fillAttr "$(printf '%s' "$attrs" | _rofi -dmenu -p "$ITEM@Pass")"
fi
}
fillAttr() {
local attr="$1"
fcitx-remote -c
if [ -z "$attr" ]; then
exit
elif [ "$attr" == pass ]; then
xdotool type --delay 100 --clearmodifiers $(pass show "$ITEM" | head -1)
else
xdotool type --delay 100 --clearmodifiers $(pass show "$ITEM" | grep "$attr: " | sed "s/$attr: //g")
fi
}
newItem() {
[[ " ${PASSWORD_ITEMS[@]} " =~ " $ITEM " ]] && die "Item $ITEM exists, so you cannot add this item."
printf '%s' "$(pwgen -B 14)
USERNAME: $(pwgen -B 9)
URL:
E-MAIL:
PHONE:
NOTE:"| pass insert -m "$ITEM"
termite -t CHin_pass -e "pass edit '$ITEM'"
}
editItem() {
[[ " ${PASSWORD_ITEMS[@]} " =~ " $ITEM " ]] || die "Item $ITEM doesn't exists, so you cannot edit this item."
termite -t CHin_pass -e "pass edit '$ITEM'"
}
reloadAgent() {
gpg-connect-agent reloadagent /bye
}
postAction() {
sleep 0.1
xdotool keyup Control
xdotool keyup Shift
xdotool keyup Alt
xdotool keyup Super
}
showHelp() {
local message
message='Alt+a -- Paste username and password
Alt+s -- Paste password
Alt+d -- Paste username
Alt+q -- Reload GPG Agent
Alt+w -- List attributes
Alt+e -- Edit select item
Alt+r -- Input name, then create this item by using template
Alt+h -- Show this help
'
_rofi -e "$message"
}
die() {
_rofi -e "$@"
exit 1
}
getItems
listItems
postAction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment