From 3cb375f64b6c4d80b64e03e829a5c75c5c1d2ac1 Mon Sep 17 00:00:00 2001 From: Jon-William Lewis Date: Thu, 7 May 2015 22:11:13 -0500 Subject: [PATCH] 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 --- build.sh | 33 +- errors.ref | 12 - functions/edit.sns.sh | 26 -- init/stage2.sns.sh | 44 --- sns.sh | 119 +++---- sns.xcodeproj/project.pbxproj | 97 +++--- .../WorkspaceSettings.xcsettings | 10 + sns1.sh | 316 ++++++++++++++++++ {functions => src/includes}/create.sns.sh | 12 +- .../includes/defaults.sh | 0 {functions => src/includes}/delete.sns.sh | 0 src/includes/edit.sns.sh | 21 ++ {functions => src/includes}/help.sns.sh | 0 src/includes/libencryption.sh | 8 + {functions => src/includes}/list.sns.sh | 0 {functions => src/includes}/p_header.sh | 0 {functions => src/includes}/pause.sns.sh | 0 {functions => src/includes}/print.sns.sh | 0 {functions => src/includes}/w_conf.sns.sh | 4 +- {init => src/main}/stage1.sns.sh | 0 src/main/stage2.sns.sh | 41 +++ {init => src/main}/stage3.sns.sh | 5 +- 22 files changed, 540 insertions(+), 208 deletions(-) delete mode 100644 errors.ref delete mode 100644 functions/edit.sns.sh delete mode 100644 init/stage2.sns.sh create mode 100644 sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/WorkspaceSettings.xcsettings create mode 100644 sns1.sh rename {functions => src/includes}/create.sns.sh (87%) rename functions/init_default_config.sns.sh => src/includes/defaults.sh (100%) rename {functions => src/includes}/delete.sns.sh (100%) create mode 100644 src/includes/edit.sns.sh rename {functions => src/includes}/help.sns.sh (100%) create mode 100644 src/includes/libencryption.sh rename {functions => src/includes}/list.sns.sh (100%) rename {functions => src/includes}/p_header.sh (100%) rename {functions => src/includes}/pause.sns.sh (100%) rename {functions => src/includes}/print.sns.sh (100%) rename {functions => src/includes}/w_conf.sns.sh (92%) rename {init => src/main}/stage1.sns.sh (100%) create mode 100644 src/main/stage2.sns.sh rename {init => src/main}/stage3.sns.sh (90%) diff --git a/build.sh b/build.sh index 8b308d4..a41ef91 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ S=sns.sh PROD_STR="Simple Note System" -VER_STR=v2.0a3 +VER_STR=v2.0a4 cat > $S << EOF #!/bin/bash @@ -15,21 +15,22 @@ VER_STR="$VER_STR" EOF -echo "# Section: Functions" >> $S -cat ./functions/init_default_config.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 -cat ./functions/print.sns.sh >> $S -cat ./functions/list.sns.sh >> $S -echo "# End Section: Functions" >> $S -cat ./init/stage1.sns.sh >> $S -cat ./init/stage2.sns.sh >> $S -cat ./init/stage3.sns.sh >> $S +echo "# Section: Functions" >> $S +cat ./src/includes/defaults.sh >> $S +cat ./src/includes/w_conf.sns.sh >> $S +cat ./src/includes/pause.sns.sh >> $S +cat ./src/includes/help.sns.sh >> $S +cat ./src/includes/p_header.sh >> $S +cat ./src/includes/libencryption.sh >> $S +cat ./src/includes/create.sns.sh >> $S +cat ./src/includes/delete.sns.sh >> $S +cat ./src/includes/edit.sns.sh >> $S +cat ./src/includes/print.sns.sh >> $S +cat ./src/includes/list.sns.sh >> $S +echo "# End Section: Functions" >> $S +cat ./src/main/stage1.sns.sh >> $S +cat ./src/main/stage2.sns.sh >> $S +cat ./src/main/stage3.sns.sh >> $S diff --git a/errors.ref b/errors.ref deleted file mode 100644 index ce7c5f3..0000000 --- a/errors.ref +++ /dev/null @@ -1,12 +0,0 @@ -Simple Note System, version 2 -Error Code Reference - -General------------------------------------------------------------------------- -ERR_NO_ARGS 10 No arguments were specified -ERR_NO_OP 20 -ERR_INSUFFICIENT_ARGS 30 A required argument was not provided - - -Encryption---------------------------------------------------------------------- -ERR_NO_SSL 100 Cannot execute openssl -ERR_NO_KEY 110 No encryption key in sns.conf \ No newline at end of file diff --git a/functions/edit.sns.sh b/functions/edit.sns.sh deleted file mode 100644 index 1fb0181..0000000 --- a/functions/edit.sns.sh +++ /dev/null @@ -1,26 +0,0 @@ -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 - - else - printf "\nERROR: Note cannot be opened for editting.\n" - fi -} diff --git a/init/stage2.sns.sh b/init/stage2.sns.sh deleted file mode 100644 index fd8bf45..0000000 --- a/init/stage2.sns.sh +++ /dev/null @@ -1,44 +0,0 @@ -#============================================================================== -# Section: Argument Parsing -#============================================================================== - -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. - 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 - 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 - fi - fi - done - if [ -n "$NAME" -a -z "$NOTEBOOK" ]; then - NOTEBOOK="$NAME" - NAME="" - fi -fi - -# Note: w_conf has highest priority, and it is the only function that can -# work without any parameters. - -#============================================================================== -# End Section: Argument Parsing -#============================================================================== diff --git a/sns.sh b/sns.sh index cede94d..434f2f4 100755 --- a/sns.sh +++ b/sns.sh @@ -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 diff --git a/sns.xcodeproj/project.pbxproj b/sns.xcodeproj/project.pbxproj index a1b54d1..6dec092 100644 --- a/sns.xcodeproj/project.pbxproj +++ b/sns.xcodeproj/project.pbxproj @@ -7,70 +7,80 @@ objects = { /* Begin PBXFileReference section */ - 5D5625481AC8A26500D6E1B5 /* p_header.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = p_header.sh; sourceTree = ""; }; + 5D22D6A21AFC4F5A0036DC52 /* create.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = create.sns.sh; sourceTree = ""; }; + 5D22D6A31AFC4F5A0036DC52 /* delete.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = delete.sns.sh; sourceTree = ""; }; + 5D22D6A41AFC4F5A0036DC52 /* edit.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = edit.sns.sh; sourceTree = ""; }; + 5D22D6A51AFC4F5A0036DC52 /* help.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = help.sns.sh; sourceTree = ""; }; + 5D22D6A61AFC4F5A0036DC52 /* defaults.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = defaults.sh; sourceTree = ""; }; + 5D22D6A71AFC4F5A0036DC52 /* list.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = list.sns.sh; sourceTree = ""; }; + 5D22D6A81AFC4F5A0036DC52 /* p_header.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = p_header.sh; sourceTree = ""; }; + 5D22D6A91AFC4F5A0036DC52 /* pause.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = pause.sns.sh; sourceTree = ""; }; + 5D22D6AA1AFC4F5A0036DC52 /* print.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = print.sns.sh; sourceTree = ""; }; + 5D22D6AB1AFC4F5A0036DC52 /* w_conf.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = w_conf.sns.sh; sourceTree = ""; }; + 5D22D6AD1AFC4F5A0036DC52 /* stage1.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage1.sns.sh; sourceTree = ""; }; + 5D22D6AE1AFC4F5A0036DC52 /* stage2.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage2.sns.sh; sourceTree = ""; }; + 5D22D6AF1AFC4F5A0036DC52 /* stage3.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage3.sns.sh; sourceTree = ""; }; + 5D22D6B01AFC5B100036DC52 /* libencryption.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = libencryption.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 = ""; }; - 5DE8397B1AB9D629006CB4F6 /* stage1.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage1.sns.sh; sourceTree = ""; }; - 5DE8397C1AB9D64B006CB4F6 /* stage2.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage2.sns.sh; sourceTree = ""; }; - 5DE8397D1AB9D694006CB4F6 /* stage3.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = stage3.sns.sh; sourceTree = ""; }; - 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 /* 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 = ""; }; - 5DE839861AB9FF14006CB4F6 /* init_default_config.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = init_default_config.sns.sh; sourceTree = ""; }; 5DE839881ABA04DD006CB4F6 /* errors.ref */ = {isa = PBXFileReference; lastKnownFileType = text; path = errors.ref; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXGroup section */ + 5D22D6A01AFC4F5A0036DC52 /* src */ = { + isa = PBXGroup; + children = ( + 5D22D6A11AFC4F5A0036DC52 /* includes */, + 5D22D6AC1AFC4F5A0036DC52 /* main */, + ); + path = src; + sourceTree = ""; + }; + 5D22D6A11AFC4F5A0036DC52 /* includes */ = { + isa = PBXGroup; + children = ( + 5D22D6A21AFC4F5A0036DC52 /* create.sns.sh */, + 5D22D6A31AFC4F5A0036DC52 /* delete.sns.sh */, + 5D22D6A41AFC4F5A0036DC52 /* edit.sns.sh */, + 5D22D6A51AFC4F5A0036DC52 /* help.sns.sh */, + 5D22D6A61AFC4F5A0036DC52 /* defaults.sh */, + 5D22D6A71AFC4F5A0036DC52 /* list.sns.sh */, + 5D22D6A81AFC4F5A0036DC52 /* p_header.sh */, + 5D22D6A91AFC4F5A0036DC52 /* pause.sns.sh */, + 5D22D6AA1AFC4F5A0036DC52 /* print.sns.sh */, + 5D22D6AB1AFC4F5A0036DC52 /* w_conf.sns.sh */, + 5D22D6B01AFC5B100036DC52 /* libencryption.sh */, + ); + path = includes; + sourceTree = ""; + }; + 5D22D6AC1AFC4F5A0036DC52 /* main */ = { + isa = PBXGroup; + children = ( + 5D22D6AD1AFC4F5A0036DC52 /* stage1.sns.sh */, + 5D22D6AE1AFC4F5A0036DC52 /* stage2.sns.sh */, + 5D22D6AF1AFC4F5A0036DC52 /* stage3.sns.sh */, + ); + path = main; + sourceTree = ""; + }; 5D7E61181AB74D11001D49B9 = { isa = PBXGroup; children = ( + 5D22D6A01AFC4F5A0036DC52 /* src */, 5DE839881ABA04DD006CB4F6 /* errors.ref */, 5DE839831AB9DACE006CB4F6 /* sns.sh */, - 5DE839791AB9D5F8006CB4F6 /* init */, 5D7E611F1AB74D33001D49B9 /* build.sh */, - 5DE839751AB9D41B006CB4F6 /* functions */, ); sourceTree = ""; }; - 5DE839751AB9D41B006CB4F6 /* functions */ = { - isa = PBXGroup; - children = ( - 5DE839861AB9FF14006CB4F6 /* init_default_config.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 */, - ); - path = functions; - sourceTree = ""; - }; - 5DE839791AB9D5F8006CB4F6 /* init */ = { - isa = PBXGroup; - children = ( - 5DE8397D1AB9D694006CB4F6 /* stage3.sns.sh */, - 5DE8397B1AB9D629006CB4F6 /* stage1.sns.sh */, - 5DE8397C1AB9D64B006CB4F6 /* stage2.sns.sh */, - ); - path = init; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXProject section */ 5D7E61191AB74D11001D49B9 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0620; + LastUpgradeCheck = 0630; }; buildConfigurationList = 5D7E611C1AB74D11001D49B9 /* Build configuration list for PBXProject "sns" */; compatibilityVersion = "Xcode 3.2"; @@ -91,6 +101,7 @@ 5D7E611D1AB74D11001D49B9 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ONLY_ACTIVE_ARCH = YES; }; name = Debug; }; diff --git a/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/WorkspaceSettings.xcsettings b/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..659c876 --- /dev/null +++ b/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/WorkspaceSettings.xcsettings @@ -0,0 +1,10 @@ + + + + + HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges + + SnapshotAutomaticallyBeforeSignificantChanges + + + diff --git a/sns1.sh b/sns1.sh new file mode 100644 index 0000000..e1c251f --- /dev/null +++ b/sns1.sh @@ -0,0 +1,316 @@ +#!/bin/bash +#========================================================== +# Simple Note System, v1.1 +# Copyright 2014, Xenese Labs/Sicron-Perion XNF +#========================================================== + +PROD_STR="Simple Note System" +VER_STR=v1.1 + +#============================================================================== +# Section: Helper Functions +#============================================================================== +function writeconf { +cat > $HOME/.sns << EOF +#========================================================== +# Simple Note System Config, v1.1 +# Copyright 2014, Xenese Labs/Sicron-Perion XNF +#========================================================== + +#Directory where notes will be stored +BASEDIR=$HOME/notes + +#File extension to use (for listing notes) +EXT=note + +#Preferred Editor +EDITOR=vim + +#Encryption +#WARNING: ANY PREVIOUSLY UNENCRYPTED NOTES WILL BE LOST +ENCRYPTION="FALSE" +ENC_KEY="" +EOF + +chmod 600 $HOME/.sns +} + +function help { + echo "" + echo "usage: sns [-ce] NAME NOTEBOOK SECTION" + echo " sns [-d ] NAME NOTEBOOK SECTION" + echo " sns [-l ] NOTEBOOK" + echo " sns [-w ]" + + echo "" + echo " -c | --create : Create note" + echo " -d | --delete : Delete note" + echo " -e | --edit : Open note for editing" + echo " -p | --print : Print note to console" + echo " -l | --list : List all notes in NOTEBOOK" + echo " -w | --wconf : Write default configuration to ~/.sns (useful for Encryption)" + echo "" +} + +function pause { + read -p " Press [Enter] to continue." + echo "" +} + +#============================================================================== +# End Section: Helper Functions +#============================================================================== + +#============================================================================== +# Section: Configuration +#============================================================================== +if [ -r $HOME/.sns ]; then + source $HOME/.sns +else + BASEDIR=$HOME/notes + EDITOR=vim + EXT=note +fi + +if [ "$ENCRYPTION" == "TRUE" ]; then + if [ -z "$ENC_KEY" ]; then + ERR_NO_KEY="TRUE" + ENCRYPTION="FALSE" + pause + fi + command -v openssl >/dev/null 2>&1 || { ERR_NO_SSL="TRUE"; ENCRYPTION="FALSE"; } +fi + +if [ "$ENCRYPTION" == "TRUE" ]; then + PROD_STR="Simple Note System (Encryption Enabled)" + EXT="$EXT.enc" + if [ ! -d ~/.tmp ]; then + mkdir -p ~/.tmp + fi +fi + +echo "$PROD_STR, $VER_STR" +if [ -n "$ERR_NO_SSL" ]; then + echo >&2 " Warning: OpenSSL not installed. Encryption disabled." +fi +if [ -n "$ERR_NO_KEY" ]; then + echo " Warning: No encryption key was provided. Encryption disabled." +fi + +if [ -n "$ERR_NO_SSL" -o -n "$ERR_NO_KEY" ]; then + pause +fi + +#============================================================================== +# End Section: Read Configuration +#============================================================================== + + +#============================================================================== +# Section: Argument Parsing +#============================================================================== + +NAME="" +NOTEBOOK="" +SECTION="" + +if [ -z "$1" ]; then #If no input was given, print help + help + exit +else #Assume the user wants to do something. + 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 writeconf; exit + else + if [ -z "$NAME" -a -n $ARG ]; then + NAME=$ARG + echo "Name: $NAME" + elif [ -z "$NOTEBOOK" -a -n $ARG ]; then + NOTEBOOK=$ARG + echo "Notebook: $NOTEBOOK" + elif [ -z "$SECTION" -a -n $ARG ]; then + SECTION=$ARG + echo "Section: $SECTION" + fi + fi + done +fi + +#============================================================================== +# End Section: Argument Parsing +#============================================================================== + +#============================================================================== +# Section: Main +#============================================================================== + + NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION/ + NOTE=$NOTEDIR$NAME.$EXT + + if [ "$ENCRYPTION" == "TRUE" ]; then + NOTE=$NOTE.tmp + fi + + #========================================================================== + # Subection: List + #========================================================================== + if [ -n "$LIST" ]; then + NOTEBOOK="$NAME" #In case of a list command, arg parsing fails. + if [ -z "$NOTEBOOK" ]; then + echo " ERROR: Insufficient arguments" + help + exit + else + if [ -d "$BASEDIR"/"$NOTEBOOK" ]; then + + echo "" + printf "Notes in $(basename $NOTEBOOK):" + echo "" + NOTES=( $(find $BASEDIR/$NOTEBOOK -name "*.$EXT" -print0 | sed s:$BASEDIR/$NOTEBOOK/:" ":g | sed -e s:".$EXT"::g | tr "/" " ") ) + let i=0 + for NOTE in ${NOTES[@]}; do + if [ -d $BASEDIR/$NOTEBOOK/$NOTE ]; then + if [ "$LAST_SECTION" != "$NOTE" ]; then + printf " Section: $NOTE\n" + fi + LAST_SECTION=$NOTE + else + #if [ $(($i % 1)) -eq 0 ]; then + # printf "\n " + #fi + printf " $NOTE\n" + fi + let i++ + done + printf "\n" + else + echo "" + echo "ERROR: Notebook $NOTEBOOK does not exist." + echo "" + fi + fi + exit + fi + #====================================================================== + # Sanity Check - Actions below require a valid $NAME and $NOTEBOOK + #====================================================================== + if [ -z "$NAME" -o -z "$NOTEBOOK" ]; then + echo " ERROR: Insufficient arguments" + help + exit 10 + fi + #========================================================================== + # Subection: Delete + #========================================================================== + if [ "$DELETE" == "TRUE" ]; then + if [ -e $NOTE -o -e ${NOTE%.*} ]; then + if [ "$ENCRYPTION" == "TRUE" ]; then + rm ${NOTE%.*} + else + rm $NOTE + fi + + echo "" + echo "Deleted note: $NOTEBOOK/$SECTION$NAME." + exit + else + + echo "" + echo "ERROR: Note $NOTEBOOK/$SECTION$NAME does not exist." + exit + fi + fi + + #========================================================================== + # Subection: Create + #========================================================================== + if [ -z "$CREATE" -a -z "$EDIT" -a -z "$PRINT" ]; then #If no action specified, print help and exit + help + exit + else + + + if [ "$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 + + #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 + + #========================================================================== + # Subection: Edit + #========================================================================== + if [ "$EDIT" == "TRUE" ]; then + if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then + if [ -z "$CREATE" ]; then + if [ "$ENCRYPTION" == "TRUE" ]; then + openssl enc -d -aes-256-cbc -in ${NOTE%.*} -pass pass:$ENC_KEY > $NOTE + fi + echo "" >> $NOTE + echo "EDIT $(date)" >> $NOTE + fi + $EDITOR $NOTE + if [ "$ENCRYPTION" == "TRUE" ]; then + openssl enc -aes-256-cbc -salt -in $NOTE -out ${NOTE%.*} -pass pass:$ENC_KEY + rm $NOTE + fi + + else + + echo "" + echo "ERROR: Note cannot be opened for editting." + echo "" + + fi + fi + +fi + #========================================================================== + # Subection: Print + #========================================================================== + if [ "$PRINT" == "TRUE" ]; then + if [ -r "$NOTE" -o -r ${NOTE%.*} ]; then + if [ -z "$CREATE" ]; then + if [ "$ENCRYPTION" == "TRUE" ]; then + openssl enc -d -aes-256-cbc -in ${NOTE%.*} -pass pass:$ENC_KEY + else + cat $NOTE + echo "" >> $NOTE + fi + else + + echo "" + echo "ERROR: Note cannot be found." + echo "" + + fi + fi + +fi + +exit diff --git a/functions/create.sns.sh b/src/includes/create.sns.sh similarity index 87% rename from functions/create.sns.sh rename to src/includes/create.sns.sh index cb6519e..cedeaa9 100644 --- a/functions/create.sns.sh +++ b/src/includes/create.sns.sh @@ -28,15 +28,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" } diff --git a/functions/init_default_config.sns.sh b/src/includes/defaults.sh similarity index 100% rename from functions/init_default_config.sns.sh rename to src/includes/defaults.sh diff --git a/functions/delete.sns.sh b/src/includes/delete.sns.sh similarity index 100% rename from functions/delete.sns.sh rename to src/includes/delete.sns.sh diff --git a/src/includes/edit.sns.sh b/src/includes/edit.sns.sh new file mode 100644 index 0000000..a0c5477 --- /dev/null +++ b/src/includes/edit.sns.sh @@ -0,0 +1,21 @@ +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 + + + +} diff --git a/functions/help.sns.sh b/src/includes/help.sns.sh similarity index 100% rename from functions/help.sns.sh rename to src/includes/help.sns.sh diff --git a/src/includes/libencryption.sh b/src/includes/libencryption.sh new file mode 100644 index 0000000..392bf8d --- /dev/null +++ b/src/includes/libencryption.sh @@ -0,0 +1,8 @@ +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" +} diff --git a/functions/list.sns.sh b/src/includes/list.sns.sh similarity index 100% rename from functions/list.sns.sh rename to src/includes/list.sns.sh diff --git a/functions/p_header.sh b/src/includes/p_header.sh similarity index 100% rename from functions/p_header.sh rename to src/includes/p_header.sh diff --git a/functions/pause.sns.sh b/src/includes/pause.sns.sh similarity index 100% rename from functions/pause.sns.sh rename to src/includes/pause.sns.sh diff --git a/functions/print.sns.sh b/src/includes/print.sns.sh similarity index 100% rename from functions/print.sns.sh rename to src/includes/print.sns.sh diff --git a/functions/w_conf.sns.sh b/src/includes/w_conf.sns.sh similarity index 92% rename from functions/w_conf.sns.sh rename to src/includes/w_conf.sns.sh index 4ac6034..d934cc1 100644 --- a/functions/w_conf.sns.sh +++ b/src/includes/w_conf.sns.sh @@ -13,7 +13,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 diff --git a/init/stage1.sns.sh b/src/main/stage1.sns.sh similarity index 100% rename from init/stage1.sns.sh rename to src/main/stage1.sns.sh diff --git a/src/main/stage2.sns.sh b/src/main/stage2.sns.sh new file mode 100644 index 0000000..5694d64 --- /dev/null +++ b/src/main/stage2.sns.sh @@ -0,0 +1,41 @@ +#============================================================================== +# Section: Argument Parsing +#============================================================================== + +NAME="" +NOTEBOOK="" +SECTION="" +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 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" + fi + fi + done + 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 and help have highest priority, as they are the only functions +# that can work without any parameters. + +#============================================================================== +# End Section: Argument Parsing +#============================================================================== diff --git a/init/stage3.sns.sh b/src/main/stage3.sns.sh similarity index 90% rename from init/stage3.sns.sh rename to src/main/stage3.sns.sh index 73b9276..1aa53e4 100644 --- a/init/stage3.sns.sh +++ b/src/main/stage3.sns.sh @@ -9,7 +9,6 @@ if [ -z "$NOTEBOOK" ]; then fi if [ -n "$LIST" ]; then - list exit 0 fi @@ -22,8 +21,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