#!/usr/bin/perl
# @(#) fix email subjects by removing multiple re: etc.
# (c) 2006 by Dirk Jagdmann <doj@cubic.org>
#
# use this script in procmail as a filter:
# :0 f
# | /path/.../fixreaw.pl

$re='re|re-\d+|aw|antwort';
$fwd='fwd?|wg';
$colon='(:|=3A)';
$regex="($re|$fwd)$colon";

while(<>)
{
    # find the correct subject line
    if(/^Subject:.*$regex/io)
    {
	my $r=0; my $f=0;
	s/^Subject:\s*//i;	# strip Subject: prefix
	if(s/($re)$colon\s*//gi) { $r=1 } # strip Re: etc
	if(s/($fwd)$colon\s*//gi) { $f=1 } # strip Fwd: etc
	my $t=$_;

	# rebuild subject line
	$_="Subject: ";
	$_.="Re: " if $r;
	$_.="Fwd: " if $f;
	$_.=$t;
    }
    print;
}
