From a539daebadf5f4a5058027f5be9d7cd4cbdee878 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 5 Dec 2024 20:41:23 +0800 Subject: Update comment --- bin/mail/comment.sh | 91 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 31 deletions(-) (limited to 'bin') 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 @@ # Save incoming mail as comment # Usage: -# echo '|' >>~/.forward +# echo '| --output_dir=' >>~/.forward -set -x -exec 2>>/dev/pts/1 -env >&2 +# 1. Check mail is for comment {{{ + +# Restore mail into variable +MAIL="$(tr -d '\r')" +header="$(<<<"$MAIL" sed '/^$/ q')" +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 - --mailto ) shift; mailto=$1 ;; --output_dir ) shift; output_dir=$1 ;; + --markdown_bin ) shift; markdown_bin=$1 ;; + *) shift ;; esac shift done -mailto=${mailto:-`whoami`@${HOSTNAME:?HOSTNAME is not specified}} -[ -z "$mailto" ] && echo --mailto is not specified} -[ -z "$output_dir" ] && echo --output_dir is not specified} -markdown_bin=${markdown:-markdown} +output_dir=${output_dir:?} +markdown_bin=${markdown_bin:-markdown} [ -x $(which $markdown_bin) ] || markdown_bin=cat -# Check mail is for comment {{{ - -# Restore mail into variable -MAIL="$(tee /tmp/mail | tr -d '\r')" -header="$(<<<"$MAIL" sed '/^$/ q')" -body="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" - -# determine mail is for comment by pattern -pattern='^Subject: .*comment on (https?://)?([^/]+/)?([^ ]+)$' -<<<"$header" grep -E "$pattern" >/dev/null || exit 0 - # }}} -# Process header fields {{{ +# 3. Read header fields {{{ # enable execute last command in pipe under current shell shopt -s lastpipe; set +m; @@ -48,7 +46,7 @@ done DATE=${DATE:+$(date --rfc-3339 seconds --date "$DATE")} # }}} -# Get path of output file {{{ +# 4. Get path of output file {{{ path=$(<<<"$header" sed -En "\\|${pattern}| {s//\\3/p; q}") @@ -65,7 +63,7 @@ path=${path/.html} output=$output_dir/${path}.comment.html # }}} -# Get comment from mail body {{{ +# 5. Get comment from mail body {{{ # check mail includes multiple part if [[ "$CONTENT_TYPE" =~ mixed ]]; then @@ -81,29 +79,60 @@ if [[ "$CONTENT_TYPE" =~ mixed ]]; then fi # }}} -# Write comment to output file {{{ +# 6. Write comment to output file {{{ -# add basic html layout for output file if necessary +# add basic html layout for output file if necessary {{{ if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then - echo -e '
    \n
' >$output + <<-LAYOUT cat >$output + +
    +
+ LAYOUT fi -# get line of insert position by header field "In-Reply-To" +# }}} +# get line of insert position by header field "In-Reply-To" {{{ if [ -n "${IN_REPLY_TO}" ]; then line=$(grep -n "^$" $output | cut -d':' -f1) fi - -# insert comment into output file -<<-COMMENT sed -i "${line:-1}r /dev/stdin" $output +# }}} +# insert comment into output file {{{ +<<-COMMENT sed -i "${line:-/
    /}r /dev/stdin" $output
  • + - [reply] + [reply] +
    $(<<<"${body}" ${markdown_bin}) +
    +
    + replies
    +
    +
  • COMMENT # }}} + +# }}} -- cgit v1.2.3-70-g09d2