Add validators to valentine template - postreich - Unnamed repository; edit this file 'description' to name the repository.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit e3b970d4f9ad336f7eaa8894e28a789c35082020
 (DIR) parent f54a9529b65ca142d6336a0e349889f2d822b8c8
 (HTM) Author: Scarlett McAllister <no+reply@roygbyte.com>
       Date:   Wed, 24 Jan 2024 21:12:39 -0400
       
       Add validators to valentine template
       
       Diffstat:
         M geomyidae/postreich/api/templates/… |      40 +++++++++++++++++++------------
       
       1 file changed, 25 insertions(+), 15 deletions(-)
       ---
 (DIR) diff --git a/geomyidae/postreich/api/templates/valentine/main b/geomyidae/postreich/api/templates/valentine/main
       @@ -9,20 +9,17 @@
        . ./api/common # this directory is based on sendmail.dcgi PWD
        
        WIDTH=45
       -MESSAGE_BYTE_LIMIT=128
       -
       -find_value_in_message() {
       -    # Invoked like this `find_arg_in_path "type" "type=1?handle=roygbyte"`
       -    # return the value associated with type. "type" can of course
       -    # be any value expected to be found in $2.
       -    printf "%s" "$2" \
       -        | xargs -d '&' -I x echo x \
       -        | grep -oE "$1=(.*)" \
       -        | sed "s/$1=//"
       -}
       +MESSAGE_BYTE_LIMIT=69
       +SWEETHEARTS="api/$TEMPLATES/valentine/sweethearts.txt"
        
        validate_text() {
       -    echo $1
       +    len=$( printf "%s" "$1" \
       +               | wc --bytes )
       +    if [ $len -gt $MESSAGE_BYTE_LIMIT -o \
       +              $len -le 1 ]; then
       +        return 1
       +    fi
       +    return 0
        }
        
        validate_border() {
       @@ -30,12 +27,13 @@ validate_border() {
        }
        
        validate_sweetheart() {
       -    echo $1
       +    grep -xc "$1" "$SWEETHEARTS" 2> /dev/null
       +    return $?
        }
        
        path="$1"
        query="$2"
       -read message
       +# read message
        
        border=$( find_value_in_args "border" "$path" )
        text=$( find_value_in_args "text" "$path" )
       @@ -46,7 +44,7 @@ fi
        sweetheart=$( find_value_in_args "sweetheart" "$path" )
        
        choose_sweetheart() {
       -    cat "api/$TEMPLATES/valentine/sweethearts.txt" \
       +    cat "$SWEETHEARTS" \
                | awk -v args="$path" \
                      'BEGIN {FS = "\t"} { printf "[1|%s|sendmail.dcgi?%s?sweetheart=%s|localhost|70]\n", $1, args, $1 }'
        }
       @@ -70,6 +68,18 @@ if [ -z "$text" ]; then
            return 1
        fi
        
       +validate_sweetheart "$sweetheart"
       +if [ ! $? -eq 0 ]; then
       +    printf "Incorrect sweetheart\n"
       +    return 1
       +fi
       +
       +validate_text "$text"
       +if [ ! $? -eq 0 ]; then
       +    printf "Message not within limit of 1-%s bytes.\n" \
       +           $MESSAGE_BYTE_LIMIT
       +    return 1
       +fi
        return 0
        
        # valentine's template will expect a single record with two fields.