diff options
Diffstat (limited to 'bin/mail')
| -rwxr-xr-x | bin/mail/deliver.sh | 75 | ||||
| -rwxr-xr-x | bin/mail/mbox.sh | 8 |
2 files changed, 83 insertions, 0 deletions
diff --git a/bin/mail/deliver.sh b/bin/mail/deliver.sh new file mode 100755 index 0000000..00cec08 --- /dev/null +++ b/bin/mail/deliver.sh | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | # Process each incoming mail | ||
| 4 | # TODO image/audio mail part | ||
| 5 | |||
| 6 | exec 2>>~/Downloads/delivery.log | ||
| 7 | |||
| 8 | shopt -s nocasematch | ||
| 9 | |||
| 10 | # update index for dovecot | ||
| 11 | trap 'doveadm force-resync ${mailbox:-/}' EXIT | ||
| 12 | |||
| 13 | # Restore mail into variables | ||
| 14 | MAIL="$(tr -d '\r')" | ||
| 15 | header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')" | ||
| 16 | body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" | ||
| 17 | |||
| 18 | # vars about output | ||
| 19 | date=$(date --iso=seconds) | ||
| 20 | maildir=${HOME}/Maildir | ||
| 21 | mailbox= | ||
| 22 | |||
| 23 | # Set set_stdout | ||
| 24 | set_stdout() { | ||
| 25 | name=${Subject// /_} | ||
| 26 | |||
| 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) | ||
| 35 | |||
| 36 | exec 1>$path | ||
| 37 | } | ||
| 38 | |||
| 39 | print_mail() { | ||
| 40 | echo "$MAIL" | ||
| 41 | } | ||
| 42 | |||
| 43 | # save each field of header into vars | ||
| 44 | # TODO Use GNU MailUtils to save header | ||
| 45 | while IFS=: read field value; do | ||
| 46 | declare ${field//-/_}="${value# }" | ||
| 47 | done <<<"$header" | ||
| 48 | |||
| 49 | # save to mailbox | ||
| 50 | if [[ "$SENDER" = pham@topo.tw && -n $Chat_Version ]]; then | ||
| 51 | mailbox=box | ||
| 52 | |||
| 53 | print_mail() { | ||
| 54 | <<-MAIL cat | ||
| 55 | From: me | ||
| 56 | Date: $(date --rfc-email) | ||
| 57 | Message-ID: ${Message_ID} | ||
| 58 | Subject: $(head -1 <<<"$body") | ||
| 59 | |||
| 60 | $(sed 1d <<<"$body") | ||
| 61 | |||
| 62 | } | ||
| 63 | elif [[ "$List_ID" =~ ^~rjarry/aerc-discuss ]]; then | ||
| 64 | mailbox=list/aerc | ||
| 65 | elif [[ "$List_ID" =~ '<mutt-users.mutt.org>' ]]; then | ||
| 66 | mailbox=list/mutt | ||
| 67 | elif [[ "$Subject" =~ 'login|verify|sign-in|密碼|安全性警示|登入' ]]; then | ||
| 68 | mailbox=login | ||
| 69 | elif [[ "$To" = cloudflare@topo.tw ]]; then | ||
| 70 | mailbox=service | ||
| 71 | elif [[ "$TO" = ithelp@topo.tw ]]; then | ||
| 72 | mailbox=promote | ||
| 73 | fi | ||
| 74 | |||
| 75 | set_stdout && print_mail | ||
diff --git a/bin/mail/mbox.sh b/bin/mail/mbox.sh new file mode 100755 index 0000000..e3c966b --- /dev/null +++ b/bin/mail/mbox.sh | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | exec 1>>~/mbox | ||
| 4 | |||
| 5 | echo | ||
| 6 | echo From $SENDER | ||
| 7 | env | ||
| 8 | cat | ||