From d1b57c1d68fa60a4a67c208f29b739ef0f20d5c5 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 3 Dec 2024 17:33:13 +0800 Subject: Update --- bin/mail/comment.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 bin/mail/comment.sh (limited to 'bin/mail/comment.sh') diff --git a/bin/mail/comment.sh b/bin/mail/comment.sh new file mode 100755 index 0000000..807bcb2 --- /dev/null +++ b/bin/mail/comment.sh @@ -0,0 +1,41 @@ +#! /bin/bash + +output_dir=${output_dir:-/srv/http} + +# Restore mail into variable +MAIL="$(tr -d '\r')" +headers="$(<<<"$MAIL" sed '/^$/ q')" +contents="$(<<<"$MAIL" sed -n '/^$/,$ p' | sed '1d')" + +# enable execute last command in pipe under current shell +shopt -s lastpipe; set +m; + +# get route (target page of comment) and id +<<<"$MAIL" sed -En '/^To: comment+/ {s/^To: comment\+([^+]+)\+?(.*)@.*$/\1 \2/p; q}; /^$/q' \ +| read route id + +# sender want comment on some page, but find no route for this +if [ $route = "" ]; then + echo 'rcpt "comment+" not matched' >&2 + exit 1 +fi + +output=$output_dir/${route#/}.comment +exec 1>>$output + +# check mail includes multiple part +<<<"$headers" grep '^Content-Type:.*mixed' >/dev/null +if [ $? -eq 0 ]; then + boundary="$(<<<"$headers" sed -En 's/^Content-Type:.*boundary="(.*)".*$/\1/p')" + if [ $boundary = "" ]; then + echo 'cannot get boundary from mail header' >&2 + exit 1 + fi + + # print content of first mail part + pattern="\\@^--${boundary}\$@" + <<<"$contents" sed -n "${pattern},${pattern} p" | sed -n "1,4d; ${pattern} q; p" +else + # print content + <<<"$contents" sed '/^$/,$ p' +fi -- cgit v1.2.3-70-g09d2