aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/mail/comment.sh
blob: d363886adf513426b37a7c2628bc34f6a87c44c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#! /bin/bash

# Save incoming mail as comment
# Usage:
#   Step1. Add script into forward(5) for MDA
#
#     echo '|"<PATH_TO_THIS_SCRIPT> --output_dir <PATH_OF_HTML_FILES>"' >>~/.forward
#
#   Step2. Insert related html file into target page, the following example use <object> element
#          * Please replace <PATH> for your target page, relative to value of --output_dir
#          * Set <RECIPIENT> for mail address which can achive MDA
#          * Change <PATH> in data attribute of <object> based on routing if necessary
#
#     <!-- START OF COMMENT BLOCK -->
#     <div style="border-radius: 6px; background: lightyellow">
#       <a style="display: inline-block; margin: 0.5em 0.5em 0 0; float: right" href="mailto:<RECIPIENT>?subject=Comment on page: <PATH>">[Comment on this page]</a>
#       <object type="text/html" data="<PATH>.comment.html" onload="observeResize(this)" style="width: 100%;"></object>
#       <script>
#         function observeResize(commentBlock) {
#           new ResizeObserver((entries) => {
#             commentBlock.style.height = entries[0].target.clientHeight + 'px';
#           }).observe(commentBlock.contentDocument.documentElement);
#         }
#       </script>
#     </div>
#     <!-- END OF COMMENT BLOCK -->

# 0. Work as CGI {{{

# Print this script as HTTP Response
if [ -n "$REQUEST_METHOD" ]; then
  <<-RESPONSE cat
	Status: 200
	Content-Type: text/plain

	$(cat $0)
	RESPONSE
  exit 0
fi

# }}}
# 1. Check mail is for comment {{{

# Restore mail into variables
MAIL="$(tr -d '\r')"
# join multi-line field value into one line
header="$(<<<"$MAIL" sed '/^$/ q; :a; N; s/\n\s\+//; ta')"
body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')"

# determine mail is for comment by pattern
pattern='^Subject: .*[cC]omment on page:? +(https?://[^/]+)?(/?[^ ]+).*$'
<<<"$header" grep -E "$pattern" >/dev/null || exit 0

# }}}
# 2. Get necessary variables from arguments {{{

while [[ "$1" =~ ^-- && ! "$1" == "--" ]]; do
  case $1 in
    --output_dir ) shift; output_dir=$1 ;;
    --markdown_bin ) shift; markdown_bin=$1 ;;
    *) shift ;;
  esac
  shift
done

export PATH=/bin:/usr/bin:/usr/local/bin:~/.local/bin
output_dir=${output_dir:-$(dirname $0)}
markdown_bin=${markdown_bin:-$(which markdown 2>/dev/null)}
[ -x "$markdown_bin" ] || markdown_bin=cat

# }}}
# 3. Read header fields {{{

# enable execute last command in pipe under current shell
shopt -s lastpipe; set +m;

# save each field of header into variables
echo "$header" | \
while IFS=: read field value; do
  field="${field^^}"
  field="${field//-/_}"
  export declare $field="${value}"
done
DATE=${DATE:+$(date --rfc-3339 seconds --date "$DATE")}

# }}}
# 4. Get path of output file {{{

path=$(<<<"$header" sed -En "\\|${pattern}| {s//\\2/p; q}")

# sender want comment on some page, but find no path for this
if [ $path = "" ]; then
  echo 'Cannot get target of comment from mail' >&2
  exit 1
fi

# get output path
[[ "$path" =~ /$ ]] && path+=index
path=${path#/}
path=${path/.html}
output=$output_dir/${path}.comment.html
umask 022; mkdir -p $(dirname $output)

# }}}
# 5. Get comment from mail body {{{

printTextPart() {
  boundary=$1
  beginPat="\\|^--${boundary}\$|"
  endPat="\\|^--${boundary}(--)?\$|"
  sed -En "${beginPat},${endPat} { \@^Content-Type: text/plain@,$ {1,/^$/d; ${beginPat}d; p} }"
}

# check mail includes multiple part
boundary="$(<<<"$CONTENT_TYPE" sed -En 's/^.*boundary="?([^"]+)"?.*$/\1/p')"
if [ -n "${boundary}" ]; then
  # print mail part in MIME: text/plain
  body="$(<<<"$body" printTextPart ${boundary})"
fi

# }}}
# 6. Write comment to output file {{{

# set STDOUT to $output, with readable permission for everyone
umask 133

# add basic html layout for output file if necessary {{{
if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then
  <<-LAYOUT cat >$output
	<style>
	   ul {
	     padding-inline: 1rem;
	     li {
	       margin-block: 1rem;
	     }
	   }
	  .comment-body {
	    margin-top: 0.5rem;
	    padding: 0.5rem;
	    overflow-x: scroll;
	    border-radius: 4px;
	    background: lightblue;
	    p {
	      margin: 0.5rem;
	    }
	  }
	  .replies {
	    display: none;
	    &:has(li) {
	      display: block;
	    }
	    summary {
	      cursor: pointer;
	    }
	    ul {
	      padding-inline: 1rem 0;
	    }
	  }
	</style>
	<ul>
	</ul>
	LAYOUT
fi
# }}}
# get line of insert position by header field "In-Reply-To" {{{
if [ -n "${IN_REPLY_TO}" ]; then
  line=$(grep -n "^<!-- ${IN_REPLY_TO} -->$" $output | cut -d':' -f1)
fi
# }}}
# insert comment into output file {{{

# FIXME prevent pattern <!-- ${MESSAGE_ID} --> shown in <pre> block
<<-COMMENT sed -i "${line:-/<ul>/}r /dev/stdin" $output
	<li>

	<time datetime="${DATE}">${DATE}</time>
	<a href="mailto:${RECIPIENT}?subject=Comment on page: ${path}&in-reply-to=${MESSAGE_ID}">[reply]</a>

	<div class='comment-body'>
	$(<<<"${body}" ${markdown_bin})
	</div>

	<details class="replies" open="true">
	<summary>replies</summary>
	<ul>
	<!-- ${MESSAGE_ID} -->
	</ul>
	</details>

	</li>
COMMENT

# }}}

# }}}

# vim:fdm=marker fdl=0