aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/mail/comment.sh
blob: 807bcb2a6a0360b266b89af550988a775eec38e5 (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
#! /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+<ROUTE>" 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