started refactoring

This commit is contained in:
Rasmus Steinke 2015-11-14 05:27:26 +01:00
parent 4c43d92c32
commit 908bcd4586

349
rofi-pass
View File

@ -17,89 +17,154 @@ list_passwords() {
}
doClip () {
if [[ $clip == "primary" ]]; then
xclip
elif [[ $clip == "clipboard" ]]; then
xclip -selection clipboard
elif [[ $clip == "both" ]]; then
xclip
xclip -o | xclip -selection clipboard
if [[ $clip == "primary" ]]; then xclip;
elif [[ $clip == "clipboard" ]]; then xclip -selection clipboard;
elif [[ $clip == "both" ]]; then xclip; xclip -o | xclip -selection clipboard;
fi
}
checkIfPass () {
if [[ $selected_password == "[ Add Entry ]>" ]]; then
mainMenu
elif [[ $selected_password == "[ Manage Database ]>" ]]; then
mainMenu
elif [[ $selected_password == "---" ]]; then
mainMenu
if [[ $selected_password == "[ Add Entry ]>" ]]; then mainMenu;
elif [[ $selected_password == "---" ]]; then mainMenu;
else
rm -f "$HOME/.config/rofi-pass/last_used"
echo "${root}: $selected_password" > "$HOME/.config/rofi-pass/last_used"
fi
}
autopass () {
checkIfPass
if [[ ${selected_password} == "[ Add Entry ]>" ]]; then insertPass;
elif [[ ${selected_password} == "---" ]]; then mainMenu;
else
if [[ -z "${stuff["$AUTOTYPE_field"]}" ]]; then
echo -n "${stuff["${USERNAME_field}"]}" | xdotool type --clearmodifiers --file -
xdotool key Tab
echo -n "${password}" | xdotool type --clearmodifiers --file -
sleep 1
if [[ ${auto_enter} == "true" ]]; then xdotool key Return; fi
else
echo "${stuff["$AUTOTYPE_field"]}"
for word in ${stuff["$AUTOTYPE_field"]}; do
if [[ $word == ":tab" ]]; then xdotool key Tab;
elif [[ $word == ":space" ]]; then xdotool key space;
elif [[ $word == ":delay" ]]; then sleep ${delay};
elif [[ $word == ":enter" ]]; then xdotool key Return;
elif [[ $word == "pass" ]]; then echo -n "${password}" | xdotool type --clearmodifiers --file -;
else echo -n "${stuff[${word}]}" | xdotool type --clearmodifiers --file -
fi
done
if [[ ${auto_enter} == "true" ]]; then xdotool key Return; fi
fi
fi
}
openURL () {
checkIfPass
$BROWSER "$(pass "$selected_password" | grep "${URL_field}: " | awk '{sub(/:/,"")}{print $2}1' | head -1)"; exit;
}
typeUser () {
checkIfPass
echo -n "${stuff[${USERNAME_field}]}" | xdotool type --clearmodifiers --file -
}
typePass () {
checkIfPass
echo -n "${password}" | xdotool type --clearmodifiers --file -
if [[ $notify == "true" ]]; then
if [[ "${stuff[notify]}" == "false" ]]; then
:
else notify-send "rofi-pass" "finished typing password";
fi
elif [[ $notify == "false" ]]; then
if [[ "${stuff[notify]}" == "true" ]]; then notify-send "rofi-pass" "finished typing password";
else
:
fi
fi
}
typeField () {
checkIfPass
echo -n "${stuff[${typefield}]}" | xdotool type --clearmodifiers --file -
}
copyUser () {
checkIfPass
echo -n "${stuff[${USERNAME_field}]}" | doClip
}
copyField () {
checkIfPass
echo -n "${stuff[${copyfield}]}" | doClip
}
copyURL () {
checkIfPass
echo -n "${stuff[${URL_field}]}" | doClip
}
copyPass () {
checkIfPass
echo -n "$password" | doClip
notify-send "rofi-pass" "Copied Password\nClearing in 45 seconds"
$(sleep 45; echo -n "" | xclip; echo "" | xclip -selection clipboard | notify-send "rofi-pass" "Clipboard cleared") &
}
viewEntry () {
checkIfPass
showEntry "${selected_password}"
}
generatePass () {
checkIfPass
symbols=$(echo -e "0 Cancel\n---\n1 Yes\n2 No" | rofi -dmenu -p "Use Symbols? > ")
if [[ $symbols == "0 Cancel" ]]; then mainMenu;
elif [[ $symbols == "1 Yes" ]]; then symbols="";
elif [[ $symbols == "2 No" ]]; then symbols="-n";
fi
HELP="<span color='$help_color'>Enter Number or hit Enter to use default length</span>"
length=$(echo -e "" | rofi -dmenu -mesg "${HELP}" -p "Password length? (Default: ${passlength}) > ")
if [[ $length == "" ]]; then pass generate ${symbols} -i "$selected_password" "${passlength}" > /dev/null;
else pass generate ${symbols} -i "$selected_password" "${length}" > /dev/null;
fi
}
# main Menu
mainMenu () {
help_text=$(echo -e "${autotype}: Autotype - ${copy_name}: Copy User - ${edit}: Edit - ${show}: Show Entry\n${type_user}: Type User - ${copy_pass}: Copy Pass - ${move}: Move - ${generate}: Generate Pass\n${type_pass}: Type Pass - ${copy_url}: Copy URL - ${delete}: Delete" | column -s '-' -t)
help_text=$(echo -e "${autotype}: Autotype - ${copy_menu}: Copy Field - ${show}: View\n${type_menu}: Type Field - ${action_menu}: Actions - ${help}: Help" | column -s '-' -t)
line1=$(echo "${help_text}" | head -1)
line2=$(echo "${help_text}" | tail -2 | head -1)
line2=$(echo "${help_text}" | head -2 | tail -1)
line3=$(echo "${help_text}" | tail -1)
HELP="<span color='$help_color'>${line1}
${line2}
${line3}</span>"
selected_password="$(echo -e "[ Add Entry ]>\n---\n$(list_passwords 2>/dev/null)" \
| _rofi -mesg "${HELP}" \
-dmenu -kb-custom-1 "${autotype}" \
-kb-custom-2 "${type_user}" \
-kb-custom-3 "${type_pass}" \
-kb-custom-4 "${open_url}" \
-kb-custom-5 "${copy_name}" \
-kb-custom-6 "${copy_pass}" \
-kb-custom-7 "${show}" \
-kb-custom-8 "${copy_url}" \
-kb-custom-9 "${edit}" \
-kb-custom-10 "${move}" \
-kb-custom-11 "${delete}" \
-kb-custom-12 "${generate}" \
-dmenu \
-select "$entry" \
-p "rofi-pass > ")"
rofi_exit=$?
if [[ -n $selected_password ]]; then
if [[ $selected_password == "[ Add Entry ]>" ]]; then
:
elif [[ $selected_password == "[ Manage Database ]>" ]]; then
:
elif [[ $selected_password == "---" ]]; then
:
else
rm -f "$HOME/.config/rofi-pass/last_used"
echo "${root}: $selected_password" > "$HOME/.config/rofi-pass/last_used"
fi
fi
if [[ "${rofi_exit}" -eq 0 ]]; then
if [[ ${selected_password} == "[ Add Entry ]>" ]]; then
insertPass
elif [[ ${selected_password} == "---" ]]; then
mainMenu
else
true
fi
elif [[ "${rofi_exit}" -eq 13 ]]; then
checkIfPass
$BROWSER "$(pass "$selected_password" | grep "${URL_field}: " | awk '{sub(/:/,"")}{print $2}1' | head -1)"; exit;
elif [[ "${rofi_exit}" -eq 1 ]]; then exit ${rofi_exit};
elif [[ "${rofi_exit}" -eq 10 ]]; then
checkIfPass
fi
selected_password="$(echo -e "[ Add Entry ]>\n---\n$(list_passwords 2>/dev/null)" \
| _rofi -mesg "${HELP}" \
-dmenu -kb-custom-1 "${autotype}" \
-kb-custom-2 "${type_user}" \
-kb-custom-3 "${type_pass}" \
-kb-custom-4 "${open_url}" \
-kb-custom-5 "${copy_name}" \
-kb-custom-6 "${copy_pass}" \
-kb-custom-7 "${show}" \
-kb-custom-8 "${copy_url}" \
-kb-custom-9 "${edit}" \
-kb-custom-13 "${type_menu}" \
-kb-custom-14 "${action_menu}" \
-kb-custom-15 "${copy_menu}" \
-kb-custom-16 "${help}" \
-dmenu \
-select "$entry" \
-p "rofi-pass > ")"
rofi_exit=$?
# generate Array of fields
password_temp=$(PASSWORD_STORE_DIR="${root}" pass "$selected_password")
password=$(echo "${password_temp}" | head -1)
declare -A stuff
while read LINE; do
_id=$(echo -e "${LINE}" | awk -F ':[[:space:]]*' '{print $1}')
_val=$(echo -e "${LINE}" | awk '{sub(/:/,"")}{for (i=2; i<NF; i++) printf $i " "; print $NF}')
@ -107,112 +172,22 @@ ${line3}</span>"
done < <(PASSWORD_STORE_DIR="${root}" pass "${selected_password}" | tail -n+2 | grep -P '(: |:\t)' )
stuff["pass"]=${password}
if [[ $rofi_exit -eq 11 ]]; then
checkIfPass
echo -n "${stuff[${USERNAME_field}]}" | xdotool type --clearmodifiers --file -
exit
elif [[ "${rofi_exit}" -eq 18 ]]; then
manageEntry edit;
elif [[ "${rofi_exit}" -eq 19 ]]; then
manageEntry move;
elif [[ "${rofi_exit}" -eq 20 ]]; then
manageEntry delete;
elif [[ "${rofi_exit}" -eq 21 ]]; then
checkIfPass
symbols=$(echo -e "0 Cancel\n---\n1 Yes\n2 No" | rofi -dmenu -p "Use Symbols? > ")
if [[ $symbols == "0 Cancel" ]]; then
mainMenu
elif [[ $symbols == "1 Yes" ]]; then
symbols=""
elif [[ $symbols == "2 No" ]]; then
symbols="-n"
fi
HELP="<span color='$help_color'>Enter Number or hit Enter to use default length</span>"
length=$(echo -e "" | rofi -dmenu -mesg "${HELP}" -p "Password length? (Default: ${passlength}) > ")
if [[ $length == "" ]]; then
pass generate ${symbols} -i "$selected_password" "${passlength}" > /dev/null
else
pass generate ${symbols} -i "$selected_password" "${length}" > /dev/null
fi
exit
elif [[ $rofi_exit -eq 12 ]]; then
checkIfPass
echo -n "${password}" | xdotool type --clearmodifiers --file -
if [[ $notify == "true" ]]; then
if [[ "${stuff[notify]}" == "false" ]]; then
:
else
notify-send "rofi-pass" "finished typing password"
fi
elif [[ $notify == "false" ]]; then
if [[ "${stuff[notify]}" == "true" ]]; then
notify-send "rofi-pass" "finished typing password"
else
:
fi
fi
exit
elif [[ $rofi_exit -eq 14 ]]; then
checkIfPass
echo -n "${stuff[${USERNAME_field}]}" | doClip
exit
elif [[ $rofi_exit -eq 17 ]]; then
checkIfPass
echo -n "${stuff[${URL_field}]}" | doClip
exit
elif [[ $rofi_exit -eq 16 ]]; then
checkIfPass
showEntry "${selected_password}"
elif [[ $rofi_exit -eq 15 ]]; then
checkIfPass
echo -n "$password" | doClip
notify-send "rofi-pass" "Copied Password\nClearing in 45 seconds"
$(sleep 45; echo -n "" | xclip; echo "" | xclip -selection clipboard | notify-send "rofi-pass" "Clipboard cleared") &
exit
# actions based on keypresses
if [[ "${rofi_exit}" -eq 0 ]]; then autopass;
elif [[ "${rofi_exit}" -eq 13 ]]; then openURL;
elif [[ "${rofi_exit}" -eq 1 ]]; then exit ${rofi_exit};
elif [[ "${rofi_exit}" -eq 10 ]]; then autopass;
elif [[ "${rofi_exit}" -eq 11 ]]; then typeUser;
elif [[ "${rofi_exit}" -eq 12 ]]; then typePass;
elif [[ "${rofi_exit}" -eq 14 ]]; then copyUser;
elif [[ "${rofi_exit}" -eq 17 ]]; then copyURL;
elif [[ "${rofi_exit}" -eq 16 ]]; then viewEntry;
elif [[ "${rofi_exit}" -eq 15 ]]; then copyPass;
elif [[ "${rofi_exit}" -eq 22 ]]; then typeMenu;
elif [[ "${rofi_exit}" -eq 23 ]]; then actionMenu;
elif [[ "${rofi_exit}" -eq 25 ]]; then helpMenu;
elif [[ "${rofi_exit}" -eq 24 ]]; then copyMenu;
fi
if [[ -z "${stuff["$AUTOTYPE_field"]}" ]]; then
echo -n "${stuff[${USERNAME_field}]}" | xdotool type --clearmodifiers --file -
xdotool key Tab
echo -n "${password}" | xdotool type --clearmodifiers --file -
sleep 1
if [[ ${auto_enter} == "true" ]]; then
xdotool key Return
fi
else
echo "${stuff["$AUTOTYPE_field"]}"
for word in ${stuff["$AUTOTYPE_field"]}; do
if [[ $word == ":tab" ]]; then
xdotool key Tab
elif [[ $word == ":space" ]]; then
xdotool key space
elif [[ $word == ":delay" ]]; then
sleep ${delay}
elif [[ $word == ":enter" ]]; then
xdotool key Return
elif [[ $word == "pass" ]]; then
echo -n "${password}" | xdotool type --clearmodifiers --file -
else
echo -n "${stuff[${word}]}" | xdotool type --clearmodifiers --file -
fi
#xdotool key Tab
done
if [[ ${auto_enter} == "true" ]]; then
xdotool key Return
fi
fi
password=''
selected_password=''
unset stuff
@ -222,6 +197,39 @@ ${line3}</span>"
unset stuff
}
helpMenu () {
help=$(echo -e "${type_user}: Type User
${type_pass}: Type Password
${type_menu}: Type Custom Field
${autotype}: Autotype
---
${copy_name}: Copy Username
${copy_pass}: Copy Password
${copy_url}: Copy URL
${copy_menu}: Copy Custom Field
---
${action_menu}: Edit, Move, Delete, Re-generate Submenu
${show}: Show Password File" | rofi -dmenu -p "Help > ")
}
typeMenu () {
typefield=$(pass "${selected_password}" | awk -F ':' '{ print $1 }' | grep -Ev '\-\-\-' | tail -n +2 | rofi -dmenu -p "Choose Field to type > ")
typeField
}
copyMenu () {
copyfield=$(pass "${selected_password}" | awk -F ':' '{ print $1 }' | grep -Ev '\-\-\-' | tail -n +2 | rofi -dmenu -p "Choose Field to type > ")
copyField
}
actionMenu () {
action=$(echo -e "1 Move Password File\n2 Delete Password File\n3 Generate New Password" | rofi -dmenu -p "Choose Action > ")
if [[ ${action} == "1 Move Password File" ]]; then manageEntry move;
elif [[ ${action} == "2 Delete Password File" ]]; then manageEntry delete;
elif [[ ${action} == "3 Generate New Password" ]]; then generatePass;
fi
}
showEntry () {
HELP="<span color='$help_color'>${type_entry}: Type Entry | ${copy_entry}: Copy Entry</span>"
bla=$(echo -e "0 Return\n---\n$(PASSWORD_STORE_DIR="${root}" pass "$selected_password")" | _rofi -dmenu -mesg "${HELP}" -p "> ")
@ -298,6 +306,7 @@ manageEntry () {
cd "${root}"
selected_password2=$(basename "$selected_password" .gpg)
group=$(find -type d -not -iwholename '*.git*' -printf '%d\t%P\n' | sort -r -nk1 | cut -f2- | _rofi -dmenu -p "Choose Group > ")
if [[ $group == "" ]]; then exit; fi
PASSWORD_STORE_DIR="${root}" pass mv "$selected_password" "${group}"
mainMenu