aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/mail
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mail')
-rwxr-xr-xbin/mail/comment.sh24
1 files changed, 15 insertions, 9 deletions
diff --git a/bin/mail/comment.sh b/bin/mail/comment.sh
index 842e4b0..8d36e32 100755
--- a/bin/mail/comment.sh
+++ b/bin/mail/comment.sh
@@ -3,10 +3,8 @@
3# Save incoming mail as comment 3# Save incoming mail as comment
4# Usage: 4# Usage:
5# Step1. Add script into forward(5) for MDA 5# Step1. Add script into forward(5) for MDA
6# Some MDA won't accept arguments for command. If --output_dir is not specified,
7# directory of this script would be used instead
8# 6#
9# echo '|<PATH_TO_THIS_SCRIPT> --output_dir <PATH_OF_HTML_FILES>' >>~/.forward 7# echo '|"<PATH_TO_THIS_SCRIPT> --output_dir <PATH_OF_HTML_FILES>"' >>~/.forward
10# 8#
11# Step2. Insert related html file into target page, the following example use <object> element 9# Step2. Insert related html file into target page, the following example use <object> element
12# * Please replace <PATH> for your target page, relative to value of --output_dir 10# * Please replace <PATH> for your target page, relative to value of --output_dir
@@ -36,7 +34,7 @@ header="$(<<<"$MAIL" sed '/^$/ q; :a; N; s/\n\s\+//; ta')"
36body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" 34body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
37 35
38# determine mail is for comment by pattern 36# determine mail is for comment by pattern
39pattern='^Subject: .*[cC]omment on page: (https?://)?([^/]+/)?([^ ]+)$' 37pattern='^Subject: .*[cC]omment on page:? +(https?://[^/]+)?(/?[^ ]+).*$'
40<<<"$header" grep -E "$pattern" >/dev/null || exit 0 38<<<"$header" grep -E "$pattern" >/dev/null || exit 0
41 39
42# }}} 40# }}}
@@ -74,7 +72,7 @@ DATE=${DATE:+$(date --rfc-3339 seconds --date "$DATE")}
74# }}} 72# }}}
75# 4. Get path of output file {{{ 73# 4. Get path of output file {{{
76 74
77path=$(<<<"$header" sed -En "\\|${pattern}| {s//\\3/p; q}") 75path=$(<<<"$header" sed -En "\\|${pattern}| {s//\\2/p; q}")
78 76
79# sender want comment on some page, but find no path for this 77# sender want comment on some page, but find no path for this
80if [ $path = "" ]; then 78if [ $path = "" ]; then
@@ -92,18 +90,26 @@ umask 022; mkdir -p $(dirname $output)
92# }}} 90# }}}
93# 5. Get comment from mail body {{{ 91# 5. Get comment from mail body {{{
94 92
93printTextPart() {
94 boundary=$1
95 beginPat="\\|^--${boundary}\$|"
96 endPat="\\|^--${boundary}(--)?\$|"
97 sed -En "${beginPat},${endPat} { \@^Content-Type: text/plain@,$ {1,/^$/d; ${beginPat}d; p} }"
98}
99
95# check mail includes multiple part 100# check mail includes multiple part
96boundary="$(<<<"$CONTENT_TYPE" sed -En 's/^.*boundary="?([^"]+)"?.*$/\1/p')" 101boundary="$(<<<"$CONTENT_TYPE" sed -En 's/^.*boundary="?([^"]+)"?.*$/\1/p')"
97if [ -n "${boundary}" ]; then 102if [ -n "${boundary}" ]; then
98 # print content of first mail part 103 # print mail part in MIME: text/plain
99 boundaryPat="\\|^--${boundary}\$|" 104 body="$(<<<"$body" printTextPart ${boundary})"
100 body="$(<<<"$body" sed -n "${boundaryPat},${boundaryPat} p" | sed -n "1,4d; ${boundaryPat} q; p")"
101fi 105fi
102 106
103# }}} 107# }}}
104# 6. Write comment to output file {{{ 108# 6. Write comment to output file {{{
105 109
110# set STDOUT to $output, with readable permission for everyone
106umask 133 111umask 133
112
107# add basic html layout for output file if necessary {{{ 113# add basic html layout for output file if necessary {{{
108if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then 114if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then
109 <<-LAYOUT cat >$output 115 <<-LAYOUT cat >$output
@@ -165,7 +171,7 @@ fi
165 <ul> 171 <ul>
166 <!-- ${MESSAGE_ID} --> 172 <!-- ${MESSAGE_ID} -->
167 </ul> 173 </ul>
168 </details> 174 </details>
169 175
170 </li> 176 </li>
171COMMENT 177COMMENT