Revision 7451 (by gradha, 2006/08/06 18:54:19) Found valid configuration to resend to SF mailing list: removed
bounce addresses, X-BeenThere headers and other mailman created
headers.
#!/bin/sh

# This script should be used to process incoming alleg-commit emails
# on the machine of somebody subscribed to that mailing list (most
# likely the web master). The script will process the content of
# the message and rebounce the mail to alleg-www-commit if some
# specific strings are found in the body.
#
# The script uses grep, cat, sed, head and the mail processing
# programs formail and procmail. To meet the path requirement of
# the special procmail bounce file, link it from your home directory
# to the place where you have the allegrowww svn checkout.
#
# In fact, you can do the same with this script, instead of using
# it directly, put in your .procmail or .forward file a specific
# path in your home which is a link to the checkout. That will allow
# other people to update the script, as long as you have cron jobs
# that perform "svn up" on that checkout.

PROCMAIL_FILTER=~/.procmailrc-rebounce-alleg-commit
CAT=/bin/cat
DATE=/bin/date
FORMAIL=/usr/bin/formail
GREP=/bin/grep
HEAD=/usr/bin/head
PROCMAIL=/usr/bin/procmail
RM=/bin/rm
SED=/bin/sed

REJECT_LOG=/tmp/.rebounce-alleg-commit.rejects
ERROR_LOG=/tmp/.rebounce-alleg-commit.errors
TMP_FILE=/tmp/rebounce-alleg-commit.$$.tmp
FULL_FILE=/tmp/rebounce-alleg-commit.$$.full.tmp

# Redirect all output and errors to local log file.
exec 2>> $ERROR_LOG 1>> $ERROR_LOG

# Captures the full mail to a temporary file.
$CAT > $FULL_FILE

# Process only up to the first equal line, and if there is none,
# do checks on first 200 lines to avoid wasting time.
$SED -e "/^================/ q" < $FULL_FILE | $HEAD -n 200 > $TMP_FILE
echo End marker `$DATE` >> $TMP_FILE

# Moves the full mail to the reject log adding some comments,
# then removes the temporary files of the script.
function cleanup_and_exit
{
	echo "Not found allegrowww/ text" >> $REJECT_LOG
	$CAT $FULL_FILE >> $REJECT_LOG
	echo End marker `$DATE` >> $REJECT_LOG
	$RM $TMP_FILE $FULL_FILE
	exit 0
}

# Check that we are actually processing the correct type of mail.
$GREP "X-BeenThere: alleg-commits@lists.sourceforge.net" $TMP_FILE 1> /dev/null
if [[ $? != 0 ]]
then
	cleanup_and_exit "Not coming from alleg-commits"
fi

# Did we find any allegrowww/ paths?
$GREP "allegrowww/" < $TMP_FILE 1> /dev/null
if [[ $? == 0 ]]
then
	# Add our headers and bounce with procmail.
	$SED -e "s/alleg-commits-bounces@lists.sourceforge.net/gradha@users.sourceforge.net/g" $FULL_FILE | \
		$FORMAIL -i "List-Id: Log messages from allegrowww svn commits" | \
		$FORMAIL -i "X-rebounce: alleg-commit" | \
		$FORMAIL -i "To: alleg-www-commits@lists.sourceforge.net" | \
		$FORMAIL -I "X-BeenThere:" | \
		$FORMAIL -i "Reply-to: alleg-www-commits@lists.sourceforge.net" | \
		$PROCMAIL $PROCMAIL_FILTER

	$RM $TMP_FILE $FULL_FILE
else
	cleanup_and_exit "Not found allegrowww/ text"
fi

exit 0