summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2018-04-08 21:40:18 +0200
committerErich Eckner <git@eckner.net>2018-04-08 21:40:18 +0200
commit4dc2bbda9102321cee5701736be76ad559e7b7f6 (patch)
tree12d1f786bac576d8fb5562f6f244980f2580844a
parent18151ae9d83ee2175a47782baabe2a83612e8e8a (diff)
downloaddigest-mailer-4dc2bbda9102321cee5701736be76ad559e7b7f6.tar.xz
soweit fertig
-rw-r--r--Makefile2
-rw-r--r--digest-mailer.in92
2 files changed, 92 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index abb7884..fe6572a 100644
--- a/Makefile
+++ b/Makefile
@@ -48,7 +48,7 @@ all: man.commons digest-mailer digest-mailer.1
.PHONY: install dist clean
install: all
- install -d -m0755 $(DESTDIR)$(LIBDIR)/digest-mailer
+ install -d -m1777 $(DESTDIR)$(LIBDIR)/digest-mailer
install -D -m0755 digest-mailer $(DESTDIR)$(BINDIR)/digest-mailer
install -D -m0644 digest-mailer.1 $(DESTDIR)$(MANDIR)/man1/digest-mailer.1
install -D -m0644 digest-mailer.conf $(DESTDIR)$(ETCDIR)/digest-mailer.conf
diff --git a/digest-mailer.in b/digest-mailer.in
index 4e88494..be071fd 100644
--- a/digest-mailer.in
+++ b/digest-mailer.in
@@ -1,5 +1,7 @@
#!/bin/bash
+set +e
+
verwendung() {
>&2 echo 'digest-mailer collects emails into digests before sending them.'
>&2 echo ''
@@ -32,4 +34,92 @@ then
. #ETCDIR#/digest-mailer.conf
fi
-#TODO
+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}'
+)
+time=$(
+ date +%s
+)
+hash=$(
+ printf '%s' "${message}" | \
+ sha224sum | \
+ awk '{print $1}'
+)
+if [ -s "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent" ]; then
+ last_sent=$(
+ cat "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent"
+ )
+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]\+\$" \
+ -printf '%f\n' | \
+ cut -d. -f3 | \
+ sort -nr | \
+ tail -n1
+)
+
+to_send=$(
+ find '#LIBDIR#/digest-mailer' -regextype grep -maxdepth 1 -mindepth 1 \
+ -regex ".*/${sender}\.${receiver}\.[0-9]\+\.[0-9a-f]\+\$" | \
+ sort
+)
+to_send_count=$(
+ printf '%s\n' "${to_send}" | \
+ wc -l
+)
+if [ $((last_sent + min_delay)) -gt ${time} ]; then
+ # last email was sent too close
+ exit
+fi
+if [ ${to_send_count} -gt 1 ] && \
+ [ $(printf 'scale=10; (%s+%s)*%s + (%s+%s)*(1-%s)\n' "${min_delay}" "${last_sent}" "${agressivity}" "${max_delay}" "${first_unsent}" "${agressivity}" | bc) -gt ${time} ] && \
+ [ $((first_unsent + max_delay)) -ge ${time} ]; then
+ exit
+fi
+
+printf '%s' "${time}" > "#LIBDIR#/digest-mailer/${sender}.${receiver}.last-sent"
+
+if [ ${to_send_count} -eq 1 ]; then
+ ${mail_cmd} < "${to_send}"
+ rm "${to_send}"
+ exit
+fi
+
+{
+ sed -n '
+ /^$/q
+ s/^Subject:.*$/Subject: mail-digest/
+ ' "$(
+ printf '%s' "${to_send}" | \
+ head -n1
+ )"
+ printf '\n'
+ printf '%s\n' "${to_send}" | \
+ while read -r file; do
+ cat "${file}"
+ printf '\n'
+ rm "${file}"
+ done
+} | ${mail_cmd}