diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2025-01-07 10:17:48 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2025-01-07 10:17:53 +0800 |
| commit | 67bb7d0708a4d7fa0b8a1734ec41c6f289103575 (patch) | |
| tree | 77bc54278ff46909b4ec9eada6d47df42f7b92ff | |
| parent | a164a0242bf9dca1a4c5d682a67332f7bbf2a9d6 (diff) | |
update deliver.sh
| -rwxr-xr-x | bin/mail/deliver.sh | 82 |
1 files 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 @@ | |||
| 3 | # Process each incoming mail | 3 | # Process each incoming mail |
| 4 | # TODO image/audio mail part | 4 | # TODO image/audio mail part |
| 5 | 5 | ||
| 6 | exec 2>>~/Downloads/delivery.log | 6 | # message for logging delivery |
| 7 | log=$(grep -rlE 'From:\s+<?MDA' ~/Maildir/cur | head -1) | ||
| 8 | if [ -z $log ]; then | ||
| 9 | log=~/Maildir/cur/deliver.log | ||
| 10 | <<-HEADER cat >$log | ||
| 11 | From: MDA | ||
| 12 | Content-Type: text/plain; charset=UTF-8 | ||
| 13 | Subject: Delivery Log | ||
| 14 | |||
| 15 | HEADER | ||
| 16 | fi | ||
| 7 | 17 | ||
| 18 | exec 2>>$log | ||
| 8 | shopt -s nocasematch | 19 | shopt -s nocasematch |
| 9 | 20 | ||
| 10 | # update index for dovecot | 21 | # update index for dovecot |
| 11 | trap 'doveadm force-resync ${mailbox:-/}' EXIT | 22 | trap 'doveadm force-resync ${mailbox:-/}' EXIT |
| 12 | 23 | ||
| 24 | # temp file for decodemail (GNU Mailutils) | ||
| 25 | tmp_mailbox=$(mktemp -d); mkdir -p ${tmp_mailbox}/{tmp,new,cur} | ||
| 26 | cat >${tmp_mailbox}/cur/mail | ||
| 27 | trap 'rm -rf ${tmp_mailbox}' EXIT | ||
| 28 | |||
| 13 | # Restore mail into variables | 29 | # Restore mail into variables |
| 14 | MAIL="$(tr -d '\r')" | 30 | MAIL="$(decodemail ${tmp_mailbox})" |
| 31 | # TODO process multi-line header field | ||
| 15 | header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')" | 32 | header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')" |
| 16 | body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" | 33 | body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" |
| 17 | 34 | ||
| @@ -22,15 +39,8 @@ mailbox= | |||
| 22 | 39 | ||
| 23 | # Set set_stdout | 40 | # Set set_stdout |
| 24 | set_stdout() { | 41 | set_stdout() { |
| 25 | name=${Subject// /_} | 42 | filename=${Subject// /_} |
| 26 | 43 | path=${maildir}/${mailbox}${mailbox:+/}new/${date//:/}.${filename//[^[:alnum:]_]/} | |
| 27 | # process encoded string (RFC2047) | ||
| 28 | if [[ $name =~ ^=?utf-8?B? ]]; then | ||
| 29 | shopt -s lastpipe; set +m | ||
| 30 | echo "$name" | cut -d '?' -f4 | base64 -d | read name | ||
| 31 | fi | ||
| 32 | |||
| 33 | path=${maildir}/${mailbox}${mailbox:+/}new/${date//:/}.${name//[^[:alnum:]_]/} | ||
| 34 | mkdir -p $(dirname $path) | 44 | mkdir -p $(dirname $path) |
| 35 | 45 | ||
| 36 | exec 1>$path | 46 | exec 1>$path |
| @@ -42,34 +52,58 @@ print_mail() { | |||
| 42 | 52 | ||
| 43 | # save each field of header into vars | 53 | # save each field of header into vars |
| 44 | # TODO Use GNU MailUtils to save header | 54 | # TODO Use GNU MailUtils to save header |
| 45 | while IFS=: read field value; do | 55 | while IFS=': ' read field value; do |
| 46 | declare ${field//-/_}="${value# }" | 56 | field="${field^^}" |
| 57 | field="${field//-/_}" | ||
| 58 | declare ${field}="${value}" | ||
| 47 | done <<<"$header" | 59 | done <<<"$header" |
| 48 | 60 | ||
| 49 | # save to mailbox | 61 | # save to mailbox |
| 50 | if [[ "$SENDER" = pham@topo.tw && -n $Chat_Version ]]; then | 62 | if [[ "$SENDER" = pham@topo.tw && -n $Chat_Version ]]; then |
| 51 | mailbox=box | 63 | heading="$(head -1 <<<"${body}")" |
| 64 | |||
| 65 | if [[ "${heading}" =~ ^"." ]]; then | ||
| 66 | mailbox=do | ||
| 67 | heading=${heading#.} | ||
| 68 | else | ||
| 69 | mailbox=box | ||
| 70 | fi | ||
| 52 | 71 | ||
| 53 | print_mail() { | 72 | print_mail() { |
| 54 | <<-MAIL cat | 73 | <<-MAIL cat |
| 55 | From: me | 74 | From: me |
| 56 | Date: $(date --rfc-email) | 75 | Date: $(date --rfc-email) |
| 57 | Message-ID: ${Message_ID} | 76 | Message-ID: ${Message_ID} |
| 58 | Subject: $(head -1 <<<"$body") | 77 | Self: true |
| 78 | Subject: ${heading} | ||
| 59 | 79 | ||
| 60 | $(sed 1d <<<"$body") | 80 | $(sed 1d <<<"$body") |
| 61 | 81 | ||
| 62 | } | 82 | } |
| 63 | elif [[ "$List_ID" =~ ^~rjarry/aerc-discuss ]]; then | 83 | elif [[ "$SUBJECT" =~ 帳單|轉帳|對帳|付款|發票|消費|繳費|收據|費用|Invoice|Billing ]]; then |
| 64 | mailbox=list/aerc | 84 | mailbox=pay |
| 65 | elif [[ "$List_ID" =~ '<mutt-users.mutt.org>' ]]; then | 85 | elif [[ "$TO" = dmarc@topo.tw ]]; then |
| 66 | mailbox=list/mutt | 86 | mailbox=DEV/dmarc |
| 67 | elif [[ "$Subject" =~ 'login|verify|sign-in|密碼|安全性警示|登入' ]]; then | 87 | elif [[ "$LIST_ID" =~ ^'Open Street Map Taiwan' ]]; then |
| 88 | mailbox=FOSS/osm | ||
| 89 | elif [[ "$LIST_ID" =~ ^~rjarry/aerc-discuss ]]; then | ||
| 90 | mailbox=LIST/aerc | ||
| 91 | elif [[ "$LIST_ID" =~ '<mutt-users.mutt.org>' ]]; then | ||
| 92 | mailbox=LIST/mutt | ||
| 93 | elif [[ -n "${LIST_ID}" ]]; then | ||
| 94 | mailbox=news | ||
| 95 | elif [[ "$SUBJECT" =~ login|verify|sign-in|密碼|安全性警示|登入 ]]; then | ||
| 68 | mailbox=login | 96 | mailbox=login |
| 69 | elif [[ "$To" = cloudflare@topo.tw ]]; then | 97 | elif [[ "$SUBJECT" =~ 電子報|newsletter ]]; then |
| 70 | mailbox=service | 98 | mailbox=news |
| 71 | elif [[ "$TO" = ithelp@topo.tw ]]; then | 99 | elif [[ "$TO" = cloudflare@topo.tw ]]; then |
| 72 | mailbox=promote | 100 | mailbox=SRV/cloudflare |
| 101 | elif [[ "$SUBJECT" =~ eDM ]]; then | ||
| 102 | mailbox=MISC/promote | ||
| 73 | fi | 103 | fi |
| 74 | 104 | ||
| 105 | # deliver mail to mailbox | ||
| 75 | set_stdout && print_mail | 106 | set_stdout && print_mail |
| 107 | |||
| 108 | # log to stderr | ||
| 109 | echo -e ${date} ${mailbox:-INBOX} '\t' ${heading:-${SUBJECT}} >&2 | ||