blob: 00cec080da12fba564fee6a21105d35f69dee057 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#! /bin/bash
# Process each incoming mail
# TODO image/audio mail part
exec 2>>~/Downloads/delivery.log
shopt -s nocasematch
# update index for dovecot
trap 'doveadm force-resync ${mailbox:-/}' EXIT
# Restore mail into variables
MAIL="$(tr -d '\r')"
header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')"
body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
# vars about output
date=$(date --iso=seconds)
maildir=${HOME}/Maildir
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:]_]/}
mkdir -p $(dirname $path)
exec 1>$path
}
print_mail() {
echo "$MAIL"
}
# save each field of header into vars
# TODO Use GNU MailUtils to save header
while IFS=: read field value; do
declare ${field//-/_}="${value# }"
done <<<"$header"
# save to mailbox
if [[ "$SENDER" = pham@topo.tw && -n $Chat_Version ]]; then
mailbox=box
print_mail() {
<<-MAIL cat
From: me
Date: $(date --rfc-email)
Message-ID: ${Message_ID}
Subject: $(head -1 <<<"$body")
$(sed 1d <<<"$body")
MAIL
}
elif [[ "$List_ID" =~ ^~rjarry/aerc-discuss ]]; then
mailbox=list/aerc
elif [[ "$List_ID" =~ '<mutt-users.mutt.org>' ]]; then
mailbox=list/mutt
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
fi
set_stdout && print_mail
|