#!/bin/bash set -euo pipefail WD=$(mktemp -d) cleanup (){ rm -rf "${WD}" } trap cleanup EXIT cd "${WD}" ITER_SUBS=/opt/cropswap/gdbm-iter-subscriptions.py MIME_INJECT=/opt/cropswap/mime-inject.py DELIVERED=false SENDMAIL="/usr/sbin/sendmail" if ((DEBUG != 0)); then SENDMAIL="$DEBUGMAIL" fi mk_subject(){ subject="$(cat subject)" if [ "${subject/\[${SUBJECT}\]/}" == "${subject}" ]; then subject="[${SUBJECT}] ${subject## }" fi echo "${subject}" } tee >(reformail -c -x "Subject:" >subject) original | "${MIME_INJECT}" "${MAILGROUP}" | reformime -s1 -e > body reformail \ -X "Date:" \ -X "MIME-Version:" \ -X "Subject:" \ -X "Content-Type:" \ < original | reformail \ -U "MIME-Version:" \ -U "Content-Type:" | reformail \ -I "Subject: $(mk_subject)" \ -I "List-Id: $MAILGROUP" \ -I "List-Unsubscribe: " \ -I "List-Subscribe: " \ -I "List-Post: " \ -I "List-Unsubscribe-Post: List-Unsubscribe=One-Click" \ | cat - body > mail declare -A SUBSCRIBED declare -a actions update(){ alias="${actions[$1-1]}" email="${2}" SUBSCRIBED["$email"]="$alias" } mapfile -t -c 2 -C update actions <" echo "To: ${SUBSCRIBED["$email"]} <${email}>" "$SENDMAIL" -F "${FROM}" -f "${MAILGROUP}" "${SUBSCRIBED["$email"]} <${email}>" < mail DELIVERED=true done if ! $DELIVERED; then "$DEBUGMAIL" undelivered exit "$UNHANDLED" fi