From 67bb7d0708a4d7fa0b8a1734ec41c6f289103575 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 7 Jan 2025 10:17:48 +0800 Subject: update deliver.sh --- bin/mail/deliver.sh | 82 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/bin/mail/deliver.sh b/bin/mail/deliver.sh index 00cec08..134e4fd 100755 --- a/bin/mail/deliver.sh +++ b/bin/mail/deliver.sh @@ -3,15 +3,32 @@ # Process each incoming mail # TODO image/audio mail part -exec 2>>~/Downloads/delivery.log +# message for logging delivery +log=$(grep -rlE 'From:\s+$log + From: MDA + Content-Type: text/plain; charset=UTF-8 + Subject: Delivery Log + + HEADER +fi +exec 2>>$log shopt -s nocasematch # update index for dovecot trap 'doveadm force-resync ${mailbox:-/}' EXIT +# temp file for decodemail (GNU Mailutils) +tmp_mailbox=$(mktemp -d); mkdir -p ${tmp_mailbox}/{tmp,new,cur} +cat >${tmp_mailbox}/cur/mail +trap 'rm -rf ${tmp_mailbox}' EXIT + # Restore mail into variables -MAIL="$(tr -d '\r')" +MAIL="$(decodemail ${tmp_mailbox})" +# TODO process multi-line header field header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')" body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" @@ -22,15 +39,8 @@ mailbox= # Set set_stdout set_stdout() { - name=${Subject// /_} - - # process encoded string (RFC2047) - if [[ $name =~ ^=?utf-8?B? ]]; then - shopt -s lastpipe; set +m - echo "$name" | cut -d '?' -f4 | base64 -d | read name - fi - - path=${maildir}/${mailbox}${mailbox:+/}new/${date//:/}.${name//[^[:alnum:]_]/} + filename=${Subject// /_} + path=${maildir}/${mailbox}${mailbox:+/}new/${date//:/}.${filename//[^[:alnum:]_]/} mkdir -p $(dirname $path) exec 1>$path @@ -42,34 +52,58 @@ print_mail() { # save each field of header into vars # TODO Use GNU MailUtils to save header -while IFS=: read field value; do - declare ${field//-/_}="${value# }" +while IFS=': ' read field value; do + field="${field^^}" + field="${field//-/_}" + declare ${field}="${value}" done <<<"$header" # save to mailbox if [[ "$SENDER" = pham@topo.tw && -n $Chat_Version ]]; then - mailbox=box + heading="$(head -1 <<<"${body}")" + + if [[ "${heading}" =~ ^"." ]]; then + mailbox=do + heading=${heading#.} + else + mailbox=box + fi print_mail() { <<-MAIL cat From: me Date: $(date --rfc-email) Message-ID: ${Message_ID} - Subject: $(head -1 <<<"$body") + Self: true + Subject: ${heading} $(sed 1d <<<"$body") MAIL } -elif [[ "$List_ID" =~ ^~rjarry/aerc-discuss ]]; then - mailbox=list/aerc -elif [[ "$List_ID" =~ '' ]]; then - mailbox=list/mutt -elif [[ "$Subject" =~ 'login|verify|sign-in|密碼|安全性警示|登入' ]]; then +elif [[ "$SUBJECT" =~ 帳單|轉帳|對帳|付款|發票|消費|繳費|收據|費用|Invoice|Billing ]]; then + mailbox=pay +elif [[ "$TO" = dmarc@topo.tw ]]; then + mailbox=DEV/dmarc +elif [[ "$LIST_ID" =~ ^'Open Street Map Taiwan' ]]; then + mailbox=FOSS/osm +elif [[ "$LIST_ID" =~ ^~rjarry/aerc-discuss ]]; then + mailbox=LIST/aerc +elif [[ "$LIST_ID" =~ '' ]]; then + mailbox=LIST/mutt +elif [[ -n "${LIST_ID}" ]]; then + mailbox=news +elif [[ "$SUBJECT" =~ login|verify|sign-in|密碼|安全性警示|登入 ]]; then mailbox=login -elif [[ "$To" = cloudflare@topo.tw ]]; then - mailbox=service -elif [[ "$TO" = ithelp@topo.tw ]]; then - mailbox=promote +elif [[ "$SUBJECT" =~ 電子報|newsletter ]]; then + mailbox=news +elif [[ "$TO" = cloudflare@topo.tw ]]; then + mailbox=SRV/cloudflare +elif [[ "$SUBJECT" =~ eDM ]]; then + mailbox=MISC/promote fi +# deliver mail to mailbox set_stdout && print_mail + +# log to stderr +echo -e ${date} ${mailbox:-INBOX} '\t' ${heading:-${SUBJECT}} >&2 -- cgit v1.2.3-70-g09d2