aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/mail/comment.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mail/comment.sh')
-rwxr-xr-xbin/mail/comment.sh91
1 files changed, 60 insertions, 31 deletions
diff --git a/bin/mail/comment.sh b/bin/mail/comment.sh
index c29fdf4..299299a 100755
--- a/bin/mail/comment.sh
+++ b/bin/mail/comment.sh
@@ -2,39 +2,37 @@
2 2
3# Save incoming mail as comment 3# Save incoming mail as comment
4# Usage: 4# Usage:
5# echo '|<PATH_TO_THIS_SCRIPT>' >>~/.forward 5# echo '|<PATH_TO_THIS_SCRIPT> --output_dir=<PATH_OF_HTML_FILES>' >>~/.forward
6 6
7set -x 7# 1. Check mail is for comment {{{
8exec 2>>/dev/pts/1 8
9env >&2 9# Restore mail into variable
10MAIL="$(tr -d '\r')"
11header="$(<<<"$MAIL" sed '/^$/ q')"
12body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
13
14# determine mail is for comment by pattern
15pattern='^Subject: .*[cC]omment on page: (https?://)?([^/]+/)?([^ ]+)$'
16<<<"$header" grep -E "$pattern" >/dev/null || exit 0
17
18# }}}
19# 2. Get necessary variables from arguments {{{
10 20
11while [[ "$1" =~ ^-- && ! "$1" == "--" ]]; do 21while [[ "$1" =~ ^-- && ! "$1" == "--" ]]; do
12 case $1 in 22 case $1 in
13 --mailto ) shift; mailto=$1 ;;
14 --output_dir ) shift; output_dir=$1 ;; 23 --output_dir ) shift; output_dir=$1 ;;
24 --markdown_bin ) shift; markdown_bin=$1 ;;
25 *) shift ;;
15 esac 26 esac
16 shift 27 shift
17done 28done
18 29
19mailto=${mailto:-`whoami`@${HOSTNAME:?HOSTNAME is not specified}} 30output_dir=${output_dir:?}
20[ -z "$mailto" ] && echo --mailto is not specified} 31markdown_bin=${markdown_bin:-markdown}
21[ -z "$output_dir" ] && echo --output_dir is not specified}
22markdown_bin=${markdown:-markdown}
23[ -x $(which $markdown_bin) ] || markdown_bin=cat 32[ -x $(which $markdown_bin) ] || markdown_bin=cat
24 33
25# Check mail is for comment {{{
26
27# Restore mail into variable
28MAIL="$(tee /tmp/mail | tr -d '\r')"
29header="$(<<<"$MAIL" sed '/^$/ q')"
30body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"
31
32# determine mail is for comment by pattern
33pattern='^Subject: .*comment on (https?://)?([^/]+/)?([^ ]+)$'
34<<<"$header" grep -E "$pattern" >/dev/null || exit 0
35
36# }}} 34# }}}
37# Process header fields {{{ 35# 3. Read header fields {{{
38 36
39# enable execute last command in pipe under current shell 37# enable execute last command in pipe under current shell
40shopt -s lastpipe; set +m; 38shopt -s lastpipe; set +m;
@@ -48,7 +46,7 @@ done
48DATE=${DATE:+$(date --rfc-3339 seconds --date "$DATE")} 46DATE=${DATE:+$(date --rfc-3339 seconds --date "$DATE")}
49 47
50# }}} 48# }}}
51# Get path of output file {{{ 49# 4. Get path of output file {{{
52 50
53path=$(<<<"$header" sed -En "\\|${pattern}| {s//\\3/p; q}") 51path=$(<<<"$header" sed -En "\\|${pattern}| {s//\\3/p; q}")
54 52
@@ -65,7 +63,7 @@ path=${path/.html}
65output=$output_dir/${path}.comment.html 63output=$output_dir/${path}.comment.html
66 64
67# }}} 65# }}}
68# Get comment from mail body {{{ 66# 5. Get comment from mail body {{{
69 67
70# check mail includes multiple part 68# check mail includes multiple part
71if [[ "$CONTENT_TYPE" =~ mixed ]]; then 69if [[ "$CONTENT_TYPE" =~ mixed ]]; then
@@ -81,29 +79,60 @@ if [[ "$CONTENT_TYPE" =~ mixed ]]; then
81fi 79fi
82 80
83# }}} 81# }}}
84# Write comment to output file {{{ 82# 6. Write comment to output file {{{
85 83
86# add basic html layout for output file if necessary 84# add basic html layout for output file if necessary {{{
87if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then 85if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then
88 echo -e '<ul>\n</ul>' >$output 86 <<-LAYOUT cat >$output
87 <style>
88 .comment-body {
89 padding: 0.5rem;
90 width: fit-content;
91 border-radius: 8px;
92 background: lightblue;
93 p {
94 margin: 0.5rem;
95 }
96 }
97 .replies {
98 display: none;
99 cursor: pointer;
100 &:has(li) {
101 display: block;
102 }
103 }
104 </style>
105 <ul>
106 </ul>
107 LAYOUT
89fi 108fi
90# get line of insert position by header field "In-Reply-To" 109# }}}
110# get line of insert position by header field "In-Reply-To" {{{
91if [ -n "${IN_REPLY_TO}" ]; then 111if [ -n "${IN_REPLY_TO}" ]; then
92 line=$(grep -n "^<!-- ${IN_REPLY_TO} -->$" $output | cut -d':' -f1) 112 line=$(grep -n "^<!-- ${IN_REPLY_TO} -->$" $output | cut -d':' -f1)
93fi 113fi
94 114# }}}
95# insert comment into output file 115# insert comment into output file {{{
96<<-COMMENT sed -i "${line:-1}r /dev/stdin" $output 116<<-COMMENT sed -i "${line:-/<ul>/}r /dev/stdin" $output
97 <li> 117 <li>
118
98 <time datetime="${DATE}">${DATE}</time> 119 <time datetime="${DATE}">${DATE}</time>
99 <a href="mailto:${mailto}?subject=comment on ${path}&in-reply-to=${MESSAGE_ID}">[reply]</a> 120 <a href="mailto:${RECIPIENT}?subject=Comment on page: ${path}&in-reply-to=${MESSAGE_ID}">[reply]</a>
100 121
122 <div class='comment-body'>
101 $(<<<"${body}" ${markdown_bin}) 123 $(<<<"${body}" ${markdown_bin})
124 </div>
102 125
126 <details class="replies" open="true">
127 <summary>replies</summary>
103 <ul> 128 <ul>
104 <!-- ${MESSAGE_ID} --> 129 <!-- ${MESSAGE_ID} -->
105 </ul> 130 </ul>
131 </details>
132
106 </li> 133 </li>
107COMMENT 134COMMENT
108 135
109# }}} 136# }}}
137
138# }}}