From a8614df78ab59765937ed9d896429cce7d0ec800 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 6 Dec 2024 11:55:33 +0800 Subject: Update comment.sh --- bin/mail/comment.sh | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'bin/mail/comment.sh') diff --git a/bin/mail/comment.sh b/bin/mail/comment.sh index 299299a..f13b03d 100755 --- a/bin/mail/comment.sh +++ b/bin/mail/comment.sh @@ -6,9 +6,10 @@ # 1. Check mail is for comment {{{ -# Restore mail into variable +# Restore mail into variables MAIL="$(tr -d '\r')" -header="$(<<<"$MAIL" sed '/^$/ q')" +# 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 @@ -38,8 +39,9 @@ markdown_bin=${markdown_bin:-markdown} shopt -s lastpipe; set +m; # save each field of header into variables -<<<"$header" grep '^[a-zA-Z]' \ -| while read field value; do +echo "$header" | \ +while read field value; do + echo "$field" "$value" >>/tmp/header declare field=$(<<<$field tr [:lower:] [:upper:] | tr '-' '_' | tr -d ':') declare $field="${value}" done @@ -66,13 +68,8 @@ output=$output_dir/${path}.comment.html # 5. Get comment from mail body {{{ # check mail includes multiple part -if [[ "$CONTENT_TYPE" =~ mixed ]]; then - boundary="$(<<<"$CONTENT_TYPE" sed -En 's/^.*boundary="(.*)".*$/\1/p')" - if [ $boundary = "" ]; then - echo 'cannot get boundary from mail header' >&2 - exit 1 - fi - +boundary="$(<<<"$CONTENT_TYPE" sed -En 's/^.*boundary="?([^"]+)"?.*$/\1/p')" +if [ -n "${boundary}" ]; then # print content of first mail part boundaryPat="\\|^--${boundary}\$|" body="$(<<<"$body" sed -n "${boundaryPat},${boundaryPat} p" | sed -n "1,4d; ${boundaryPat} q; p")" @@ -85,10 +82,17 @@ fi if [ ! -f $output ] || ! xmllint --html --nofixup-base-uris $output &>/dev/null; then <<-LAYOUT cat >$output