diff --git a/README.md b/README.md index 690076d..e8ff71d 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,13 @@ root, and is deleted after editing, but can be recovered by forensic utilities. SNS's encryption is mainly useful when the store is being _transferred over a network_. +## Vim Encryption +As of SNS v2b, SNS will look for a file named .vim in its store. If found, SNS will start in Vim mode, calling Vim directly instead of the environment's $EDITOR, and using Vim's native functionality for in-place encryption. + +At this time, SNS does not provide a way to convert between store formats. Additionally, SNS v2b does not support printing Vim-encrypted notes to stdout; you will need to open the note for editing and copy/paste out of Vim for the time being. + +Finally, when using Vim mode, treat -c and -e separately. + ## Tips and Tricks * To list all notes in all notebooks, use `sns -l .` diff --git a/sns.sh b/sns.sh index d98aace..f4b7731 100755 --- a/sns.sh +++ b/sns.sh @@ -1,11 +1,10 @@ #!/bin/bash XL_PRODUCT="Simple Note System" -XL_VER="v2a" +XL_VER="v2b" # Environment Constants readonly SNS_STORE="$HOME/.local/sns" readonly SNS_KEYFILE="$SNS_STORE/.pubkey" -readonly SNS_DEPS=("gpg2" "tree" "git") readonly IFS=$'\n\t' set -euo pipefail @@ -114,44 +113,7 @@ function sns_list(){ # Print the tree tree -C --noreport --prune "$(echo "$@" | awk '{print $1}')" } -function sns_create(){ - # Make sure the note doesn't already exist - if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then - sns_printError "Note already exists." - return - fi - - # Print the standard header to a temporary note - sns_NoteHeader > /tmp/"$SNS_SID" - - # Edit the new note - sns_edit "$(echo "$@" | awk '{print $1}')" -} -function sns_edit(){ - # Make the function more readable - local readonly SNS_NOTE="$SNS_STORE/$(echo "$@" | awk '{print $1}')" - # Test if edit was called from create - if [ ! -r /tmp/"$SNS_SID" ]; then gpg2 -d -o /tmp/"$SNS_SID" "$SNS_NOTE"; fi - - # Edit the note - vim /tmp/"$SNS_SID" - - # Make sure the notebook/section exists - if [ ! -d "$(dirname "$SNS_NOTE")" ]; then mkdir -p "$(dirname "$SNS_NOTE")"; fi - - # If the note previously existed, make a backup. - if [ -r "$SNS_NOTE" ]; then mv "$SNS_NOTE" "$SNS_NOTE.bk"; fi - - # Re-encrypt it to the store - gpg2 -r "$SNS_PUBKEY" -o "$SNS_NOTE" -e /tmp/"$SNS_SID" - - # If all went well, remove the backup - if [ -r "$SNS_NOTE" ]; then rm "$SNS_NOTE.bk"; fi -} -function sns_print(){ - gpg2 -d "$SNS_STORE/$(echo "$@" | awk '{print $1}')" -} function sns_rm(){ rm -f "$SNS_STORE/$(echo "$@" | awk '{print $1}')" } @@ -209,6 +171,71 @@ function sns_argParse(){ printf "%s\n%s\n" "$XL_PRODUCT" "$XL_VER" printf "\n" +#Determine Run Mode +if [ -r "$SNS_STORE/.vim" ]; then + readonly SNS_DEPS=("vim" "tree" "git") + + function sns_create(){ + if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then + sns_printError "Note already exists." + return + fi + vim -x "$SNS_STORE/$(echo "$@" | awk '{print $1}')" + } + + function sns_edit(){ + vim "$SNS_STORE/$(echo "$@" | awk '{print $1}')" + } + + function sns_print(){ + sns_printError "Printing of Vim-encrypted notes is not supported at this time." + } +else + readonly SNS_DEPS=("gpg2" "tree" "git") + + function sns_create(){ + # Make sure the note doesn't already exist + if [ -r "$SNS_STORE/$(echo "$@" | awk '{print $1}')" ]; then + sns_printError "Note already exists." + return + fi + + # Print the standard header to a temporary note + sns_NoteHeader > /tmp/"$SNS_SID" + + # Edit the new note + sns_edit "$(echo "$@" | awk '{print $1}')" + } + + function sns_edit(){ + # Make the function more readable + local readonly SNS_NOTE="$SNS_STORE/$(echo "$@" | awk '{print $1}')" + + # Test if edit was called from create + if [ ! -r /tmp/"$SNS_SID" ]; then gpg2 -d -o /tmp/"$SNS_SID" "$SNS_NOTE"; fi + + # Edit the note + vim /tmp/"$SNS_SID" + + # Make sure the notebook/section exists + if [ ! -d "$(dirname "$SNS_NOTE")" ]; then mkdir -p "$(dirname "$SNS_NOTE")"; fi + + # If the note previously existed, make a backup. + if [ -r "$SNS_NOTE" ]; then mv "$SNS_NOTE" "$SNS_NOTE.bk"; fi + + # Re-encrypt it to the store + gpg2 -r "$SNS_PUBKEY" -o "$SNS_NOTE" -e /tmp/"$SNS_SID" + + # If all went well, remove the backup + if [ -r "$SNS_NOTE" ]; then rm "$SNS_NOTE.bk"; fi + } + + function sns_print(){ + gpg2 -d "$SNS_STORE/$(echo "$@" | awk '{print $1}')" + } +fi + + sns_argParse "$@" sns_sanityCheck if [ "$SNS_EXIT" -eq 0 ]; then