diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index 397e97b..8b308d4 --- a/build.sh +++ b/build.sh @@ -17,9 +17,10 @@ EOF echo "# Section: Functions" >> $S cat ./functions/init_default_config.sns.sh >> $S -cat ./functions/wconf.sns.sh >> $S +cat ./functions/w_conf.sns.sh >> $S cat ./functions/pause.sns.sh >> $S cat ./functions/help.sns.sh >> $S +cat ./functions/p_header.sh >> $S cat ./functions/create.sns.sh >> $S cat ./functions/delete.sns.sh >> $S cat ./functions/edit.sns.sh >> $S diff --git a/functions/create.sns.sh b/functions/create.sns.sh index 80447ab..cb6519e 100644 --- a/functions/create.sns.sh +++ b/functions/create.sns.sh @@ -1,28 +1,42 @@ +#function create(){ +# +# #Check if note exists +# if [ -e "$NOTE" -o -e ${NOTE%.*} ]; then +# echo "" +# echo "ERROR: Note already exists" +# echo "Hint: use -e to edit the note." +# echo "" +# exit +# else +# #Create any necessary folders +# mkdir -p $NOTEDIR +# +# #Fill in title +# echo "TITLE: $NAME" > $NOTE +# #Fill the second line with the date +# echo "DATE: $(date)" >> $NOTE +# +# if [ "$ENCRYPTION" == "TRUE" ]; then +# if [ "$EDIT" == "FALSE" ]; then +# echo " openssl enc -aes-256-cbc -salt -in $NOTE -out $NOTE.enc -pass pass:$ENC_KEY" +# fi +# fi +# fi +#} + function create(){ - if [ -z "$CREATE" -a -z "$EDIT" -a -z "$PRINT" ]; then #If no action specified, print help and exit - help - exit - elif [ "$CREATE" == "TRUE" ]; then - if [ -e $NOTE -o -e ${NOTE%.*} ]; then - echo "" - echo "ERROR: Note already exists" - echo "Hint: use -e to edit the note." - echo "" - exit - else - #Create any necessary folders - mkdir -p $NOTEDIR + 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 + 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 - #Fill in title - echo "TITLE: $NAME" > $NOTE - #Fill the second line with the date - echo "DATE: $(date)" >> $NOTE - - if [ "$ENCRYPTION" == "TRUE" ]; then - if [ $EDIT == "FALSE" ]; then - openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY - fi - fi - fi fi + echo "$NOTE" } diff --git a/functions/edit.sns.sh b/functions/edit.sns.sh index 140a117..1fb0181 100644 --- a/functions/edit.sns.sh +++ b/functions/edit.sns.sh @@ -1,27 +1,26 @@ function edit (){ - if [ "$EDIT" == "TRUE" ]; then - if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then - 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 - else - echo "" >> $NOTE - echo "EDIT $(date)" >> $NOTE - fi - fi - $EDITOR $NOTE + if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then + #Pre-Processing (Encryption) + if [ -z "$CREATE" ]; then if [ "$ENCRYPTION" == "TRUE" ]; then - openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY - rm $NOTE + 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 - - else - echo "" - echo "ERROR: Note cannot be opened for editting." - echo "" 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 + + else + printf "\nERROR: Note cannot be opened for editting.\n" fi } diff --git a/functions/init_default_config.sns.sh b/functions/init_default_config.sns.sh index 5bdd1dd..9ed8c69 100644 --- a/functions/init_default_config.sns.sh +++ b/functions/init_default_config.sns.sh @@ -1,7 +1,19 @@ function init_default_config() { +if [ -z "$ROOTDIR" ]; then ROOTDIR=$HOME/.sns +fi +if [ -z "$BASEDIR" ]; then BASEDIR=$ROOTDIR/notes +fi +if [ -z "$EXT" ]; then EXT=note +fi +if [ -z "$EDITOR" ]; then EDITOR=vim +fi +if [ -z "$ENC_KEY" ]; then ENCRYPTION="FALSE" +else + ENCRYPTION="TRUE" +fi } diff --git a/functions/p_header.sh b/functions/p_header.sh new file mode 100644 index 0000000..537085a --- /dev/null +++ b/functions/p_header.sh @@ -0,0 +1,3 @@ +function p_header(){ + printf "TITLE: $NAME\nDATE: $(date)\n" +} diff --git a/functions/wconf.sns.sh b/functions/w_conf.sns.sh similarity index 96% rename from functions/wconf.sns.sh rename to functions/w_conf.sns.sh index b6ea422..4ac6034 100644 --- a/functions/wconf.sns.sh +++ b/functions/w_conf.sns.sh @@ -1,4 +1,4 @@ -function writeconf { +function w_conf { cat > $HOME/.sns/sns.conf << EOF #========================================================== # Simple Note System Config, v2.0a1 diff --git a/init/stage1.sns.sh b/init/stage1.sns.sh index 89ab8d2..5647731 100644 --- a/init/stage1.sns.sh +++ b/init/stage1.sns.sh @@ -17,7 +17,7 @@ fi if [ "$ENCRYPTION" == "TRUE" ]; then PROD_STR="Simple Note System (Encryption Enabled)" -EXT="$EXT.enc" +EXT="$EXT" if [ ! -d ~/.sns/tmp ]; then mkdir -p ~/.sns/tmp fi @@ -36,5 +36,5 @@ pause fi #============================================================================== -# End Section: Read Configuration +# End Section: Configuration #============================================================================== diff --git a/init/stage2.sns.sh b/init/stage2.sns.sh index 03ece6c..fd8bf45 100644 --- a/init/stage2.sns.sh +++ b/init/stage2.sns.sh @@ -19,7 +19,7 @@ else #Assume the user wants to do something. 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 writeconf; exit + elif [ "$ARG" = "-w" -o "$ARG" == "--wconf" ]; then w_conf; exit else if [ -z "$NAME" -a -n $ARG ]; then NAME=$ARG @@ -36,7 +36,7 @@ else #Assume the user wants to do something. fi fi -# Note: writeconf has highest priority, and it is the only function that can +# Note: w_conf has highest priority, and it is the only function that can # work without any parameters. #============================================================================== diff --git a/init/stage3.sns.sh b/init/stage3.sns.sh index c9dd616..73b9276 100644 --- a/init/stage3.sns.sh +++ b/init/stage3.sns.sh @@ -25,6 +25,7 @@ fi NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION/ NOTE=$NOTEDIR$NAME.$EXT -if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.tmp fi - - +if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.enc; fi +if [ "$PRINT" == "TRUE" ]; then print; exit 0; fi +if [ "$CREATE" == "TRUE" ]; then create; fi +if [ "$EDIT" == "TRUE" ]; then edit; fi \ No newline at end of file diff --git a/sns.sh b/sns.sh old mode 100644 new mode 100755 index 75b48fd..cede94d --- a/sns.sh +++ b/sns.sh @@ -9,13 +9,25 @@ VER_STR="v2.0a3" # Section: Functions function init_default_config() { +if [ -z "$ROOTDIR" ]; then ROOTDIR=$HOME/.sns +fi +if [ -z "$BASEDIR" ]; then BASEDIR=$ROOTDIR/notes +fi +if [ -z "$EXT" ]; then EXT=note +fi +if [ -z "$EDITOR" ]; then EDITOR=vim +fi +if [ -z "$ENC_KEY" ]; then ENCRYPTION="FALSE" +else + ENCRYPTION="TRUE" +fi } -function writeconf { +function w_conf { cat > $HOME/.sns/sns.conf << EOF #========================================================== # Simple Note System Config, v2.0a1 @@ -62,33 +74,50 @@ function help { echo " -w | --wconf : Write default configuration to ~/.sns (useful for Encryption)" echo "" } +function p_header(){ + printf "TITLE: $NAME\nDATE: $(date)\n" +} +#function create(){ +# +# #Check if note exists +# if [ -e "$NOTE" -o -e ${NOTE%.*} ]; then +# echo "" +# echo "ERROR: Note already exists" +# echo "Hint: use -e to edit the note." +# echo "" +# exit +# else +# #Create any necessary folders +# mkdir -p $NOTEDIR +# +# #Fill in title +# echo "TITLE: $NAME" > $NOTE +# #Fill the second line with the date +# echo "DATE: $(date)" >> $NOTE +# +# if [ "$ENCRYPTION" == "TRUE" ]; then +# if [ "$EDIT" == "FALSE" ]; then +# echo " openssl enc -aes-256-cbc -salt -in $NOTE -out $NOTE.enc -pass pass:$ENC_KEY" +# fi +# fi +# fi +#} + function create(){ - if [ -z "$CREATE" -a -z "$EDIT" -a -z "$PRINT" ]; then #If no action specified, print help and exit - help - exit - elif [ "$CREATE" == "TRUE" ]; then - if [ -e $NOTE -o -e ${NOTE%.*} ]; then - echo "" - echo "ERROR: Note already exists" - echo "Hint: use -e to edit the note." - echo "" - exit - else - #Create any necessary folders - mkdir -p $NOTEDIR + 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 + 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 - #Fill in title - echo "TITLE: $NAME" > $NOTE - #Fill the second line with the date - echo "DATE: $(date)" >> $NOTE - - if [ "$ENCRYPTION" == "TRUE" ]; then - if [ $EDIT == "FALSE" ]; then - openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY - fi - fi - fi fi + echo "$NOTE" } function delete(){ if [ "$DELETE" == "TRUE" ]; then @@ -109,30 +138,29 @@ function delete(){ fi } function edit (){ - if [ "$EDIT" == "TRUE" ]; then - if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then - 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 - else - echo "" >> $NOTE - echo "EDIT $(date)" >> $NOTE - fi - fi - $EDITOR $NOTE + if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then + #Pre-Processing (Encryption) + if [ -z "$CREATE" ]; then if [ "$ENCRYPTION" == "TRUE" ]; then - openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY - rm $NOTE + 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 - - else - echo "" - echo "ERROR: Note cannot be opened for editting." - echo "" 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 + + else + printf "\nERROR: Note cannot be opened for editting.\n" fi } function print(){ @@ -203,7 +231,7 @@ fi if [ "$ENCRYPTION" == "TRUE" ]; then PROD_STR="Simple Note System (Encryption Enabled)" -EXT="$EXT.enc" +EXT="$EXT" if [ ! -d ~/.sns/tmp ]; then mkdir -p ~/.sns/tmp fi @@ -222,7 +250,7 @@ pause fi #============================================================================== -# End Section: Read Configuration +# End Section: Configuration #============================================================================== #============================================================================== # Section: Argument Parsing @@ -245,7 +273,7 @@ else #Assume the user wants to do something. 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 writeconf; exit + elif [ "$ARG" = "-w" -o "$ARG" == "--wconf" ]; then w_conf; exit else if [ -z "$NAME" -a -n $ARG ]; then NAME=$ARG @@ -262,7 +290,7 @@ else #Assume the user wants to do something. fi fi -# Note: writeconf has highest priority, and it is the only function that can +# Note: w_conf has highest priority, and it is the only function that can # work without any parameters. #============================================================================== @@ -295,6 +323,7 @@ fi NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION/ NOTE=$NOTEDIR$NAME.$EXT -if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.tmp fi - - +if [ "$ENCRYPTION" == "TRUE" ]; then NOTE=$NOTE.enc; fi +if [ "$PRINT" == "TRUE" ]; then print; exit 0; fi +if [ "$CREATE" == "TRUE" ]; then create; fi +if [ "$EDIT" == "TRUE" ]; then edit; fi \ No newline at end of file diff --git a/sns.xcodeproj/project.pbxproj b/sns.xcodeproj/project.pbxproj index c31e0a4..a1b54d1 100644 --- a/sns.xcodeproj/project.pbxproj +++ b/sns.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXFileReference section */ + 5D5625481AC8A26500D6E1B5 /* p_header.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = p_header.sh; sourceTree = ""; }; 5D7E611F1AB74D33001D49B9 /* build.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = build.sh; sourceTree = ""; }; 5DE839761AB9D42F006CB4F6 /* list.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = list.sns.sh; sourceTree = ""; }; 5DE839771AB9D52C006CB4F6 /* delete.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = delete.sns.sh; sourceTree = ""; }; @@ -16,7 +17,7 @@ 5DE8397F1AB9D7B9006CB4F6 /* create.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = create.sns.sh; sourceTree = ""; }; 5DE839801AB9D7DD006CB4F6 /* edit.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = edit.sns.sh; sourceTree = ""; }; 5DE839811AB9D7F8006CB4F6 /* print.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = print.sns.sh; sourceTree = ""; }; - 5DE839821AB9DAA6006CB4F6 /* wconf.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = wconf.sns.sh; sourceTree = ""; }; + 5DE839821AB9DAA6006CB4F6 /* w_conf.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = w_conf.sns.sh; sourceTree = ""; }; 5DE839831AB9DACE006CB4F6 /* sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = sns.sh; sourceTree = ""; }; 5DE839841AB9FDF9006CB4F6 /* help.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = help.sns.sh; sourceTree = ""; }; 5DE839851AB9FE1C006CB4F6 /* pause.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = pause.sns.sh; sourceTree = ""; }; @@ -40,12 +41,13 @@ isa = PBXGroup; children = ( 5DE839861AB9FF14006CB4F6 /* init_default_config.sns.sh */, - 5DE839821AB9DAA6006CB4F6 /* wconf.sns.sh */, + 5DE839821AB9DAA6006CB4F6 /* w_conf.sns.sh */, 5DE839841AB9FDF9006CB4F6 /* help.sns.sh */, 5DE839851AB9FE1C006CB4F6 /* pause.sns.sh */, 5DE839761AB9D42F006CB4F6 /* list.sns.sh */, 5DE839771AB9D52C006CB4F6 /* delete.sns.sh */, 5DE8397F1AB9D7B9006CB4F6 /* create.sns.sh */, + 5D5625481AC8A26500D6E1B5 /* p_header.sh */, 5DE839801AB9D7DD006CB4F6 /* edit.sns.sh */, 5DE839811AB9D7F8006CB4F6 /* print.sns.sh */, );