diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2024-12-07 19:29:03 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-12-07 19:29:03 +0800 |
commit | 8e21c0933c70f8c27fb9620bf55d01b2ecc77e73 (patch) | |
tree | bca4bab7205672a1b1f2b4a8f92015614debb6cd /bin | |
parent | 76e609e5dbb88bfcecd72afc83dc2a519493049d (diff) |
Update comment.sh
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/mail/comment.sh | 24 |
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')" | |||
36 | body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" | 34 | body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" |
37 | 35 | ||
38 | # determine mail is for comment by pattern | 36 | # determine mail is for comment by pattern |
39 | pattern='^Subject: .*[cC]omment on page: (https?://)?([^/]+/)?([^ ]+)$' | 37 | pattern='^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 | ||
77 | path=$(<<<"$header" sed -En "\\|${pattern}| {s//\\3/p; q}") | 75 | path=$(<<<"$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 |
80 | if [ $path = "" ]; then | 78 | if [ $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 | ||
93 | printTextPart() { | ||
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 |
96 | boundary="$(<<<"$CONTENT_TYPE" sed -En 's/^.*boundary="?([^"]+)"?.*$/\1/p')" | 101 | boundary="$(<<<"$CONTENT_TYPE" sed -En 's/^.*boundary="?([^"]+)"?.*$/\1/p')" |
97 | if [ -n "${boundary}" ]; then | 102 | if [ -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")" | ||
101 | fi | 105 | fi |
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 | ||
106 | umask 133 | 111 | umask 133 |
112 | |||
107 | # add basic html layout for output file if necessary {{{ | 113 | # add basic html layout for output file if necessary {{{ |
108 | if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then | 114 | if [ ! -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> |
171 | COMMENT | 177 | COMMENT |