kontaktdb

Store, retrieve, and edit basic contact information in plain text.
git clone https://git.sr.ht/~jbauer/kontaktdb
Log | Files | Refs | README | LICENSE

commit dfcd3b3599b10944bf4c82617e21b8a6d5eda98d
parent 0ccb3edfbd2a70d1ec282de5fcb6a44a484d68d8
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Sun, 14 Jun 2020 15:50:14 -0400

Add BRE character escaping to query results

Escaping basic regular expression characters ($, ., *, ^, [, /, \)
allows them to be used in contact names and emails. It ensures that sed
does not fail with an error because of trying to interpret these
characters as special commands.

It's now possible to name a contact "$.*[^/\" and include those
characters in email addresses without issue.

Diffstat:
Mkontaktdb | 30++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/kontaktdb b/kontaktdb @@ -19,9 +19,8 @@ VERSION="0.1.0" -# POSIX Shell "Strict Mode" +# POSIX Shell "Strict Mode" (but unset variables are fine) set -o errexit -set -o nounset IFS=$(printf '\n\t') if [ -n "$KONTAKT_DB" ]; then @@ -32,10 +31,21 @@ else database="$HOME"/.kontaktdb fi +queryfile="/tmp/kontaktdb-query-results.txt" +touch $database + +# Escapes all special BRE characters which could appear in a returned entry +escape_entry() +{ +entry=$(sed 's/\\/\\\\/g; s/\//\\\//g; s/\^/\\^/g; s/\[/\\[/g; s/\*/\\*/g; + s/\./\\./g; s/\$/\\$/g' "$queryfile") +} + retrieve_contact() { [ -z "$query" ] && echo "Please specify a query string" && exit 1 - grep -i "$query" "$database" || true + grep -i "$query" "$database" > "$queryfile" || true + escape_entry } add_contact() @@ -43,8 +53,8 @@ add_contact() [ -z "$name" ] && echo "The contact's name must be specified" && exit 1 [ -z "$email" ] && echo "The contact's email must be specified" && exit 1 - exists=$(retrieve_contact) - if [ -n "$exists" ]; then + retrieve_contact + if [ -n "$entry" ]; then echo "A contact with that email already exists" exit 1 fi @@ -55,8 +65,8 @@ add_contact() delete_contact() { - original=$(retrieve_contact) - sed -i "/$original/d" "$database" + retrieve_contact + sed -i "/$entry/d" "$database" echo "Contact deleted from database" } @@ -65,8 +75,8 @@ change_contact() [ -z "$name" ] && echo "The contact's new name must be specified" && exit 1 [ -z "$email" ] && echo "The contact's new email must be specified" && exit 1 - original=$(retrieve_contact) - sed -i "/$original/d" "$database" + retrieve_contact + sed -i "/$entry/d" "$database" printf "%s\t%s\n" "$email" "$name" >> "$database" echo "Contact information changed" @@ -148,7 +158,7 @@ while test $# -gt 0; do exit 0 ;; -l|--list|list) - cat "$database" + cat "$database" exit 0 ;; *)