Skip to content

Commit

Permalink
Merge pull request #198 from chrko/cleanup
Browse files Browse the repository at this point in the history
Fix handling of filenames with spaces (regression #179) + clean formating
  • Loading branch information
carnager authored Jul 23, 2020
2 parents 63e870e + 527bb3b commit 6e7fd8e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 68 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

indent_style = tab

[Makefile]
indent_style = tab

[*.md]
trim_trailing_whitespace = false

[*.py]
indent_style = space
indent_size = 4
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ in a convenient way using [rofi](https://github.com/DaveDavenport/rofi).
* xdotool
* gawk
* bash 4.x
* find
* pwgen
* [pass-otp](https://github.com/tadfisher/pass-otp) (optional: for OTPs)

Expand Down
145 changes: 77 additions & 68 deletions rofi-pass
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ has_qrencode() {
command -v qrencode >/dev/null 2>&1
}

# get all password files and create an array
listgpg () {
mapfile -d '' pw_list < <(find -L . -name '*.gpg' -print0)
pw_list=("${pw_list[@]#./}")
printf '%s\n' "${pw_list[@]}" | sort -n
}

# get all password files and output as newline-delimited text
list_passwords() {
cd "${root}" || exit
pw_list=($(find -L * -name "*.gpg"))
mapfile -t pw_list < <(listgpg)
printf '%s\n' "${pw_list[@]%.gpg}" | sort -n

}

doClip () {
Expand Down Expand Up @@ -102,7 +107,7 @@ autopass () {
":enter") xdotool key Return;;
":otp") printf '%s' "$(generateOTP)" | xdotool type --delay ${xdotool_delay} --clearmodifiers --file -;;
"pass") printf '%s' "${password}" | xdotool type --delay ${xdotool_delay} --clearmodifiers --file -;;
"path") printf '%s' "${selected_password}" | rev | cut -d'/' -f1 | rev | xdotool type --clearmodifiers --file -;;
"path") printf '%s' "${selected_password}" | rev | cut -d'/' -f1 | rev | xdotool type --clearmodifiers --file -;;
*) printf '%s' "${stuff[${word}]}" | xdotool type --delay ${xdotool_delay} --clearmodifiers --file -;;
esac
done
Expand Down Expand Up @@ -266,7 +271,8 @@ viewEntry () {
generatePass () {
askmenu_content=(
"Yes"
"No")
"No"
)

askGenMenu=$(printf '%s\n' "${askmenu_content[@]}" | _rofi -dmenu -p "Generate new Password for ${selected_password}? > ")
askgen_exit=$?
Expand All @@ -285,7 +291,8 @@ generatePass () {
symbols_content=(
"0 Cancel"
"1 Yes"
"2 No")
"2 No"
)

symbols=$(printf '%s\n' "${symbols_content[@]}" | _rofi -dmenu -p "Use Symbols? > ")
symbols_val=$?
Expand Down Expand Up @@ -319,11 +326,11 @@ generatePass () {
mainMenu () {
if [[ $1 == "--bmarks" ]]; then
selected_password="$(list_passwords 2>/dev/null \
| _rofi -mesg "Bookmarks Mode. ${switch} to switch" \
-dmenu \
-kb-custom-10 "${switch}" \
-select "$entry" \
-p "rofi-pass > ")"
| _rofi -mesg "Bookmarks Mode. ${switch} to switch" \
-dmenu \
-kb-custom-10 "${switch}" \
-select "$entry" \
-p "rofi-pass > ")"

rofi_exit=$?

Expand Down Expand Up @@ -354,11 +361,12 @@ mainMenu () {
-kb-custom-16 "${help}"
-kb-custom-17 "${switch}"
-kb-custom-18 "${insert_pass}"
-kb-custom-19 "${qrcode}")
args+=(-kb-mode-previous "" # These keyboard shortcut options are needed, because
-kb-mode-next "" # Shift+<Left|Right> are otherwise taken by rofi.
-select "$entry"
-p "> ")
-kb-custom-19 "${qrcode}"
)
args+=( -kb-mode-previous "" # These keyboard shortcut options are needed, because
-kb-mode-next "" # Shift+<Left|Right> are otherwise taken by rofi.
-select "$entry"
-p "> " )

if [[ ${#roots[@]} -gt "1" || $custom_root == "true" ]]; then
args+=(-mesg "PW Store: ${root}")
Expand Down Expand Up @@ -404,7 +412,7 @@ mainMenu () {
*)
_id="${LINE%%: *}"
_val="${LINE#* }"
;;
;;
esac

if [[ -n "$_id" ]]; then
Expand All @@ -428,10 +436,10 @@ mainMenu () {
if [[ -z "${stuff["${USERNAME_field}"]}" ]]; then
if [[ -n $default_user ]]; then
if [[ "$default_user" == ":filename" ]]; then
stuff["${USERNAME_field}"]="$(basename $selected_password)"
else
stuff["${USERNAME_field}"]="${default_user}"
fi
stuff["${USERNAME_field}"]="$(basename "$selected_password")"
else
stuff["${USERNAME_field}"]="${default_user}"
fi
fi
fi
pass_content="$(for key in "${!stuff[@]}"; do printf '%s\n' "${key}: ${stuff[$key]}"; done)"
Expand Down Expand Up @@ -469,32 +477,33 @@ clearUp () {
}

helpMenu () {
printf '%s' "${autotype}: Autotype
${type_user}: Type Username
${type_pass}: Type Password
${qrcode}: Generate and display qrcode
---
${copy_name}: Copy Username
${copy_pass}: Copy Password
${copy_url}: Copy URL
${open_url}: Open URL
${copy_menu}: Copy Custom Field
---
${action_menu}: Edit, Move, Delete, Re-generate Submenu
${show}: Show Password File
${insert_pass}: Insert new Pass Entry
${switch}: Switch Pass/Bookmark Mode
---
${previous_root}: Switch to previous password store (--root)
${next_root}: Switch to next password store (--root)
" | _rofi -dmenu -mesg "Hint: All hotkeys are configurable in config file" -p "Help > "
help_val=$?

if [[ $help_val -eq 1 ]]; then
exit;
else
unset helptext; mainMenu;
fi
_rofi -dmenu -mesg "Hint: All hotkeys are configurable in config file" -p "Help > " <<- EOM
${autotype}: Autotype
${type_user}: Type Username
${type_pass}: Type Password
${qrcode}: Generate and display qrcode
---
${copy_name}: Copy Username
${copy_pass}: Copy Password
${copy_url}: Copy URL
${open_url}: Open URL
${copy_menu}: Copy Custom Field
---
${action_menu}: Edit, Move, Delete, Re-generate Submenu
${show}: Show Password File
${insert_pass}: Insert new Pass Entry
${switch}: Switch Pass/Bookmark Mode
---
${previous_root}: Switch to previous password store (--root)
${next_root}: Switch to next password store (--root)
EOM
help_val=$?

if [[ $help_val -eq 1 ]]; then
exit;
else
unset helptext; mainMenu;
fi
}


Expand Down Expand Up @@ -547,7 +556,8 @@ actionMenu () {
"2 Copy Password File"
"3 Delete Password File"
"4 Edit Password File"
"5 Generate New Password")
"5 Generate New Password"
)

action=$(printf '%s\n' "${action_content[@]}" | _rofi -dmenu -p "Choose Action > ")
if [[ ${action} == "1 Move Password File" ]]; then
Expand Down Expand Up @@ -592,7 +602,8 @@ showEntry () {
fi

bla_content=("< Return"
"${pass_content}")
"${pass_content}"
)

bla=$(printf '%s\n' "${bla_content[@]}" | _rofi -dmenu -mesg "Enter: Copy entry to clipboard" -p "> ")
rofi_exit=$?
Expand Down Expand Up @@ -657,7 +668,8 @@ manageEntry () {
elif [[ "$1" == "delete" ]]; then
HELP="<span color='$help_color'>Selected entry: ${selected_password}</span>"
ask_content=("Yes"
"No")
"No"
)
ask=$(printf '%s\n' "${ask_content[@]}" | _rofi -mesg "${HELP}" -dmenu -p "Are You Sure? > ")
if [[ "$ask" == "Yes" ]]; then
PASSWORD_STORE_DIR="${root}" pass rm --force "${selected_password}"
Expand All @@ -671,11 +683,6 @@ manageEntry () {
fi
}

listgpg () {
pw_list=(**/*.gpg)
printf '%s\n' "${pw_list[@]}" | sort -n
}

insertPass () {
url=$(xclip --selection clipboard -o)

Expand All @@ -700,7 +707,8 @@ insertPass () {

user_content=("${default_user2}"
"${USER}"
"${default_user}")
"${default_user}"
)

user=$(printf '%s\n' "${user_content[@]}" | _rofi -dmenu -mesg "Chose Username or type" -p "> ")
val=$?
Expand All @@ -711,7 +719,8 @@ insertPass () {

group_content=("No Group"
"---"
"${grouplist}")
"${grouplist}"
)

group=$(printf '%s\n' "${group_content[@]}" | _rofi -dmenu -p "Choose Group > ")
val=$?
Expand All @@ -733,25 +742,29 @@ insertPass () {
pass_content=("${pw}"
"---"
"${USERNAME_field}: ${user}"
"${URL_field}: ${url}")
"${URL_field}: ${url}"
)
printf '%s\n' "${pass_content[@]}" | PASSWORD_STORE_DIR="${root}" pass insert -m "${name}" > /dev/null && PASSWORD_STORE_DIR="${root}" pass edit "${name}"
else
pass_content=("${pw}"
"---"
"${USERNAME_field}: ${user}")
"${USERNAME_field}: ${user}"
)
printf '%s\n' "${pass_content[@]}" | PASSWORD_STORE_DIR="${root}" pass insert -m "${name}" > /dev/null && PASSWORD_STORE_DIR="${root}" pass edit "${name}"
fi
else
if [[ $url == http* ]]; then
pass_content=("${pw}"
"---"
"${USERNAME_field}: ${user}"
"${URL_field}: ${url}")
"${URL_field}: ${url}"
)
printf '%s\n' "${pass_content[@]}" | PASSWORD_STORE_DIR="${root}" pass insert -m "${group}/${name}" > /dev/null && PASSWORD_STORE_DIR="${root}" pass edit "${group}/${name}"
else
pass_content=("${pw}"
"---"
"${USERNAME_field}: ${user}")
"${USERNAME_field}: ${user}"
)
printf '%s\n' "${pass_content[@]}" | PASSWORD_STORE_DIR="${root}" pass insert -m "${group}/${name}" > /dev/null
if [[ $edit_new_pass == "true" ]]; then
PASSWORD_STORE_DIR="${root}" pass edit "${group}/${name}"
Expand Down Expand Up @@ -783,21 +796,18 @@ get_config_file () {

# return the first config file with a valid path
for config in "${configs[@]}"; do
# '! -z' is needed in case ROFI_PASS_CONFIG is not set
if [[ ! -z "${config}" && -f "${config}" ]]; then
# '-n' is needed in case ROFI_PASS_CONFIG is not set
if [[ -n "${config}" && -f "${config}" ]]; then
printf "%s" "$config"
return
fi
done
}

main () {
# enable extended globbing
shopt -s nullglob globstar

# load config file
config_file="$(get_config_file)"
[[ ! -z "$config_file" ]] && source "$config_file"
[[ -n "$config_file" ]] && source "$config_file"

# create tmp dir
if [[ ! -d "$cache_dir/rofi-pass" ]]; then
Expand Down Expand Up @@ -868,4 +878,3 @@ main () {
}

main "$@"

0 comments on commit 6e7fd8e

Please sign in to comment.