summaryrefslogtreecommitdiffhomepage
path: root/plugins/weblog/procmail/procmail.sh
blob: 44fa68f3d7ad2fac260cfca00b996d35a0319863 (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
#!/bin/bash
#
# Mail Blog input script, to be called by procmail
#
set -e

function atexit {
	if [ "$ERRORMSG" != "" -o -s errormsg.txt ] ; then
		(echo "$ERRORMSG" ; cat errormsg.txt) | mutt -s "bloginput: Error" -- "$ADDR" >> ~/log 2>&1
	fi
	cd
	rm -rf "$DIR"
}
trap atexit EXIT

function mimedecode() {
 perl -ne 'require MIME::Head; $head = MIME::Head->read(\*STDIN); $head->decode(); print $head->get("Subject")'
}

echo "Log at `date`:" >> ~/log 2>&1

BLOGDIR=/var/www/blog

# Default: Error
ERRORMSG="General error."
USERLOG=""

DIR=`mktemp -d`

cd "$DIR"

touch body.txt
touch errormsg.txt

cat > inmail

ADDR=`grep "^From: " inmail | sed -e 's/^From: //'`
if echo "$ADDR" | grep -q -v "^[a-zA-Z0-9@._-<>\" ]\+$" ; then
	# can't send error message to unknown requester
	ERRORMSG=""
	exit 1
fi
if echo "$ADDR" | grep -q "weblog-bloginput" ; then
	# don't reply to mails from weblog-bloginput, i.e. prevent mail loops
	ERRORMSG=""
	exit 1
fi

ALLLINES=`wc -l inmail | cut -f1 -d" "`

HEADERLINES=`cat inmail | (n=0; while read i ; do
 if [ "$i" == "" ] ; then
  echo $n
  break
 fi
 n=$(($n + 1))
done)`

HEADER=`head -n$HEADERLINES inmail`
BODY=`tail -n$(($ALLLINES - $HEADERLINES - 1)) inmail`

echo "$HEADER" > /home/weblog-bloginput/header.txt
echo "$BODY" > /home/weblog-bloginput/body.txt

SUBJECT=`echo "$HEADER" | mimedecode`
SUBJECT_PATH=`echo "$SUBJECT" | sed -e 's/[^a-zA-Z0-9]/_/g'`

ARTICLEDIR="$BLOGDIR/`date +%Y/%Y%m%d`_$SUBJECT_PATH"

mkdir -p $ARTICLEDIR
echo "Subject: $SUBJECT" >> $ARTICLEDIR/article.data
echo "" >> $ARTICLEDIR/article.data

# get attachments
munpack -C $ARTICLEDIR `pwd`/inmail >/dev/null || true

DATANAME="`ls $ARTICLEDIR/*.desc || true`" 2>/dev/null
if [ "$DATANAME" = "" ] ; then
 echo "$BODY" >> $ARTICLEDIR/article.data
else
 cat "$DATANAME" >> $ARTICLEDIR/article.data
 rm "$DATANAME"
fi

chmod -R a+r $ARTICLEDIR
chmod a+x $ARTICLEDIR

echo "Processed successfully." | mutt -s "Bloginput: $SUBJECT_PATH" -- "$ADDR" >> ~/log 2>&1

echo "User log:" >> ~/log
cat body.txt >> ~/log

ERRORMSG=""
echo "Done successfully at `date`." >> ~/log 2>&1