aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/mail
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mail')
-rwxr-xr-xbin/mail/deliver.sh82
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
6exec 2>>~/Downloads/delivery.log 6# message for logging delivery
7log=$(grep -rlE 'From:\s+<?MDA' ~/Maildir/cur | head -1)
8if [ -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
16fi
7 17
18exec 2>>$log
8shopt -s nocasematch 19shopt -s nocasematch
9 20
10# update index for dovecot 21# update index for dovecot
11trap 'doveadm force-resync ${mailbox:-/}' EXIT 22trap 'doveadm force-resync ${mailbox:-/}' EXIT
12 23
24# temp file for decodemail (GNU Mailutils)
25tmp_mailbox=$(mktemp -d); mkdir -p ${tmp_mailbox}/{tmp,new,cur}
26cat >${tmp_mailbox}/cur/mail
27trap 'rm -rf ${tmp_mailbox}' EXIT
28
13# Restore mail into variables 29# Restore mail into variables
14MAIL="$(tr -d '\r')" 30MAIL="$(decodemail ${tmp_mailbox})"
31# TODO process multi-line header field
15header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')" 32header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')"
16body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" 33body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
17 34
@@ -22,15 +39,8 @@ mailbox=
22 39
23# Set set_stdout 40# Set set_stdout
24set_stdout() { 41set_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
45while IFS=: read field value; do 55while IFS=': ' read field value; do
46 declare ${field//-/_}="${value# }" 56 field="${field^^}"
57 field="${field//-/_}"
58 declare ${field}="${value}"
47done <<<"$header" 59done <<<"$header"
48 60
49# save to mailbox 61# save to mailbox
50if [[ "$SENDER" = pham@topo.tw && -n $Chat_Version ]]; then 62if [[ "$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 MAIL 81 MAIL
62 } 82 }
63elif [[ "$List_ID" =~ ^~rjarry/aerc-discuss ]]; then 83elif [[ "$SUBJECT" =~ 帳單|轉帳|對帳|付款|發票|消費|繳費|收據|費用|Invoice|Billing ]]; then
64 mailbox=list/aerc 84 mailbox=pay
65elif [[ "$List_ID" =~ '<mutt-users.mutt.org>' ]]; then 85elif [[ "$TO" = dmarc@topo.tw ]]; then
66 mailbox=list/mutt 86 mailbox=DEV/dmarc
67elif [[ "$Subject" =~ 'login|verify|sign-in|密碼|安全性警示|登入' ]]; then 87elif [[ "$LIST_ID" =~ ^'Open Street Map Taiwan' ]]; then
88 mailbox=FOSS/osm
89elif [[ "$LIST_ID" =~ ^~rjarry/aerc-discuss ]]; then
90 mailbox=LIST/aerc
91elif [[ "$LIST_ID" =~ '<mutt-users.mutt.org>' ]]; then
92 mailbox=LIST/mutt
93elif [[ -n "${LIST_ID}" ]]; then
94 mailbox=news
95elif [[ "$SUBJECT" =~ login|verify|sign-in|密碼|安全性警示|登入 ]]; then
68 mailbox=login 96 mailbox=login
69elif [[ "$To" = cloudflare@topo.tw ]]; then 97elif [[ "$SUBJECT" =~ 電子報|newsletter ]]; then
70 mailbox=service 98 mailbox=news
71elif [[ "$TO" = ithelp@topo.tw ]]; then 99elif [[ "$TO" = cloudflare@topo.tw ]]; then
72 mailbox=promote 100 mailbox=SRV/cloudflare
101elif [[ "$SUBJECT" =~ eDM ]]; then
102 mailbox=MISC/promote
73fi 103fi
74 104
105# deliver mail to mailbox
75set_stdout && print_mail 106set_stdout && print_mail
107
108# log to stderr
109echo -e ${date} ${mailbox:-INBOX} '\t' ${heading:-${SUBJECT}} >&2