aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2025-01-27 16:40:33 +0800
committerHsieh Chin Fan <pham@topo.tw>2025-01-27 16:40:33 +0800
commit09e6f641ca98d19e5b17564ec2626fb9de088ee9 (patch)
treef551e3ae6034e7bec7eb78d512da6a3a53b4e173 /bin
parent4abe5ae5279243be8923d2975a247ec9b3ebcc55 (diff)
Update
Diffstat (limited to 'bin')
-rwxr-xr-xbin/mail/deliver.sh79
1 files changed, 45 insertions, 34 deletions
diff --git a/bin/mail/deliver.sh b/bin/mail/deliver.sh
index 4ed60ff..bd5bd82 100755
--- a/bin/mail/deliver.sh
+++ b/bin/mail/deliver.sh
@@ -1,9 +1,9 @@
1#! /bin/bash 1#! /bin/bash
2 2
3# Process each incoming mail 3# Deliver incoming mail to proper mailbox
4# TODO image/audio mail part 4# TODO image/audio mail part
5 5
6# message for logging delivery 6# log each delivery {{{
7log=$(grep -rlE 'From:\s+<?MDA' ~/Maildir/cur | head -1) 7log=$(grep -rlE 'From:\s+<?MDA' ~/Maildir/cur | head -1)
8if [ -z $log ]; then 8if [ -z $log ]; then
9 log=~/Maildir/cur/deliver.log 9 log=~/Maildir/cur/deliver.log
@@ -14,7 +14,8 @@ if [ -z $log ]; then
14 14
15 HEADER 15 HEADER
16fi 16fi
17 17# }}}
18# shell opt/trap {{{
18exec 2>>$log 19exec 2>>$log
19shopt -s nocasematch extglob 20shopt -s nocasematch extglob
20 21
@@ -25,8 +26,8 @@ trap 'doveadm force-resync ${mailbox:-/}' EXIT
25tmp_mailbox=$(mktemp -d); mkdir -p ${tmp_mailbox}/{tmp,new,cur} 26tmp_mailbox=$(mktemp -d); mkdir -p ${tmp_mailbox}/{tmp,new,cur}
26cat >${tmp_mailbox}/cur/mail 27cat >${tmp_mailbox}/cur/mail
27trap 'rm -rf ${tmp_mailbox}' EXIT 28trap 'rm -rf ${tmp_mailbox}' EXIT
28 29# }}}
29# Restore mail into vars 30# vars about message {{{
30MAIL="$(decodemail ${tmp_mailbox})" 31MAIL="$(decodemail ${tmp_mailbox})"
31# TODO process multi-line header field 32# TODO process multi-line header field
32header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')" 33header="$(<<<"$MAIL" sed '/^$/ q; /^[[:blank:]]/ d;')"
@@ -36,8 +37,8 @@ body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
36date=$(date --iso=seconds) 37date=$(date --iso=seconds)
37maildir=${HOME}/Maildir 38maildir=${HOME}/Maildir
38mailbox= 39mailbox=
39 40# }}}
40# Set set_stdout 41# FUNCTION: Set set_stdout {{{
41set_stdout() { 42set_stdout() {
42 filename=${Subject// /_} 43 filename=${Subject// /_}
43 path=${maildir}/${mailbox}${mailbox:+/}new/${date//:/}.${filename//[^[:alnum:]_]/} 44 path=${maildir}/${mailbox}${mailbox:+/}new/${date//:/}.${filename//[^[:alnum:]_]/}
@@ -45,12 +46,40 @@ set_stdout() {
45 46
46 exec 1>$path 47 exec 1>$path
47} 48}
48 49# }}}
50# FUNCTION: print mail {{{
49print_mail() { 51print_mail() {
50 echo "$MAIL" 52 if [ "$private" = true ]; then
53 <<-MAIL cat
54 From: me
55 Date: $(date --rfc-email)
56 Message-ID: ${Message_ID}
57 Self: true
58 Subject: ${heading}
59
60 $(sed 1d <<<"$body")
61 MAIL
62 else
63 echo "$MAIL"
64 fi
51} 65}
66# }}}
67# FUNCTION: save as private message {{{
68private_message() {
69 heading="$(head -1 <<<"${body}")"
70
71 if [[ "${heading}" =~ ^"." ]]; then
72 mailbox=act
73 heading=${heading#.}
74 else
75 mailbox=box
76 fi
52 77
53# save each field of header into vars 78 private=true
79}
80# }}}
81
82# save each header field into vars {{{
54# TODO Use GNU MailUtils to save header 83# TODO Use GNU MailUtils to save header
55while read line; do 84while read line; do
56 [[ "${line}" =~ ^" "|^" " ]] && ${field}+=" ${line##*( )}" && continue 85 [[ "${line}" =~ ^" "|^" " ]] && ${field}+=" ${line##*( )}" && continue
@@ -60,31 +89,12 @@ while read line; do
60 field="${field//-/_}" 89 field="${field//-/_}"
61 declare ${field}="${value}" 90 declare ${field}="${value}"
62done <<<"$header" 91done <<<"$header"
63 92# }}}
64# save to mailbox 93# decide mailbox by vars {{{
65if [[ "$SENDER" = pham@topo.tw && -n $CHAT_VERSION ]]; then 94if [[ "$SENDER" = pham@topo.tw && -n $CHAT_VERSION ]]; then
66 heading="$(head -1 <<<"${body}")" 95 private_message
67
68 if [[ "${heading}" =~ ^"." ]]; then
69 mailbox=act
70 heading=${heading#.}
71 else
72 mailbox=box
73 fi
74
75 print_mail() {
76 <<-MAIL cat
77 From: me
78 Date: $(date --rfc-email)
79 Message-ID: ${Message_ID}
80 Self: true
81 Subject: ${heading}
82
83 $(sed 1d <<<"$body")
84 MAIL
85 }
86elif [[ "${TO}" =~ '+'|'=' ]]; then 96elif [[ "${TO}" =~ '+'|'=' ]]; then
87 mailbox=${TO#*[+=]} # remove chars before symbol of mailbox 97 mailbox=${TO#*[+=]} # remove chars before symbol of mailbox
88 mailbox=${mailbox%@*} # remove suffix for mail address 98 mailbox=${mailbox%@*} # remove suffix for mail address
89elif [[ "${FROM}${RETURN_PATH}" =~ notifications@github.com|noreply@github.com ]]; then 99elif [[ "${FROM}${RETURN_PATH}" =~ notifications@github.com|noreply@github.com ]]; then
90 mailbox=DEV/github 100 mailbox=DEV/github
@@ -111,7 +121,7 @@ elif [[ "${SUBJECT}" =~ login|verify|sign-in|密碼|安全性警示|登入|存
111 mailbox=login 121 mailbox=login
112elif [[ "${TO}" = cloudflare@topo.tw ]]; then 122elif [[ "${TO}" = cloudflare@topo.tw ]]; then
113 mailbox=SRV/cloudflare 123 mailbox=SRV/cloudflare
114elif [[ 124elif [[
115 "${SUBJECT}" =~ 未讀|更新|核對表|嘟文|unread|summary|introduc || 125 "${SUBJECT}" =~ 未讀|更新|核對表|嘟文|unread|summary|introduc ||
116 "${FROM}" =~ no-reply@hackmd.io \ 126 "${FROM}" =~ no-reply@hackmd.io \
117 ]]; then 127 ]]; then
@@ -123,6 +133,7 @@ elif [[
123 ]]; then 133 ]]; then
124 mailbox=MISC/promote 134 mailbox=MISC/promote
125fi 135fi
136# }}}
126 137
127# deliver mail to mailbox 138# deliver mail to mailbox
128set_stdout && print_mail 139set_stdout && print_mail