aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xbin/mail/deliver.sh75
-rwxr-xr-xbin/mail/mbox.sh8
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
6exec 2>>~/Downloads/delivery.log
7
8shopt -s nocasematch
9
10# update index for dovecot
11trap 'doveadm force-resync ${mailbox:-/}' EXIT
12
13# Restore mail into variables
14MAIL="$(tr -d '\r')"
15header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')"
16body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
17
18# vars about output
19date=$(date --iso=seconds)
20maildir=${HOME}/Maildir
21mailbox=
22
23# Set set_stdout
24set_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
39print_mail() {
40 echo "$MAIL"
41}
42
43# save each field of header into vars
44# TODO Use GNU MailUtils to save header
45while IFS=: read field value; do
46 declare ${field//-/_}="${value# }"
47done <<<"$header"
48
49# save to mailbox
50if [[ "$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 MAIL
62 }
63elif [[ "$List_ID" =~ ^~rjarry/aerc-discuss ]]; then
64 mailbox=list/aerc
65elif [[ "$List_ID" =~ '<mutt-users.mutt.org>' ]]; then
66 mailbox=list/mutt
67elif [[ "$Subject" =~ 'login|verify|sign-in|密碼|安全性警示|登入' ]]; then
68 mailbox=login
69elif [[ "$To" = cloudflare@topo.tw ]]; then
70 mailbox=service
71elif [[ "$TO" = ithelp@topo.tw ]]; then
72 mailbox=promote
73fi
74
75set_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
3exec 1>>~/mbox
4
5echo
6echo From $SENDER
7env
8cat