From 6c2cfbeaf9d08745e9dfb6331fa0be1e9e9e972f Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Tue, 30 Jul 2019 11:39:26 -0400 Subject: [PATCH] Support for encrypted notes added. --- notestorm | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/notestorm b/notestorm index 76cd149..4e70f42 100755 --- a/notestorm +++ b/notestorm @@ -50,6 +50,14 @@ infobox() { 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() { # Returns: Yes or No # Args: Question to user. @@ -80,6 +88,7 @@ menulist() { more_menu() { # Options for the submenu go in the options array. declare -a options=( + "encrypt" "$(gettext "Encrypt")" "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.")" \ --no-tags \ @@ -87,6 +96,7 @@ more_menu() { 0 0 0 ${options[@]} --stdout)" case "$action" in "delete") delete_note "$1";; + "encrypt") encrypt_note "$1";; "") return;; esac } @@ -118,7 +128,11 @@ delete_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() { @@ -132,6 +146,26 @@ edit_note() { 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 # Available arguments in both long and short versions stored in associative array. declare -A argList=( @@ -208,7 +242,7 @@ while [ "$action" != "exit" ]; do for i in "${notes[@]}" ; do noteMenu+=("$i") if [[ "${i##*.}" == "gpg" ]]; then - noteMenu+=("$(basename "${i}" .gpg)") + noteMenu+=("$(gettext "[enc]") $(basename "${i%.md.gpg}")") else noteMenu+=("$(head -1 "${i}")") fi