Support for encrypted notes added.

This commit is contained in:
Storm Dragon
2019-07-30 11:39:26 -04:00
parent be2663c88d
commit 6c2cfbeaf9

View File

@@ -50,6 +50,14 @@ infobox() {
read -n1 -t $messageTimeout continue read -n1 -t $messageTimeout continue
} }
inputbox() {
# Returns: text entered by the user
# Args 1, Instructions for box.
# args: 2 initial text (optional)
dialog --backtitle "$(gettext "Enter text and press enter.")" \
--inputbox "$1" 0 0 "$2" --stdout
}
yesno() { yesno() {
# Returns: Yes or No # Returns: Yes or No
# Args: Question to user. # Args: Question to user.
@@ -80,6 +88,7 @@ menulist() {
more_menu() { more_menu() {
# Options for the submenu go in the options array. # Options for the submenu go in the options array.
declare -a options=( declare -a options=(
"encrypt" "$(gettext "Encrypt")"
"delete" "$(gettext "Delete")") "delete" "$(gettext "Delete")")
local action="$(dialog --backtitle "$(gettext "Use the up and down arrow keys to find the option you want, then press enter to select it.")" \ local action="$(dialog --backtitle "$(gettext "Use the up and down arrow keys to find the option you want, then press enter to select it.")" \
--no-tags \ --no-tags \
@@ -87,6 +96,7 @@ more_menu() {
0 0 0 ${options[@]} --stdout)" 0 0 0 ${options[@]} --stdout)"
case "$action" in case "$action" in
"delete") delete_note "$1";; "delete") delete_note "$1";;
"encrypt") encrypt_note "$1";;
"") return;; "") return;;
esac esac
} }
@@ -118,7 +128,11 @@ delete_note() {
} }
display_note() { display_note() {
markdown "$1" | eval "$pager" if [[ "${1##*.}" == "gpg" ]]; then
gpg -d "$1" | markdown | eval "$pager"
else
markdown "$1" | eval "$pager"
fi
} }
edit_note() { edit_note() {
@@ -132,6 +146,26 @@ edit_note() {
fi fi
} }
encrypt_note() {
# function requires exactly 1 argument.
if [[ $# -ne 1 ]]; then
return
fi
# Get a human readable name for the new note.
local noteName="$(inputbox "$(gettext "Please enter a descriptive name for the encrypted note:")")"
if [[ -z "$noteName" ]]; then
# No name supplied, so return
return
fi
# Encrypt the given file and if successful, remove the original.
if gpg -o "${1%/*}/${noteName%.md*}.md.gpg" -c "$1" ; then
rm -f "$1"
infobox "$(gettext "Note encrypted.")"
return
fi
infobox "$(gettext "Encryption failed.")"
}
# Configuration section # Configuration section
# Available arguments in both long and short versions stored in associative array. # Available arguments in both long and short versions stored in associative array.
declare -A argList=( declare -A argList=(
@@ -208,7 +242,7 @@ while [ "$action" != "exit" ]; do
for i in "${notes[@]}" ; do for i in "${notes[@]}" ; do
noteMenu+=("$i") noteMenu+=("$i")
if [[ "${i##*.}" == "gpg" ]]; then if [[ "${i##*.}" == "gpg" ]]; then
noteMenu+=("$(basename "${i}" .gpg)") noteMenu+=("$(gettext "[enc]") $(basename "${i%.md.gpg}")")
else else
noteMenu+=("$(head -1 "${i}")") noteMenu+=("$(head -1 "${i}")")
fi fi