From 53a85845f878c709f4ff9e26be8c968a75a559ba Mon Sep 17 00:00:00 2001 From: mtekman Date: Wed, 6 Sep 2017 22:56:09 +0200 Subject: [PATCH] added sync, encryption, and subdir features --- plugins/org-mode-manager/README.md | 18 +--- plugins/org-mode-manager/_org | 21 ++++- plugins/org-mode-manager/orgnotes.sh | 119 ++++++++++++++++++++++++--- 3 files changed, 127 insertions(+), 31 deletions(-) mode change 100644 => 100755 plugins/org-mode-manager/_org diff --git a/plugins/org-mode-manager/README.md b/plugins/org-mode-manager/README.md index 34e16f90..ea8a2419 100644 --- a/plugins/org-mode-manager/README.md +++ b/plugins/org-mode-manager/README.md @@ -1,22 +1,6 @@ # org-mode-manager -This plugin defines a function `org` that provides a tree-like representation of [org-mode](http://orgmode.org/) files in a user configured directory. +This is a merge of [org-mode-manager](https://github.com/mtekman/org-mode-manager), which provides a tree-like representation of [org-mode](http://orgmode.org/) files in a user configured directory via the command `org`. -#### Usage: - org [--remove] -#### Example usage: - - org tulip - # tulip.org is created - # - org banana --remove - # banana.org is removed - # - org flowers/colorful/tulip - # tulip.org is created at path flowers/colorful - # in the user directory - # - -The `.org` extension is automatically added if not specified. diff --git a/plugins/org-mode-manager/_org b/plugins/org-mode-manager/_org old mode 100644 new mode 100755 index 77ac589f..e0747ec1 --- a/plugins/org-mode-manager/_org +++ b/plugins/org-mode-manager/_org @@ -3,7 +3,7 @@ _org(){ local state - local f_dirname=~/.config/org_file.location + local f_dirname=~/.config/org_notes.location touch $f_dirname local f_dir=$(cat $f_dirname) @@ -18,11 +18,24 @@ _org(){ '1: :->org_files'\ '*: :->args' - local frels=$(find ${f_dir} -name '*.org' | sed "s|^${f_dir}/||" | sed "s|.org$||") + ### Attempt to make org print out everything at a subdirectory by tabbing a suggestion, one subdir (depth 1) at a time + #local dirs_at_loc=$(find ${f_dir} -maxdepth 1 -type d); + #local files_at_loc=$(find ${f_dir} -type f -name '*.org' | sed "s|^${f_dir}/||" | sed "s|.org$||") + + #case $state in + # (org_files) + # _arguments "1:files:($files_at_loc $dirs_at_loc)"; + # org --subdir=${org_files} + # return 0 + # ;; + #esac + + local all=$(find ${f_dir} -type f -name '*.org' | sed "s|^${f_dir}/||" | sed "s|.org$||") case $state in - (org_files) _arguments "1:files:($frels)" ;; - esac + (org_files) _arguments "1:files:($all)" ;; + esac + } _org "$@" diff --git a/plugins/org-mode-manager/orgnotes.sh b/plugins/org-mode-manager/orgnotes.sh index f541edd3..91de937d 100644 --- a/plugins/org-mode-manager/orgnotes.sh +++ b/plugins/org-mode-manager/orgnotes.sh @@ -4,7 +4,7 @@ __cleanup_org(){ local file=$1 local dir=$2 - [ "$file" = "" ] || [ "$dir" = "" ] && echo "nothing to clean up." && return 0 + [ "$file" = "" ] || [ "$dir" = "" ] && echo "nothing to clean up." >&2 && return 0 rm `dirname ${file}`/\#`basename ${file}`\# ${file}\~ 2>/dev/null # remove temp files @@ -17,6 +17,34 @@ __cleanup_org(){ done } +__org_get_pass(){ + # password set? + local org_pass=~/.config/org_notes.passmd5 + local pass="" + + if ! [ -e $org_pass ] || [ "$(cat $org_pass)" = "" ];then + echo "org-mode password not set. Please set one below:" >&2; read -s pass1; + echo "and again:" >&2; read -s pass2; + + [ "$pass1" != "$pass2" ] && echo "Passwords do not match -- please try again." >&2 && echo -1 && return -1 + + mkdir -p `dirname $org_pass` + echo $pass1 | md5sum | cut -f 1 -d' ' > $org_pass + echo "Set." >&2 + pass=$pass1 + fi + + + if [ "$pass" = "" ]; then + echo "Please type your org-mode password:" >&2; read -s pass + local set_md5=$(cat $org_pass) + local cur_md5=$(echo $pass | md5sum | cut -f 1 -d' ') + [ "$set_md5" != "$cur_md5" ] && echo "Incorrect." >&2 && echo -1 && return -1 + fi + + echo $pass +} + org(){ local org_loc=~/.config/org_notes.location @@ -33,10 +61,20 @@ or run the install script." local file=$1 local command=$2 + case $file in - "") - tree -CD -P '*.org' -rt $org_dir\ - | sed "s|${org_dir}|\nYou have the following org-mode notes:\n|"\ + "");& + --subdir=*) + local sub_dir=`echo $file | sed 's|--subdir=||'` + local title="You have the following org-mode notes:" + if [ "$sub_dir" != "" ]; then + title=`echo $title | sed "s|:| [${sub_dir}]:|"` + fi + + local root_dir=${org_dir}/${sub_dir} + + tree -CD -P '*.org' -rt ${root_dir} -L 3\ + | sed "s|${root_dir}|\n${title}\n|"\ | sed -r 's|([^[].*)\s(\[.*\])\s(.*)|\t\2\t\1\3|' return -1; @@ -44,16 +82,28 @@ or run the install script." -h);& --help) echo " - `basename $0` [--delete] [--move=] + `basename $0` [--help] [--sync] [--subdir=] ] [--encrypt] [--decrypt]> Generates an org-notes file in a folder location set in the $org_loc file "; return -1; ;; + --sync) + cd ${org_dir}; + git pull; + local updates=`git status ./ -s`; + if [ "$updates" != "" ]; then + echo "$updates" + git add ./ && + git commit -m "update at `date +%Y.%m.%d-%H.%M`" && + git push -u origin master + fi + cd - >/dev/null + return 0;; esac - if [[ "$command" =~ "--" ]]; then - if [ "$command" = "--delete" ] || [[ "$command" =~ "--move=" ]]; then + if [ "$command" != "" ];then + if [ "$command" = "--delete" ] || [[ "$command" =~ "--move=" ]] || [ "$command" = "--encrypt" ] || [ "$command" = "--decrypt" ]; then else echo "Unable to parse: $command" return -1 @@ -69,11 +119,45 @@ Generates an org-notes file in a folder location set in the $org_loc file if [ -e $eloc ]; then case $command in + "--encrypt") + ! [ -e ${eloc} ] && echo "File does not yet exist." && return -1 + [ "`echo $eloc | grep -oP '.crypt.org$'`" != "" ] && echo "File is already encrypted." && return -1 + + echo -n "Encrypt $file? [y/n] "; read ans; + if [ "$ans" = "y" ]; then + + local pass=$(__org_get_pass) + [ $pass = -1 ] && return $pass # exit code + + new_eloc=`dirname $eloc`/`basename $eloc .org`.crypt.org + openssl enc -aes-256-cbc -salt -in ${eloc} -out ${new_eloc} -k $pass && + mv $eloc /tmp/ && + echo "`basename $eloc` -> `basename $new_eloc`" + fi + ;; + + "--decrypt") + ! [ -e ${eloc} ] && echo "File does not yet exist." && return -1 + [ "`echo $eloc | grep -oP '.crypt.org$'`" = "" ] && echo "File is already plain-text." && return -1 + + echo -n "Decrypt $file? [y/n]"; read ans; + if [ "$ans" = "y" ]; then + + local pass=$(__org_get_pass) + [ $pass = -1 ] && return $pass # exit code + + new_eloc=`dirname $eloc`/`basename $eloc .crypt.org`.org + openssl enc -aes-256-cbc -d -in ${eloc} -out ${new_eloc} -k $pass && + mv $eloc /tmp/ && + echo "`basename $eloc` -> `basename $new_eloc`" + fi + ;; + "--delete") echo -n "Remove $file? [y/n] "; read ans; if [ "$ans" = "y" ]; then rm ${eloc} && echo "Deleted." - __cleanup_org $eloc $org_dir + __cleanup_org $eloc $org_dir return 0 fi ;; @@ -98,12 +182,27 @@ Generates an org-notes file in a folder location set in the $org_loc file return -1 fi else - # no command, just create + # no command, just create or open + local eloc=${org_dir}/$file local dirn=`dirname $file` + mkdir -p $org_dir/$dirn - emacs $org_dir/$file + # decrypt first if neccesary + if [ "`echo $file | grep -oP '.crypt.org$'`" != "" ];then + local pass=$(__org_get_pass) + [ $pass = -1 ] && return $pass # exit code + + local new_eloc=`mktemp`.org + #echo -en "\n\t-> decrypting"; + openssl enc -aes-256-cbc -d -in ${eloc} -out ${new_eloc} -k ${pass} + emacs $new_eloc + #echo -en "\t -> re-encrypting"; + openssl enc -aes-256-cbc -salt -in ${new_eloc} -out ${eloc} -k ${pass} + else + emacs $eloc + fi return 0 fi }