#! /usr/bin/perl
#
# list2list to from
#
# Dup killer, handles the case of two mailing lists that both contain
# each other. It checks for the string "X-L2L: hostname", it kills it
# if found; if not it is added and the message processed.

($program = $0) =~ s%.*/%%;

if ($#ARGV < 1) { die "usage: $program to-alias from-alias [From:-addr]\n"; } 
$toalias = $ARGV[0];
$fromalias = $ARGV[1];
if ($#ARGV > 1) { $fromhack = $ARGV[2]; }
else { $fromhack = ""; }

$sendmail = "/usr/sbin/sendmail"; 
$sendmail = "/usr/local/bin/sendmail" unless -x $sendmail;
die "$program: can't find sendmail\n"  unless -x $sendmail;
$iopts = "-om -oi -odb";

# in case sendmail dumps core or something crazy
$SIG{'PIPE'} = "plumber";
sub plumber { die "$program: \"$sendmail\" died prematurely!\n"; }

# header munging loop
while (<STDIN>) {
   last if /^$/;
   if (/^From:/) { $from = $_; }
   elsif (/^To:/) { $to = $_; }
   elsif (/^Cc:/) { $cc = $_; }
   elsif (/^Date:/) { $date = $_; }
   elsif (/^Subject:/) { $subject = $_; }
   elsif (/^Control:/) { $control = $_; }
   elsif (/^Message-Id:/) { $msgid = $_; }
   elsif (/X-L2l: wps.com/i) { $gated = $_; }
   elsif (/X-L2l:/i) { $l2l = $_; }
   } 

if ($gated ne "") { exit 0; }
if ($control ne "") { exit 0; }

open (SENDMAIL, "| $sendmail $iopts -f$fromalias $toalias") ||
    die "$program: can't run $sendmail\n";

if ($fromhack ne "") {print SENDMAIL "From: $fromhack\n";}
elsif ($from ne "") {print SENDMAIL "$from";}
if ($to ne "") {print SENDMAIL "$to";}
if ($cc ne "") {print SENDMAIL "$cc";}
if ($date ne "") {print SENDMAIL "$date";}
if ($subject ne "") {print SENDMAIL "$subject";}
if ($msgid ne "") {print SENDMAIL "$msgid";}
if ($l2l ne "") {print SENDMAIL "$l2l";}
print SENDMAIL "X-L2l: wps.com\n";

print  SENDMAIL "\n";

print SENDMAIL while <STDIN>;   # gobble rest of message

close SENDMAIL;

exit $?;


