From b09eb3451192ca82b8d841de73b80d7e4b23caec Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Mon, 9 Apr 2018 12:59:39 +0200 Subject: digest-mailer.in: -f = flush new --- digest-mailer.in | 97 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 22 deletions(-) diff --git a/digest-mailer.in b/digest-mailer.in index ec333c8..b62b57c 100644 --- a/digest-mailer.in +++ b/digest-mailer.in @@ -6,11 +6,26 @@ verwendung() { >&2 echo 'digest-mailer collects emails into digests before sending them.' >&2 echo '' >&2 echo 'Usage: digest-mailer [OPTIONS]' + >&2 echo ' -f:' + >&2 echo ' flush all pending emails' + >&2 echo ' -f $receiver:' + >&2 echo ' flush pending emails to $receiver (hash)' >&2 echo \ '#HELPTEXT# #' exit 1 } +extract_receiver_hash() { + sed -n ' + /^$/q + s/^To:\s*// + T + p + ' | \ + sha224sum | \ + awk '{print $1}' +} + if [ "x$1" = 'x--help' ]; then verwendung fi @@ -20,6 +35,19 @@ if [ "x$1" = 'x--version' ]; then exit fi +if [ "x$1" = 'x-f' ]; then + flush=true + shift + if [ $# -eq 0 ]; then + receiver='' + else + receiver="$1" + shift + fi +else + flush=false +fi + if [ $# -ne 0 ]; then verwendung fi @@ -34,33 +62,56 @@ then . '#ETCDIR#/digest-mailer.conf' fi -message=$( - cat -) sender=$( whoami | \ sha224sum | \ awk '{print $1}' ) -receiver=$( - printf '%s\n' "${message}" | \ - sed -n ' - /^$/q - s/^To:\s*// - T - p - ' | \ - sha224sum | \ - awk '{print $1}' -) +if ${flush} && \ + [ -z "${receiver}" ]; then + exec 9> '#LIBDIR#/digest-mailer/digest-mailer.flush.from.'"${sender}"'.lock' + if ! flock -n 9; then + # silently fail if lock is not available ("flush" is not _that_ important) + exit + fi + find '#LIBDIR#/digest-mailer' -regextype grep -maxdepth 1 -mindepth 1 \ + -regex ".*/${sender}\.[0-9a-f]\+\.[0-9]\+\.[0-9a-f]\+\$" | \ + while read -r file; do + extract_receiver_hash < "${file}" + done | \ + sort -u | \ + xargs -rn1 "$0" -f + exit +fi + time=$( date +%s ) -hash=$( - printf '%s' "${message}" | \ - sha224sum | \ - awk '{print $1}' -) +if ! ${flush}; then + message=$( + cat + ) + receiver=$( + printf '%s\n' "${message}" | \ + extract_receiver_hash + ) + hash=$( + printf '%s' "${message}" | \ + sha224sum | \ + awk '{print $1}' + ) + exec 9> '#LIBDIR#/digest-mailer/digest-mailer.from.'"${sender}"'.to.'"${receiver}"'.lock' + flock 9 + # we block if that lock is not available - the sender expects his mails delivered! + printf '%s' "${message}" > "#LIBDIR#/digest-mailer/${sender}.${receiver}.${time}.${hash}" +else + exec 9> '#LIBDIR#/digest-mailer/digest-mailer.from.'"${sender}"'.to.'"${receiver}"'.lock' + if ! flock -n 9; then + # silently fail if lock is not available ("flush" is not _that_ important) + exit + fi +fi + if [ -s "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent" ]; then last_sent=$( cat "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent" @@ -69,8 +120,6 @@ else last_sent='0' fi -printf '%s' "${message}" > "#LIBDIR#/digest-mailer/${sender}.${receiver}.${time}.${hash}" - first_unsent=$( find '#LIBDIR#/digest-mailer' -regextype grep -maxdepth 1 -mindepth 1 \ -regex ".*/${sender}\.${receiver}\.[0-9]\+\.[0-9a-f]\+\$" \ @@ -79,7 +128,11 @@ first_unsent=$( sort -nr | \ tail -n1 ) - +if ${flush} && \ + [ -z "${first_unsent}" ]; then + # no pending emails at all + exit +fi to_send=$( find '#LIBDIR#/digest-mailer' -regextype grep -maxdepth 1 -mindepth 1 \ -regex ".*/${sender}\.${receiver}\.[0-9]\+\.[0-9a-f]\+\$" | \ -- cgit v1.2.3-70-g09d2