#!/bin/bash #========================================================== # Simple Note System, v2.0a3 # Copyright 2014, Xenese Labs/Sicron-Perion XNF #========================================================== PROD_STR="Simple Note System" 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 w_conf { cat > $HOME/.sns/sns.conf << EOF #========================================================== # Simple Note System Config, v2.0a1 # Copyright 2014, Xenese Labs/Sicron-Perion XNF #========================================================== #Directory where notes will be stored ROOTDIR=$HOME/.sns BASEDIR=$ROOTDIR/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 $ROOTDIR/sns.conf } function pause { read -p " 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 : 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 [ -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 fi echo "$NOTE" } function 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 } 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 } function 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 } function list(){ 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 } # End Section: Functions #============================================================================== # Section: Configuration #============================================================================== if [ -r $HOME/.sns/sns.conf ]; then source $HOME/.sns/sns.conf else init_default_config fi if [ "$ENCRYPTION" == "TRUE" ]; then if [ -z "$ENC_KEY" ]; then ERR_NO_KEY="TRUE" ENCRYPTION="FALSE" 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" if [ ! -d ~/.sns/tmp ]; then mkdir -p ~/.sns/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: Configuration #============================================================================== #============================================================================== # 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 #============================================================================== # Help requires no arguments, and is exclusive. if [ -n "$HELP" ]; then help; exit 0; fi # 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 NOTEDIR=$BASEDIR/$NOTEBOOK/$SECTION/ NOTE=$NOTEDIR$NAME.$EXT 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