Skip to content

Commit bc07860

Browse files
committed
rename now working on files with no whitespace in name
1 parent 87433b7 commit bc07860

File tree

1 file changed

+120
-11
lines changed

1 file changed

+120
-11
lines changed

notes

Lines changed: 120 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/sh
22
#
33
# STANDARD(?) UNIX NOTES
44
#
@@ -7,15 +7,18 @@
77
# The idea came from 'pass' the standard unix password manager
88

99
# VARIABLES
10-
VERSION=0.1
10+
VERSION=1.0
1111
#NOTES_UMASK='077'
1212
NOTESDIR="${HOME}/.notes"
13+
CONFIGFILE="${NOTESDIR}/config"
1314
INITIAL_NOTEBOOK="${NOTESDIR}/notes"
1415
DEFAULT_POINTER="${NOTESDIR}/DEFAULT"
1516
USE_POINTER="${NOTESDIR}/USE"
1617
GPGKEY=''
1718
GPG_OPTS="--quiet --yes --compress-algo=none --no-encrypt-to"
1819
GPG="gpg"
20+
SHREDCMD="rm"
21+
#SHREDCMD="shred"
1922

2023
#
2124
# initialize notes system
@@ -47,29 +50,114 @@ cmd_init (){ # setup directories and GPG key to be used
4750
}
4851

4952
create_config () {
50-
touch $NOTESDIR/config
51-
chmod 600 $NOTESDIR/config
53+
touch $CONFIGFILE
54+
chmod 600 $CONFIGFILE
5255

5356
echo You have the following private keys on your keyring
5457
gpg -K
5558

59+
if [ "`gpg -K | wc -l`" = "0" ] ; then
60+
echo No Private keys in keyring ... aborting
61+
exit 1
62+
fi
63+
5664
echo By DEFAULT we will use the first key found as the key for encrypting
5765
echo If this is NOT what is required please edit $NOTESDIR/config to
5866
echo reflect the recipient you wish to use
5967
echo You will need to edit the KEY parameter and set it to the email
6068
echo address of the key you wish to use
6169

6270
KEY="`gpg -K | grep uid | head -1 | sed 's/>$//; s/^.*<//'`"
63-
echo "KEY $KEY" > $NOTESDIR/config
71+
echo "KEY $KEY" > $CONFIGFILE
6472

6573
echo Default config written:
66-
cat $NOTESDIR/config
74+
cat $CONFIGFILE
6775
}
6876

6977
get_recipient () {
70-
KEY=`grep KEY $NOTESDIR/config | sed s/^KEY// | tr -d [:blank:]`
78+
KEY=`grep KEY $CONFIGFILE| sed s/^KEY// | tr -d [:blank:]`
79+
}
80+
81+
get_gpg_keyid () {
82+
newkeyid="$1"
83+
echo newkeyid supplied $newkeyid
84+
85+
if [ "$newkeyid" != "" ] ; then
86+
87+
validate_gpg_keyid $newkeyid
88+
89+
echo valid gpg key $newkeyid
90+
else
91+
echo No key supplied:
92+
fi
93+
7194
}
7295

96+
validate_gpg_keyid () {
97+
testkey="$1"
98+
keyids="/tmp/keyids.$$"
99+
100+
gpg --list-secret-keys --with-colons --keyid-format short | grep sec | cut -d':' -f 5 > $keyids
101+
102+
echo testing key length
103+
if [ "${#testkey}" != 16 ] ; then
104+
echo Key supplied $testkey is wrong length: length = ${#testkey}
105+
echo Key length should be 16 characters
106+
echo Valid keys are:
107+
cat $keyids
108+
rm $keyids
109+
exit 1
110+
fi
111+
112+
113+
echo valid keyids are ...
114+
cat $keyids
115+
echo Testing $testkey
116+
117+
grep $testkey $keyids > /dev/null
118+
isvalidgpgkey="$?"
119+
echo testing result = $isvalidgpgkey
120+
121+
if [ "$isvalidgpgkey" = "0" ] ; then
122+
echo valid given
123+
124+
else
125+
echo Invalid GPG keyid \n\n
126+
127+
echo Valid GPG keys are
128+
cat $keyids
129+
echo \n\n Type 'gpg -k <keyid> to find out more about key'
130+
#rm $keyids
131+
exit 1
132+
fi
133+
134+
}
135+
136+
recrypt_file () {
137+
newkey = "$1"
138+
target = "$2"
139+
140+
get_recipient
141+
142+
echo decrypt from $KEY
143+
echo filename = $target
144+
echo new gpg key = $newkey
145+
146+
}
147+
148+
get_gpg_key_uids () { # unused but logic may be used at further date
149+
keyids="/tmp/keyids.$$"
150+
gpguids="/tmp/uids.$$"
151+
152+
gpg --list-secret-keys --with-colons --keyid-format short | grep sec | cut -d':' -f 5 > $keyids
153+
154+
echo keyids are
155+
cat $keyids
156+
echo Available keys
157+
cat $keyids | xargs -n 1 gpg -k | grep ^uid | sed 's/>$//; s/^.*<//' > $gpguids
158+
cat $gpguids | sort | nl
159+
160+
}
73161
#
74162
# note functions
75163
#
@@ -277,11 +365,13 @@ SYSTEM:
277365
notes help show help
278366
notes version show version
279367
notes show|ls|list list notes in current notebook
368+
notes newkey email change GPG key
280369
281370
NOTES MANAGEMENT:
282371
283-
notes find|search find notes [YET TO BE IMPLMENTED]
372+
notes find|search find notes [YET TO BE IMPLEMENTED]
284373
notes insert|add note_title add a note
374+
notes view|cat note_title view a note
285375
notes import file import a text file as a note
286376
notes rename|mv note_title rename a note (will prompt for new name)
287377
notes copy|cp note_title copy a note (will prompt for new name)
@@ -346,7 +436,7 @@ cmd_import () {
346436
}
347437

348438
cmd_generate () {
349-
echo cmd_generate not implmented yet
439+
echo cmd_generate not implemented yet
350440
}
351441

352442
cmd_delete () {
@@ -424,9 +514,28 @@ cmd_use () {
424514
}
425515

426516
cmd_newkey () {
427-
echo newkey
517+
mynewkey="$1"
518+
519+
get_recipient
520+
get_gpg_keyid $mynewkey
521+
522+
# now recrypt files
523+
find ~/.notes -name \*.gpg | sed s/.gpg// | \
524+
525+
while read filen ;
526+
do
527+
echo $filen;
528+
gpg -o "${filen}" -d "${filen}.gpg"
529+
gpg -r ${mynewkey} -e "${filen}" && ${SHREDCMD} "${filen}"
530+
531+
done
532+
533+
# need to fix up config file now ...
534+
NEWUSER="`gpg -k ${mynewkey} | grep uid | head -1 | sed 's/>$//; s/^.*<//' `"
535+
sed -i s/^KEY.*$/KEY\\t${NEWUSER}/ ${CONFIGFILE}
428536
}
429537

538+
430539
#########################
431540
# #
432541
# MAIN PROGRAM #
@@ -444,7 +553,7 @@ case "$1" in
444553
help|--help) shift; cmd_usage "$@" ;;
445554
version|--version) shift; cmd_version "$@" ;;
446555
show|ls|list) shift; cmd_show "$@" ;;
447-
view) shift; cmd_view "$@" ;;
556+
view|cat) shift; cmd_view "$@" ;;
448557
find|search) shift; cmd_find "$@" ;;
449558
grep) shift; cmd_grep "$@" ;;
450559
insert|add) shift; cmd_insert "$@" ;;

0 commit comments

Comments
 (0)