192 lines
6.7 KiB
Bash
192 lines
6.7 KiB
Bash
# Simple Note System
|
|
# Copyright (C) 2016, Jon Lewis
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License along
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
#==============================================================================
|
|
# Stage 1: Read Configuration / Verify Integrity
|
|
#==============================================================================
|
|
|
|
if [ -r "$CONFIG_FILE" ]; then
|
|
source "$CONFIG_FILE"
|
|
verify_store
|
|
elif [ "$1" != "-i" ]; then
|
|
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
|
|
"Configuration not found. Please run sns -i."
|
|
exit 5 #ERR_NO_STORE
|
|
fi
|
|
|
|
if [ -d "$NOTES_DIR" ]; then cd "$NOTES_DIR"; fi
|
|
if [ "$ENCRYPTION" == "TRUE" ]; then
|
|
# If the user chose not to decrypt notes before, clear that preference.
|
|
if [ -r "$NOTES_DIR"/.do_not_decrypt ]; then
|
|
rm "$NOTES_DIR"/.do_not_decrypt;
|
|
fi
|
|
# Check if GPG is installed.
|
|
if [ ! -r "$(which gpg)" ]; then
|
|
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
|
|
"Encryption was specified, but GPG is not installed."
|
|
exit 100
|
|
# Check if we have a GPG recipient
|
|
elif [ -z "$PUBKEY" ]; then
|
|
>&2 printf "$RED_COLOR!$RESET_COLOR %s\n\t%s\n"\
|
|
"No GPG recipient was provided in $CONFIG_FILE. "
|
|
exit 110
|
|
# All is good. If any previously unencrypted notes exist, encrypt them.
|
|
# No harm in extra security.
|
|
else
|
|
find . -type f -name "*.$EXT" | grep -v "gpg" | while read -r TMP_NOTE; do
|
|
NOTE="${TMP_NOTE%.$EXT}.gpg.$EXT"
|
|
encrypt
|
|
if [ -r "$NOTE" ]; then
|
|
printf "$YELLOW_COLOR!$RESET_COLOR %s\n" "Encrypted ${NOTE%.$EXT}"
|
|
rm "$TMP_NOTE"
|
|
fi
|
|
done
|
|
fi
|
|
# Set the extension to denote encryption.
|
|
declare -r EXT=gpg.note
|
|
SESSION_ID="$RANDOM" #SESSION_ID later becomes the temporary filename
|
|
|
|
# If encryption isn't enabled, make sure either all notes are decrypted or the user
|
|
# does not wish to decrypt all notes.
|
|
else
|
|
if [ ! -r "$NOTES_DIR"/.do_not_decrypt ]; then
|
|
if [ -n "$(find "$NOTES_DIR" -type f -name "*.gpg.$EXT" > /dev/null)" ]; then
|
|
while true; do
|
|
read -rp "Would you like to de-encrypt previously encrypted notes? " YN
|
|
case $YN in
|
|
[Yy]* )
|
|
read -s -rp "Please enter your passphrase: " PASSPHRASE
|
|
cd "$NOTES_DIR"
|
|
find . -type f -name "*.gpg.$EXT" | while read -r NOTE; do
|
|
gpg\
|
|
--passphrase "$PASSPHRASE"\
|
|
-o "${NOTE%.gpg.note}.note"\
|
|
--decrypt "$NOTE"
|
|
|
|
if [ -r "${NOTE%.gpg.note}.note" ]; then
|
|
printf "$YELLOW_COLOR!$RESET_COLOR %s\n"\
|
|
"De-encrypted ${NOTE%.gpg.$EXT}"
|
|
rm "$NOTE";
|
|
fi
|
|
done
|
|
break;;
|
|
[Nn]* )
|
|
# Remember the user's preference.
|
|
touch "$NOTES_DIR/.do_not_decrypt"
|
|
break;;
|
|
*)
|
|
printf " $RED_COLOR!$RESET_COLOR %s\n" "Please enter Y or N"
|
|
;;
|
|
esac
|
|
done
|
|
fi
|
|
fi
|
|
# Set the extension
|
|
declare -r EXT=note
|
|
fi
|
|
|
|
#==============================================================================
|
|
# Stage 2: Argument Parsing
|
|
#==============================================================================
|
|
if [ ! "$@" ]; then help; exit 20
|
|
else
|
|
ARGS=("$@")
|
|
declare -i INDEX
|
|
INDEX=0
|
|
for ARG in "$@"; do
|
|
let INDEX++
|
|
case "$ARG" in
|
|
-c|--create)
|
|
NOTE="${ARGS[$INDEX]}" create
|
|
exit 0
|
|
;;
|
|
-C|--config)
|
|
if [ -z "$EDITOR" ]; then
|
|
printf "$YELLOW_COLOR!$RESET_COLOR - %s\n"\
|
|
"No editor defined. Defaulting to vi."
|
|
EDITOR=vi
|
|
fi
|
|
"$EDITOR" "$CONFIG_FILE"
|
|
exit 0;
|
|
;;
|
|
-ce)
|
|
NOTE="${ARGS[$INDEX]}"
|
|
create
|
|
NO_HEADER="TRUE" edit
|
|
exit 0
|
|
;;
|
|
-d|--delete)
|
|
NOTE="${ARGS[$INDEX]}" delete
|
|
exit 0
|
|
;;
|
|
-e|--edit)
|
|
NOTE="${ARGS[$INDEX]}" edit
|
|
exit 0
|
|
;;
|
|
-l|--list)
|
|
NOTEBOOK="${ARGS[$INDEX]}" list
|
|
exit 0
|
|
;;
|
|
-p|--print)
|
|
NOTE="${ARGS[$INDEX]}" print
|
|
exit 0
|
|
;;
|
|
-h|--help)
|
|
help
|
|
exit 0
|
|
;;
|
|
-i|--init-store)
|
|
init_store
|
|
exit 0
|
|
;;
|
|
"$VCTL")
|
|
cd "$ROOT_DIR"
|
|
verctl "${@:$INDEX}"
|
|
exit 0
|
|
;;
|
|
*)
|
|
NOTE="$ARG"
|
|
break;
|
|
;;
|
|
esac
|
|
done
|
|
fi
|
|
#==============================================================================
|
|
# Section: Actions / Stage 3
|
|
#==============================================================================
|
|
# Default behavior
|
|
# If no operation was specified, print help and exit on ERR_NO_OP
|
|
if [ "$OP" != "TRUE" ]; then
|
|
help; exit 20
|
|
fi
|
|
# All options not requiring a note to be specified have been dealt
|
|
# with; if one isn't specified, exit on ERR_NO_NOTE.
|
|
#if [ -z "$NOTE" ]; then
|
|
# printf "$RED_COLOR!$RESET_COLOR %s\n" "No note specified."
|
|
# exit 30
|
|
#fi
|
|
|
|
|
|
|
|
if [ "$LIST" == "TRUE" ]; then list ; exit 0; fi
|
|
if [ "$PRINT" == "TRUE" ]; then print ; exit 0; fi
|
|
if [ "$DELETE" == "TRUE" ]; then delete; exit 0; fi
|
|
if [ "$CREATE" == "TRUE" ]; then create; fi
|
|
if [ "$EDIT" == "TRUE" ]; then edit ; fi
|
|
#==============================================================================
|
|
# End Section: Actions / Stage 3
|
|
#==============================================================================
|