diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index c9e5843..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/README.md b/README.md index 41e571c..2cfb451 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,31 @@ Simple Note System ================== -The Simple Note System is a shell script enabling easy management of plain-text -notes. SNS depends on OpenSSL for encryption and uses the vim editor by default. +The Simple Note System is a shell script partially inspired by [pass], in that +it stores notes as normal, plaintext, files in normal folders. It uses the +environment-specified editor, and can be configured to use GPG encryption. - usage: sns [-ce] NAME NOTEBOOK SECTION" - sns [-d ] NAME NOTEBOOK SECTION" - sns [-lp] NOTEBOOK" - sns [-w ]" - sns [-h ]" +SNS was originally conceived one morning during an update to a popular note-taking +app. The thought occurred that a note system need not reinvent the wheel with +its own GUI editor and proprietary file format, but instead could use the tools +already provided by the operating system. - -c | --create : Create note" - -d | --delete : Delete note" - -e | --edit : Open note for editing" - -h | --help : Display this message" - -p | --print : Print note to console" - -l | --list : List all notes in NOTEBOOK" - -w | --wconf : Rewrite default configuration" +As it developed, OpenSSL encryption was dropped in favor of GPG, and the script +was almost entirely rewritten as SNSv2. + + simple note system + ================== + + usage: sns [-cedp] NAME NOTEBOOK SECTION + sns [-l] NOTEBOOK + sns [-hi] + -c | --create : Create note + -d | --delete : Delete note + -e | --edit : Open note for editing + -h | --help : Display this message + -p | --print : Print note to console + -l | --list : List all notes in NOTEBOOK + -i | --init : Write default config and initalize SNS store" + + +[pass]: http://passwordstore.org diff --git a/build.sh b/build.sh index aad3dcc..3a306e3 100644 --- a/build.sh +++ b/build.sh @@ -1,29 +1,28 @@ +#!/bin/bash +#========================================================== +# Simple Note System - Build Script +# Copyright 2016, Xenese Labs/Sicron-Perion XNF +#========================================================== + S=sns.sh bash header.sh > "$S" echo -e "\n# Section: Functions" >> "$S" -cat ./src/includes/create_sns_root.sns.sh >> "$S" +cat ./src/includes/init_store.sns.sh >> "$S" +cat ./src/includes/verify_store.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/libencryption.sns.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" printf "%s\n\n\n\n" "# End Section: Functions" >> "$S" - -printf "\n" >> "$S" -printf "%s\n" "###############################################################################" >> "$S" -printf "%s\n" "#==============================================================================" >> "$S" -printf "%s\n" "# Begin Main Program" >> "$S" -printf "%s\n" "#==============================================================================" >> "$S" -printf "%s\n" "###############################################################################" >> "$S" -printf "\n" >> "$S" - cat ./src/main/stage1.sns.sh >> "$S" cat ./src/main/stage2.sns.sh >> "$S" cat ./src/main/stage3.sns.sh >> "$S" +chmod u+x "$S" exit diff --git a/errors.ref b/errors.ref index 508077d..eb46e38 100644 --- a/errors.ref +++ b/errors.ref @@ -3,10 +3,12 @@ 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 - +ERR_NO_OP 20 No operation was specified +ERR_NO_NOTE 30 A required argument was not provided Encryption---------------------------------------------------------------------- -ERR_NO_GPG 100 GPG is not installed -ERR_NO_KEY 110 No recipient specified \ No newline at end of file +ERR_NO_GPG 100 Encryption is enabled, but GPG is not installed +ERR_NO_KEY 110 Encryption is enabled, but no recipient was specified + +Create-------------------------------------------------------------------------- +ERR_NOTE_EXiSTS 200 The specified note already exists diff --git a/header.sh b/header.sh index 6ba4564..63d8c87 100644 --- a/header.sh +++ b/header.sh @@ -1,22 +1,27 @@ -#!/bin/bash - PROD_STR="Simple Note System" VER_STR="v2.0a8" +YEAR=2016 cat << EOF #!/bin/bash #========================================================== # $PROD_STR, $VER_STR -# Copyright 2014, Xenese Labs/Sicron-Perion XNF +# Copyright $YEAR, Xenese Labs/Sicron-Perion XNF #========================================================== +# Prevent freak accidents involving the root directory if [ -z "\$HOME" ]; then HOME=/home/"\$(whoami)"; fi -PROD_STR="$PROD_STR" +readonly PROD_STR="$PROD_STR" readonly VER_STR="$VER_STR" -readonly ROOT_DIR="\$HOME"/.config/xenlabs/sns +readonly ROOT_DIR="\$HOME"/.config/sns readonly NOTES_DIR="\$ROOT_DIR"/notes readonly TMP_DIR="\$ROOT_DIR"/tmp readonly CONFIG_FILE="\$ROOT_DIR/sns.conf" +readonly RED_COLOR='\033[1;31m' +readonly RESET_COLOR='\033[0m' + +printf "%s\n" "\$PROD_STR" +printf "%s\n" "------------------" EOF diff --git a/nul b/nul deleted file mode 100644 index e69de29..0000000 diff --git a/null b/null deleted file mode 100644 index e69de29..0000000 diff --git a/sns.sh b/sns.sh deleted file mode 100644 index 4bbfa8c..0000000 --- a/sns.sh +++ /dev/null @@ -1,304 +0,0 @@ -#!/bin/bash -#========================================================== -# Simple Note System, v2.0a8 -# Copyright 2014, Xenese Labs/Sicron-Perion XNF -#========================================================== - -if [ -z "$HOME" ]; then HOME=/home/"$(whoami)"; fi - -PROD_STR="Simple Note System" -readonly VER_STR="v2.0a8" -readonly ROOT_DIR="$HOME"/.config/xenlabs/sns -readonly NOTES_DIR="$ROOT_DIR"/notes -readonly TMP_DIR="$ROOT_DIR"/tmp -readonly CONFIG_FILE="$ROOT_DIR/sns.conf" - - -# Section: Functions -function w_conf { - -if [ ! -r "$ROOT_DIR" ]; then mkdir -p "$ROOT_DIR"; fi -if [ ! -d "$TMP_DIR" ]; then mkdir -p "$TMP_DIR" ; fi - - -cat > "$CONFIG_FILE" << EOF -#========================================================== -# Simple Note System Config, v2.0a8 -# Copyright 2014, Xenese Labs/Sicron-Perion XNF -#========================================================== - -#File extension to use (for listing notes) -EXT=note - -#Preferred Editor -if [ -z "$EDITOR" ]; then - EDITOR=vim -fi - -#Encryption -#WARNING: ANY PREVIOUSLY UNENCRYPTED NOTES WILL BE LOST - -#ENCRYPTION="TRUE" -ENCRYPTION="FALSE" - -PUBKEY="" -EOF - -chmod 600 "$CONFIG_FILE" -} -function pause { - read -rp " Press [Enter] to continue." - echo "" -} -function help { - echo "" - echo "usage: sns [-ce] NAME NOTEBOOK SECTION" - echo " sns [-d ] NAME NOTEBOOK SECTION" - echo " sns [-lp] NOTEBOOK" - echo " sns [-w ]" - echo " sns [-h ]" - - echo "" - echo " -c | --create : Create note" - echo " -d | --delete : Delete note" - echo " -e | --edit : Open note for editing" - echo " -h | --help : Display this message" - echo " -p | --print : Print note to console" - echo " -l | --list : List all notes in NOTEBOOK" - echo " -w | --wconf : Rewrite default configuration" - echo "" -} -function p_header(){ - printf "TITLE: %s\nDATE: %s\n" "$NAME" "$(date)" -} -function encrypt(){ -# This function, given a recipient, $PUBKEY; a file to encrypt, $TMP_NOTE; and an -# output file, "$NOTE", will encrypt $TMP_NOTE to $NOTE against $PUBKEY's private -# GPG key. - - gpg -r "$PUBKEY" -o "$NOTE" -e "$TMP_NOTE" - -} - -function decrypt(){ -# This function, given a recipient, $PUBKEY; a file to decrypt, $TMP_NOTE; and an -# output file, "$NOTE", will decrpyt $TMP_NOTE to $NOTE against $PUBKEY's private -# GPG key. - gpg -d "$NOTE" -} -function create(){ - if [ -e "$NOTE" ]; then - printf "\nERROR: Note already exists\nHint: use -e to edit the note.\n" - exit - else - mkdir -p "$NOTE_DIR" - fi - - if [ -z "$ENCRYPTION" ]; then - echo "TITLE: $NAME" > "$NOTE" - echo "DATE: $(date)" >> "$NOTE" - elif [ "$ENCRYPTION" == "TRUE" ]; then - TMP_NOTE="$TMP_DIR"/"$SESSION_ID" - p_header > "$TMP_NOTE" - encrypt - fi - - if [ -e "$NOTE" ]; then - echo "Created note: $NOTEBOOK/$SECTION/$NAME." - fi - -} -function delete(){ - if [ "$DELETE" == "TRUE" ]; then - if [ -e "$NOTE" ]; then - rm "$NOTE" - echo "" - echo "Deleted note: $NOTEBOOK/$SECTION/$NAME." - exit - else - echo "" - echo "ERROR: Note $NOTEBOOK/$SECTION/$NAME does not exist." - exit - fi - fi -} -function edit(){ -if [ ! -r "$NOTE" ]; then - echo "ERROR: Note cannot be opened for editing." - exit 40; -fi - -if [ "$ENCRYPTION" == "TRUE" ]; then - cp "$NOTE" "$NOTE".bk - if [ ! -d "$ROOT_DIR"/tmp ]; then mkdir "$ROOT_DIR"/tmp; fi - TMP_NOTE="$TMP_DIR/$SESSION_ID" - decrypt > "$TMP_NOTE" -else TMP_NOTE="$NOTE"; fi - - -if [ -z "$CREATE" ]; then printf "\nEDIT %s" "$(date)" >> "$TMP_NOTE"; fi - -"$EDITOR" "$TMP_NOTE" - -if [ "$ENCRYPTION" == "TRUE" ]; then - rm "$NOTE" - encrypt; - if [ -r "$NOTE" ]; then rm "$NOTE".bk; fi -fi -} -function print(){ - if [ -r "$NOTE" ]; then - if [ -z "$CREATE" ]; then - if [ "$ENCRYPTION" == "TRUE" ]; then - decrypt - else - cat "$NOTE" - fi - else - printf "\nERROR: Note cannot be found.\n" - fi - fi -} -function list(){ - if [ -d "$NOTES_DIR"/"$NOTEBOOK" ]; then - printf "+%s\n" "$NOTEBOOK" - find "$NOTES_DIR"/"$NOTEBOOK" -type f | while read -r NOTE; do - printf " -%s\n" "$(basename \"$NOTE\" | cut -d . -f 1 )" - done - fi -} -# End Section: Functions - - - - -############################################################################### -#============================================================================== -# Begin Main Program -#============================================================================== -############################################################################### - -#============================================================================== -# Section: Configuration / Stage 1 -#============================================================================== -if [ -r "$CONFIG_FILE" ]; then - source "$CONFIG_FILE" -else - create_sns_root - source "$CONFIG_FILE" -fi - -if [ "$ENCRYPTION" == "TRUE" ]; then - if [ -z "$PUBKEY" ]; then - ERR_NO_KEY="TRUE" - ENCRYPTION="FALSE" - fi - - command -v gpg >/dev/null 2>&1 ||\ - { ERR_NO_GPG="TRUE"; ENCRYPTION="FALSE"; } -fi - -if [ "$ENCRYPTION" == "TRUE" ]; then - PROD_STR="Simple Note System (Encryption Enabled)" - if [ ! -d "$ROOT_DIR"/tmp ]; then - mkdir -p "$ROOT_DIR"/tmp - fi -fi - -echo "$PROD_STR, $VER_STR" - -if [ -z "$EDITOR" ]; then - >&2 echo "Error no editor specified in environment." - -elif [ -n "$ERR_NO_GPG" ]; then - >&2 echo " Error: Encryption was specified, but GPG is not installed." - exit 100 -elif [ -n "$ERR_NO_KEY" ]; then - >&2 echo " Error: No GPG recipient was provided in $CONFIG_FILE. " - exit 110 -fi - - -#============================================================================== -# End Section: Configuration / Stage 1 -#============================================================================== -#============================================================================== -# Section: Argument Parsing / Stage 2 -#============================================================================== - -NAME="" -NOTEBOOK="" -SECTION="" -if [ -z "$1" ]; then help; exit 20 -else - ARGS=( "$@" ) - for ARG in "${ARGS[@]}"; do - if [ "$ARG" = "-c" ] || [ "$ARG" = "--create" ]; then CREATE="TRUE" - elif [ "$ARG" = "-d" ] || [ "$ARG" = "--delete" ]; then DELETE="TRUE" - elif [ "$ARG" = "-e" ] || [ "$ARG" = "--edit" ]; then EDIT="TRUE" - elif [ "$ARG" = "-ce" ] || [ "$ARG" = "-ec" ]; then EDIT="TRUE"; CREATE="TRUE" - elif [ "$ARG" = "-p" ] || [ "$ARG" = "--print" ]; then PRINT="TRUE" - elif [ "$ARG" = "-l" ] || [ "$ARG" = "--list" ]; then LIST="TRUE" - elif [ "$ARG" = "-h" ] || [ "$ARG" == "--help" ]; then help; exit 0 - elif [ "$ARG" = "-w" ] || [ "$ARG" == "--wconf" ]; then create_sns_root; exit 0 - else - if [ -z "$NAME" ] && [ -n "$ARG" ]; then NAME="$ARG" - elif [ -z "$NOTEBOOK" ] && [ -n "$ARG" ]; then NOTEBOOK="$ARG" - elif [ -z "$SECTION" ] && [ -n "$ARG" ]; then SECTION="$ARG" - fi - fi - done - if [ -n "$NAME" ] && [ -z "$NOTEBOOK" ] && [ -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 arguments. - -#============================================================================== -# End Section: Argument Parsing / Stage 2 -#============================================================================== -#============================================================================== -# Section: Actions / Stage 3 -#============================================================================== -# List only requires a notebook, and is exclusive. -if [ -z "$NOTEBOOK" ]; then - echo " ERROR: Insufficient arguments:" - echo " Notebook not specified" - exit 30 -fi - -if [ -n "$LIST" ]; then - list - exit 0 -fi - -#All other functions require a note title and notebook. - -if [ -z "$NAME" ]; then - echo " ERROR: Insufficient arguments:" - echo " Title not specified" - exit 30 -fi - -SESSION_ID="$RANDOM" -NOTE_DIR="$NOTES_DIR"/"$NOTEBOOK"/"$SECTION"/ - - -if [ "$ENCRYPTION" == "TRUE" ]; then readonly NOTE="$NOTE_DIR""$NAME"."$EXT".gpg -else readonly NOTE="$NOTE_DIR""$NAME"."$EXT" -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 -#============================================================================== \ No newline at end of file diff --git a/sns.xcodeproj/project.pbxproj b/sns.xcodeproj/project.pbxproj index 5eb29b4..0c77465 100644 --- a/sns.xcodeproj/project.pbxproj +++ b/sns.xcodeproj/project.pbxproj @@ -15,11 +15,12 @@ 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 /* create_sns_root.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = create_sns_root.sns.sh; sourceTree = ""; }; + 5D22D6AB1AFC4F5A0036DC52 /* init_store.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = init_store.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 = ""; }; + 5D22D6B01AFC5B100036DC52 /* libencryption.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = libencryption.sns.sh; sourceTree = ""; }; + 5D75D24F1C5F13DF001E7B33 /* verify_store.sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = verify_store.sns.sh; sourceTree = ""; }; 5D7E611F1AB74D33001D49B9 /* build.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = build.sh; sourceTree = ""; }; 5D7E91FB1B27FB620030B30D /* header.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = header.sh; sourceTree = ""; }; 5DE839831AB9DACE006CB4F6 /* sns.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = sns.sh; sourceTree = ""; }; @@ -47,8 +48,9 @@ 5D22D6A81AFC4F5A0036DC52 /* p_header.sh */, 5D22D6A91AFC4F5A0036DC52 /* pause.sns.sh */, 5D22D6AA1AFC4F5A0036DC52 /* print.sns.sh */, - 5D22D6AB1AFC4F5A0036DC52 /* create_sns_root.sns.sh */, - 5D22D6B01AFC5B100036DC52 /* libencryption.sh */, + 5D22D6AB1AFC4F5A0036DC52 /* init_store.sns.sh */, + 5D22D6B01AFC5B100036DC52 /* libencryption.sns.sh */, + 5D75D24F1C5F13DF001E7B33 /* verify_store.sns.sh */, ); path = includes; sourceTree = ""; diff --git a/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/UserInterfaceState.xcuserstate b/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/UserInterfaceState.xcuserstate index 5bc316e..df0db65 100644 Binary files a/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/UserInterfaceState.xcuserstate and b/sns.xcodeproj/project.xcworkspace/xcuserdata/xilmwa.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/sns1.sh b/sns1.sh deleted file mode 100644 index e1c251f..0000000 --- a/sns1.sh +++ /dev/null @@ -1,316 +0,0 @@ -#!/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/src/includes/create.sns.sh b/src/includes/create.sns.sh index 54de245..228bad0 100644 --- a/src/includes/create.sns.sh +++ b/src/includes/create.sns.sh @@ -1,22 +1,35 @@ function create(){ - if [ -e "$NOTE" ]; then - printf "\nERROR: Note already exists\nHint: use -e to edit the note.\n" - exit - else - mkdir -p "$NOTE_DIR" + # Depends : p_header + # Requires: $NOTE, $NOTE_DIR, + # Optional: $ENCRYPTION, $SESSION_ID, $TMP_DIR encrypt + # Given a valid setup, create writes the standard note header as specified + # by p_header, to $NOTE. + + # Refuse to overwrite a note + if [ -e "$NOTE_DIR/$NOTE" ]; then + printf " $RED_COLOR!$RESET_COLOR %s\n\t%s\n"\ + "Note already exists"\ + "Hint: use -e to edit the note." + exit 200 fi - if [ -z "$ENCRYPTION" ]; then - echo "TITLE: $NAME" > "$NOTE" - echo "DATE: $(date)" >> "$NOTE" - elif [ "$ENCRYPTION" == "TRUE" ]; then + # If the note's notebook/section does not exist, + # create the appropriate folders. + mkdir -p "$NOTE_DIR"/$(dirname "$NOTE") + + # Write the standard note header + if [ "$ENCRYPTION" == "TRUE" ]; then TMP_NOTE="$TMP_DIR"/"$SESSION_ID" p_header > "$TMP_NOTE" encrypt - fi - - if [ -e "$NOTE" ]; then - echo "Created note: $NOTEBOOK/$SECTION/$NAME." - fi - + else + p_header > "$NOTE_DIR/$NOTE" + fi + # Make sure the note exists, and inform the user of the result. + if [ -e "$NOTE_DIR/$NOTE" ]; then + printf " - %s\n" "Created note: ${NOTE%.*}" + else + printf " $RED_COLOR!$RESET_COLOR%s\n"\ + "Something went wrong, and the note was not created." + fi } diff --git a/src/includes/create_sns_root.sns.sh b/src/includes/create_sns_root.sns.sh deleted file mode 100644 index 81cfb01..0000000 --- a/src/includes/create_sns_root.sns.sh +++ /dev/null @@ -1,31 +0,0 @@ -function w_conf { - -if [ ! -r "$ROOT_DIR" ]; then mkdir -p "$ROOT_DIR"; fi -if [ ! -d "$TMP_DIR" ]; then mkdir -p "$TMP_DIR" ; fi - - -cat > "$CONFIG_FILE" << EOF -#========================================================== -# Simple Note System Config, v2.0a8 -# Copyright 2014, Xenese Labs/Sicron-Perion XNF -#========================================================== - -#File extension to use (for listing notes) -EXT=note - -#Preferred Editor -if [ -z "$EDITOR" ]; then - EDITOR=vim -fi - -#Encryption -#WARNING: ANY PREVIOUSLY UNENCRYPTED NOTES WILL BE LOST - -#ENCRYPTION="TRUE" -ENCRYPTION="FALSE" - -PUBKEY="" -EOF - -chmod 600 "$CONFIG_FILE" -} diff --git a/src/includes/delete.sns.sh b/src/includes/delete.sns.sh index f0b47dc..a8aefed 100644 --- a/src/includes/delete.sns.sh +++ b/src/includes/delete.sns.sh @@ -1,14 +1,11 @@ function delete(){ - if [ "$DELETE" == "TRUE" ]; then - if [ -e "$NOTE" ]; then - rm "$NOTE" - echo "" - echo "Deleted note: $NOTEBOOK/$SECTION/$NAME." - exit - else - echo "" - echo "ERROR: Note $NOTEBOOK/$SECTION/$NAME does not exist." - exit - fi - fi + # Requires: $NOTE, $NOTE_DIR + # Given a valid $NOTE, delete removes $NOTE from sns. + + if [ -e "$NOTE_DIR/$NOTE" ]; then + rm "$NOTE_DIR/$NOTE" + printf " - %s\n" "Deleted note: ${NOTE%.*}." + else + printf " $RED_COLOR!$RESET_COLOR %s\n" "Note ${NOTE%.*} does not exist." + fi } diff --git a/src/includes/edit.sns.sh b/src/includes/edit.sns.sh index e63c412..54702c5 100644 --- a/src/includes/edit.sns.sh +++ b/src/includes/edit.sns.sh @@ -1,12 +1,20 @@ function edit(){ -if [ ! -r "$NOTE" ]; then +# Requires: $EDITOR, $NOTE +# Optional: $ENCRYPTION, $TMP_DIR, $SESSION_ID, decrypt, encrypt + +# Verify an editor was specified +if [ -z "$EDITOR" ]; then + >&2 echo "Error no editor specified in environment." + exit +# Verify the note exists +elif [ ! -r "$NOTE" ]; then echo "ERROR: Note cannot be opened for editing." exit 40; fi - +# When encryption is enabled, decrypt $NOTE to a temp file if [ "$ENCRYPTION" == "TRUE" ]; then - cp "$NOTE" "$NOTE".bk - if [ ! -d "$ROOT_DIR"/tmp ]; then mkdir "$ROOT_DIR"/tmp; fi + cp "$NOTE" "$NOTE".bk #Insurance + if [ ! -d "$TMP_DIR" ]; then mkdir "$TMP_DIR"; fi TMP_NOTE="$TMP_DIR/$SESSION_ID" decrypt > "$TMP_NOTE" else TMP_NOTE="$NOTE"; fi diff --git a/src/includes/help.sns.sh b/src/includes/help.sns.sh index e6b9802..8f26e97 100644 --- a/src/includes/help.sns.sh +++ b/src/includes/help.sns.sh @@ -1,18 +1,14 @@ function help { - echo "" - echo "usage: sns [-ce] NAME NOTEBOOK SECTION" - echo " sns [-d ] NAME NOTEBOOK SECTION" - echo " sns [-lp] NOTEBOOK" - echo " sns [-w ]" - echo " sns [-h ]" + printf "\n%s" "usage: sns [-cedp] NAME NOTEBOOK SECTION" + printf "\n%s" " sns [-l] NOTEBOOK" + printf "\n%s" " sns [-hi ]" - echo "" - echo " -c | --create : Create note" - echo " -d | --delete : Delete note" - echo " -e | --edit : Open note for editing" - echo " -h | --help : Display this message" - echo " -p | --print : Print note to console" - echo " -l | --list : List all notes in NOTEBOOK" - echo " -w | --wconf : Rewrite default configuration" - echo "" + printf "\n%s" " -c | --create : Create note" + printf "\n%s" " -d | --delete : Delete note" + printf "\n%s" " -e | --edit : Open note for editing" + printf "\n%s" " -h | --help : Display this message" + printf "\n%s" " -p | --print : Print note to console" + printf "\n%s" " -l | --list : List all notes in NOTEBOOK" + printf "\n%s" " -i | --init : Write default config and initalize SNS store" + printf "\n" } diff --git a/src/includes/init_store.sns.sh b/src/includes/init_store.sns.sh new file mode 100644 index 0000000..40a4f0d --- /dev/null +++ b/src/includes/init_store.sns.sh @@ -0,0 +1,34 @@ +function init_store { + +if [ ! -d "$ROOT_DIR" ]; then mkdir -p "$ROOT_DIR"; WILL_INIT="TRUE"; fi +if [ ! -d "$TMP_DIR" ]; then mkdir -p "$TMP_DIR" ; WILL_INIT="TRUE"; fi + + +cat > "$CONFIG_FILE" << EOF +# This file contains directives for the Simple Note System. + +EXT=note # File extension to use (for listing notes) + +#EDITOR= # Preferred Editor: + # If you would like to specify a different editor for + # sns to use, you may do so here. + +ENCRYPTION="FALSE" # Main Encryption Toggle: + # WARNING: ANY PREVIOUSLY UNENCRYPTED NOTES WILL BE LOST + # Change this to TRUE to enable encryption. + +PUBKEY="" # Public Key + # Encryption is done using GPG. You must enter your + # public key's identifier here. +EOF + +chmod 600 "$CONFIG_FILE" + +printf "\n - %s\n" "Rewrote Default Configuration" + +if [ "$WILL_INIT" == "TRUE" ]; then + printf " - %s %s\n" "Environment initialized in" "$ROOT_DIR" +else + printf " - %s\n" "Store already initialized." +fi +} diff --git a/src/includes/libencryption.sh b/src/includes/libencryption.sns.sh similarity index 100% rename from src/includes/libencryption.sh rename to src/includes/libencryption.sns.sh diff --git a/src/includes/verify_store.sns.sh b/src/includes/verify_store.sns.sh new file mode 100644 index 0000000..31a59b9 --- /dev/null +++ b/src/includes/verify_store.sns.sh @@ -0,0 +1,7 @@ +function verify_store { + ETC_DIR=$(dirname "$CONFIG_FILE") + STORE_DIRS=("$ROOT_DIR" "$NOTES_DIR" "$TMP_DIR" "$ETC_DIR") + for DIR in "${STORE_DIRS[@]}"; do + mkdir -p "$DIR" + done +} diff --git a/src/main/stage1.sns.sh b/src/main/stage1.sns.sh index af57da1..77d5008 100644 --- a/src/main/stage1.sns.sh +++ b/src/main/stage1.sns.sh @@ -1,13 +1,16 @@ #============================================================================== -# Section: Configuration / Stage 1 +# Stage 1: Read Configuration / Verify Integrity #============================================================================== + if [ -r "$CONFIG_FILE" ]; then source "$CONFIG_FILE" else - create_sns_root + init_store source "$CONFIG_FILE" fi +verify_store + if [ "$ENCRYPTION" == "TRUE" ]; then if [ -z "$PUBKEY" ]; then ERR_NO_KEY="TRUE" @@ -19,26 +22,15 @@ if [ "$ENCRYPTION" == "TRUE" ]; then fi if [ "$ENCRYPTION" == "TRUE" ]; then - PROD_STR="Simple Note System (Encryption Enabled)" if [ ! -d "$ROOT_DIR"/tmp ]; then mkdir -p "$ROOT_DIR"/tmp fi fi -echo "$PROD_STR, $VER_STR" - -if [ -z "$EDITOR" ]; then - >&2 echo "Error no editor specified in environment." - -elif [ -n "$ERR_NO_GPG" ]; then +if [ -n "$ERR_NO_GPG" ]; then >&2 echo " Error: Encryption was specified, but GPG is not installed." exit 100 elif [ -n "$ERR_NO_KEY" ]; then >&2 echo " Error: No GPG recipient was provided in $CONFIG_FILE. " exit 110 fi - - -#============================================================================== -# End Section: Configuration / Stage 1 -#============================================================================== diff --git a/src/main/stage2.sns.sh b/src/main/stage2.sns.sh index 97c0392..3ddb324 100644 --- a/src/main/stage2.sns.sh +++ b/src/main/stage2.sns.sh @@ -1,41 +1,57 @@ #============================================================================== -# Section: Argument Parsing / Stage 2 +# Stage 2: Argument Parsing #============================================================================== - -NAME="" -NOTEBOOK="" -SECTION="" +NOTE="" if [ -z "$1" ]; then help; exit 20 else - ARGS=( "$@" ) - for ARG in "${ARGS[@]}"; do - if [ "$ARG" = "-c" ] || [ "$ARG" = "--create" ]; then CREATE="TRUE" - elif [ "$ARG" = "-d" ] || [ "$ARG" = "--delete" ]; then DELETE="TRUE" - elif [ "$ARG" = "-e" ] || [ "$ARG" = "--edit" ]; then EDIT="TRUE" - elif [ "$ARG" = "-ce" ] || [ "$ARG" = "-ec" ]; then EDIT="TRUE"; CREATE="TRUE" - elif [ "$ARG" = "-p" ] || [ "$ARG" = "--print" ]; then PRINT="TRUE" - elif [ "$ARG" = "-l" ] || [ "$ARG" = "--list" ]; then LIST="TRUE" - elif [ "$ARG" = "-h" ] || [ "$ARG" == "--help" ]; then help; exit 0 - elif [ "$ARG" = "-w" ] || [ "$ARG" == "--wconf" ]; then create_sns_root; exit 0 - else - if [ -z "$NAME" ] && [ -n "$ARG" ]; then NAME="$ARG" - elif [ -z "$NOTEBOOK" ] && [ -n "$ARG" ]; then NOTEBOOK="$ARG" - elif [ -z "$SECTION" ] && [ -n "$ARG" ]; then SECTION="$ARG" - fi - fi - done - if [ -n "$NAME" ] && [ -z "$NOTEBOOK" ] && [ -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 +for ARG in "$@"; do + case "$ARG" in + -c|--create) + CREATE="TRUE" + OP="TRUE" + ;; + -d|--delete) + DELETE="TRUE" + OP="TRUE" + ;; + -e|--edit) + EDIT="TRUE" + OP="TRUE" + ;; + -ce|-ec) + CREATE="TRUE" + EDIT="TRUE" + OP="TRUE" + ;; + -l|--list) + LIST="TRUE" + OP="TRUE" + ;; + -p|--print) + PRINT="TRUE" + OP="TRUE" + ;; + -h|--help) + help + exit 0 + ;; + -i|--init-store) + init_store + exit 0 + ;; + *) + NOTE="$ARG" + break; + ;; + esac +done +if [ -n "$NAME" ] && [ -z "$NOTEBOOK" ] && [ -n "$LIST" ]; then + # If a note title was specified, but no notebook, and the list option + # was specified, assume that the detected note title is actually the + # name of a notebook + NOTEBOOK="$NAME" + NAME="" +fi fi -# Note: w_conf and help have highest priority, as they are the only functions -# that can work without any arguments. - -#============================================================================== -# End Section: Argument Parsing / Stage 2 -#============================================================================== +# w_conf and help are called here to avoid excess stage 3 code. diff --git a/src/main/stage3.sns.sh b/src/main/stage3.sns.sh index da7f738..ca3c262 100644 --- a/src/main/stage3.sns.sh +++ b/src/main/stage3.sns.sh @@ -1,39 +1,33 @@ #============================================================================== # Section: Actions / Stage 3 #============================================================================== -# List only requires a notebook, and is exclusive. -if [ -z "$NOTEBOOK" ]; then - echo " ERROR: Insufficient arguments:" - echo " Notebook not specified" +# 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 [ -n "$LIST" ]; then - list - exit 0 +NOTE_DIR="$NOTES_DIR"/"$NOTEBOOK"/"$SECTION" + +if [ "$ENCRYPTION" == "TRUE" ]; then + SESSION_ID="$RANDOM" #SESSION_ID later becomes the temporary filename + readonly NOTE="$NOTE.$EXT.gpg" +else + readonly NOTE="$NOTE.$EXT" fi -#All other functions require a note title and notebook. - -if [ -z "$NAME" ]; then - echo " ERROR: Insufficient arguments:" - echo " Title not specified" - exit 30 -fi - -SESSION_ID="$RANDOM" -NOTE_DIR="$NOTES_DIR"/"$NOTEBOOK"/"$SECTION"/ - - -if [ "$ENCRYPTION" == "TRUE" ]; then readonly NOTE="$NOTE_DIR""$NAME"."$EXT".gpg -else readonly NOTE="$NOTE_DIR""$NAME"."$EXT" -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 +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 -#============================================================================== \ No newline at end of file +#==============================================================================