aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/mail/comment.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mail/comment.sh')
-rwxr-xr-xbin/mail/comment.sh41
1 files changed, 41 insertions, 0 deletions
diff --git a/bin/mail/comment.sh b/bin/mail/comment.sh
new file mode 100755
index 0000000..807bcb2
--- /dev/null
+++ b/bin/mail/comment.sh
@@ -0,0 +1,41 @@
1#! /bin/bash
2
3output_dir=${output_dir:-/srv/http}
4
5# Restore mail into variable
6MAIL="$(tr -d '\r')"
7headers="$(<<<"$MAIL" sed '/^$/ q')"
8contents="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
9
10# enable execute last command in pipe under current shell
11shopt -s lastpipe; set +m;
12
13# get route (target page of comment) and id
14<<<"$MAIL" sed -En '/^To: comment+/ {s/^To: comment\+([^+]+)\+?(.*)@.*$/\1 \2/p; q}; /^$/q' \
15| read route id
16
17# sender want comment on some page, but find no route for this
18if [ $route = "" ]; then
19 echo 'rcpt "comment+<ROUTE>" not matched' >&2
20 exit 1
21fi
22
23output=$output_dir/${route#/}.comment
24exec 1>>$output
25
26# check mail includes multiple part
27<<<"$headers" grep '^Content-Type:.*mixed' >/dev/null
28if [ $? -eq 0 ]; then
29 boundary="$(<<<"$headers" sed -En 's/^Content-Type:.*boundary="(.*)".*$/\1/p')"
30 if [ $boundary = "" ]; then
31 echo 'cannot get boundary from mail header' >&2
32 exit 1
33 fi
34
35 # print content of first mail part
36 pattern="\\@^--${boundary}\$@"
37 <<<"$contents" sed -n "${pattern},${pattern} p" | sed -n "1,4d; ${pattern} q; p"
38else
39 # print content
40 <<<"$contents" sed '/^$/,$ p'
41fi