2.0a4 -> 2.0a5

Changed some filenames
Rewrote edit function to be more sane
Separated encryption and decryption functions from edit into libencryption.sh
Fixed a bug where an edit tag would be added regardless of create status
This commit is contained in:
Jon-William Lewis
2015-05-07 22:11:13 -05:00
parent 64e504ebcb
commit 3cb375f64b
22 changed files with 540 additions and 208 deletions

119
sns.sh
View File

@@ -1,11 +1,11 @@
#!/bin/bash
#==========================================================
# Simple Note System, v2.0a3
# Simple Note System, v2.0a4
# Copyright 2014, Xenese Labs/Sicron-Perion XNF
#==========================================================
PROD_STR="Simple Note System"
VER_STR="v2.0a3"
VER_STR="v2.0a4"
# Section: Functions
function init_default_config() {
@@ -42,7 +42,9 @@ BASEDIR=$ROOTDIR/notes
EXT=note
#Preferred Editor
EDITOR=vim
if [ -z "$EDITOR" ]; then
EDITOR=vim
fi
#Encryption
#WARNING: ANY PREVIOUSLY UNENCRYPTED NOTES WILL BE LOST
@@ -77,6 +79,14 @@ function help {
function p_header(){
printf "TITLE: $NAME\nDATE: $(date)\n"
}
function encrypt(){
openssl enc -aes-256-cbc -salt -in "$TARGET" -out "$NOTE" -pass pass:"$ENC_KEY"
}
function decrypt(){
TARGET="$ROOTDIR"/tmp/"$RANDOM"
openssl enc -d -aes-256-cbc -in "$NOTE" -pass pass:"$ENC_KEY" > "$TARGET"
}
#function create(){
#
# #Check if note exists
@@ -107,15 +117,17 @@ function create(){
if [ -e "$NOTE" -o -e ${NOTE%.*} ]; then
printf "\nERROR: Note already exists\nHint: use -e to edit the note.\n"
exit
elif [ -z "$ENCRYPTION" ]; then
else
mkdir -p "$NOTEDIR"
fi
if [ -z "$ENCRYPTION" ]; then
echo "TITLE: $NAME" > $NOTE
echo "DATE: $(date)" >> $NOTE
elif [ "$ENCRYPTION" == "TRUE" ]; then
mkdir -p "$NOTEDIR"
touch "$NOTE"
echo "$(p_header)" | openssl enc -aes-256-cbc -salt -out "$NOTE"\
-pass pass:$ENC_KEY
echo "$(p_header)" | openssl enc -aes-256-cbc -salt -out "$NOTE"\
-pass pass:$ENC_KEY
fi
echo "$NOTE"
}
@@ -137,31 +149,26 @@ function delete(){
fi
fi
}
function edit (){
if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then
#Pre-Processing (Encryption)
if [ -z "$CREATE" ]; then
if [ "$ENCRYPTION" == "TRUE" ]; then
TMP_NAME="$ROOTDIR"/tmp/"$RANDOM"
openssl enc -d -aes-256-cbc -in $NOTE -pass pass:$ENC_KEY > $TMP_NAME
echo "" >> $TMP_NAME
echo "EDIT $(date)" >> $TMP_NAME
$EDITOR $TMP_NAME
else
echo "" >> $NOTE
echo "EDIT $(date)" >> $NOTE
$EDITOR $NOTE
fi
fi
#Post-Processing (Encryption)
if [ "$ENCRYPTION" == "TRUE" ]; then
openssl enc -aes-256-cbc -salt -in $TMP_NAME -out $NOTE -pass pass:$ENC_KEY
rm "$TMP_NAME"
fi
function edit(){
if [ ! -r "$NOTE" ]; then
echo "ERROR: Note cannot be opened for editing."
exit 40;
fi
if [ "$ENCRYPTION" == "TRUE" ]; then decrypt
else TARGET="$NOTE"; fi
if [ -z "$CREATE" ]; then printf "\nEDIT $(date)" >> "$TARGET"; fi
"$EDITOR" "$TARGET"
if [ "$ENCRYPTION" == "TRUE" ]; then encrypt; fi
else
printf "\nERROR: Note cannot be opened for editting.\n"
fi
}
function print(){
if [ "$PRINT" == "TRUE" ]; then
@@ -259,39 +266,36 @@ fi
NAME=""
NOTEBOOK=""
SECTION=""
if [ -z "$1" ]; then #If no input was given, print help
help
exit 20
else #Assume the user wants to do something.
if [ -z "$1" ]; then help; exit 20
else
ARGS=( "$@" )
for ARG in ${ARGS[@]};do
if [ "$ARG" = "-c" -o "$ARG" = "--create" ]; then CREATE="TRUE"
elif [ "$ARG" = "-d" -o "$ARG" = "--delete" ]; then DELETE="TRUE"
elif [ "$ARG" = "-e" -o "$ARG" = "--edit" ]; then EDIT="TRUE"
elif [ "$ARG" = "-ce" -o "$ARG" = "-ec" ]; then EDIT="TRUE";CREATE="TRUE"
elif [ "$ARG" = "-p" -o "$ARG" = "--print" ]; then PRINT="TRUE"
elif [ "$ARG" = "-l" -o "$ARG" = "--list" ]; then LIST="TRUE"
elif [ "$ARG" = "-h" -o "$ARG" == "--help" ]; then help; exit 0
elif [ "$ARG" = "-w" -o "$ARG" == "--wconf" ]; then w_conf; exit
for ARG in ${ARGS[@]}; do
if [ "$ARG" = "-c" -o "$ARG" = "--create" ]; then CREATE="TRUE"
elif [ "$ARG" = "-d" -o "$ARG" = "--delete" ]; then DELETE="TRUE"
elif [ "$ARG" = "-e" -o "$ARG" = "--edit" ]; then EDIT="TRUE"
elif [ "$ARG" = "-ce" -o "$ARG" = "-ec" ]; then EDIT="TRUE"; CREATE="TRUE"
elif [ "$ARG" = "-p" -o "$ARG" = "--print" ]; then PRINT="TRUE"
elif [ "$ARG" = "-l" -o "$ARG" = "--list" ]; then LIST="TRUE"
elif [ "$ARG" = "-h" -o "$ARG" == "--help" ]; then help; exit 0
elif [ "$ARG" = "-w" -o "$ARG" == "--wconf" ]; then w_conf; exit 0
else
if [ -z "$NAME" -a -n $ARG ]; then
NAME=$ARG
elif [ -z "$NOTEBOOK" -a -n $ARG ]; then
NOTEBOOK=$ARG
elif [ -z "$SECTION" -a -n $ARG ]; then
SECTION=$ARG
if [ -z "$NAME" -a -n $ARG ]; then NAME="$ARG"
elif [ -z "$NOTEBOOK" -a -n $ARG ]; then NOTEBOOK="$ARG"
elif [ -z "$SECTION" -a -n $ARG ]; then SECTION="$ARG"
fi
fi
done
if [ -n "$NAME" -a -z "$NOTEBOOK" ]; then
if [ -n "$NAME" -a -z "$NOTEBOOK" -a -n "$LIST" ]; then
# If we got a note title above, but no notebook, and the list option
# was specified, we assume that the detected note title is actually a
# notebook name.
NOTEBOOK="$NAME"
NAME=""
fi
fi
# Note: w_conf has highest priority, and it is the only function that can
# work without any parameters.
# Note: w_conf and help have highest priority, as they are the only functions
# that can work without any parameters.
#==============================================================================
# End Section: Argument Parsing
@@ -307,7 +311,6 @@ if [ -z "$NOTEBOOK" ]; then
fi
if [ -n "$LIST" ]; then
list
exit 0
fi
@@ -320,8 +323,8 @@ if [ -z "$NAME" ]; then
exit 30
fi
NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION/
NOTE=$NOTEDIR$NAME.$EXT
NOTEDIR="$BASEDIR"/"$NOTEBOOK"/"$SECTION"/
NOTE="$NOTEDIR""$NAME"."$EXT"
if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.enc; fi
if [ "$PRINT" == "TRUE" ]; then print; exit 0; fi