#!/usr/bin/perl
#
# Enable despamming of e-mail
#
$MODULES="/usr/lib/despam/modules";
$ADDFORWARD="y";
$PROCMAIL="/usr/bin/procmail";
$DESPAM="/usr/bin/despam";
$HOME = $ENV{"HOME"};
$DESPAMRC = "$HOME/.despamrc";
$PROCMAILRC = "$HOME/.procmailrc";
$FORWARD = "$HOME/.forward";

if (! -e "$PROCMAILRC")
 {
  print "You do not have a .procmailrc.  Should I create one? (y/n) [y] --->";
  $yn = <STDIN>;
  chop $yn;
  $yn = "y" unless $yn;
  &MakeProcmailRC(0) if ($yn =~ /^[Yy]/);
 }
else
 {
  open(PMRC,"$PROCMAILRC");
  while (<PMRC>)
   {
    if (/$DESPAM/)
     {
      print "You already have despam in your procmail.\n";
      print "Continuing on with configuration\n\n";
      $gotpmrc++;
      last;
     }
   }
  close(PMRC);
  if (!$gotpmrc)
   {
    print "You have a .procmailrc, but despam is not installed.\n";
    print "Should I place it at the start of the file? (y/n) [y] --->";
    $yn = <STDIN>;
    chop $yn;
    $yn = "y" unless $yn;
    &MakeProcmailRC(1) if ($yn =~ /^[Yy]/);
   }
 }
if ($ADDFORWARD)
 {
  open (FWD,"$FORWARD");
  while (<FWD>)
   {
    if (/$PROCMAIL/)
     {
      $gotpm++;
     }
   }
  close(FWD);
  if (!$gotpm)
   {
    print "To activate despam, you need to change your .forward file.\n";
    print "Should I add it to the file, replace the file, or neither?\n";
    $action = "";
    while (!$action)
     {
      print "(a/r/n) [a] --->";
      $action = <STDIN>;
      chop $action;
      $action = "a" unless $action;
      if ($action !~ /^[aArRnN]/)
       {
        $action = "";
       }
     }
    if ($action =~ /^[aA]/)
     {
      open(FWD,">>$FORWARD");
      print FWD "\"|$PROCMAIL\"\n";
      close(FWD);
     }
    elsif ($action =~ /^[rR]/)
     {
      open(FWD,">$FORWARD");
      print FWD "\"|$PROCMAIL\"\n";
      close(FWD);
     }
   }
 }

print "Now I will ask some questions to determine how you want despam to act.";
print "\n\n";
print "The default setup is none at all - that is, you trust your systems\n";
print "administrator or SysOp to be selective enough in accepting new blocks.";
print "\n";
$yn = "";
while (!$yn)
 {
  print "Is this okay? (y/n) [y] --->";
  $yn = <STDIN>;
  chop $yn;
  $yn = "y" unless $yn;
  $yn = "" if ($yn !~ /^[YynN]/);
 }
if ($yn =~ /^[Yy]/)
 {
  system ("cp /dev/null $DESPAMRC");
  print "Done! - despam is now active.\n";
  exit(0);
 }
print "Getting current blocker module list...\n\n";
print "Current modules are:\n";
open(MODLIST,"ls -1 $MODULES|");
@modlist = <MODLIST>;
chomp @modlist;
close(MODLIST);
unshift (@INC,"$MODULES");
foreach $module (@modlist)
 {
  require "$module";
  eval "\$desc = &${module}::Description()";
  print "   $module:  $desc\n";
 }
print "\n";
print "Most users will want all blockers turned on.  Administrators should\n";
print "not enable any blocker which would block by address, and anyone who\n";
print "normally deals with e-mail abuse should probably not use this\n";
print "progam at all.\n\n";
print "Which blockers do you want to disregard? [none] --->";
while (!$DontBlockBy)
 {
  $DontBlockBy = <STDIN>;
  chop $DontBlockBy;
  if (!$DontBlockBy)
   {
    $DontBlockBy = "none";
   }
  else
   {
    @dontblockby = split(/\s*,\s*/, $DontBlockBy);
    for($i = 0; $i < scalar(@dontblockby); $i++)
     {
      $dont = $dontblockby[$i];
      ($dont) = grep (/^$dont$/i, @modlist);
      if (!$dont)
       {
        print "Unable to find $dontblockby[$i]\n";
        $DontBlockBy = "";
        last;
       }
     }
   }
 }
$DontBlockBy = "" if ($DontBlockBy eq "none");
print "If a new module is added, do you want to use it automatically?\n";
print "(Again, administrators should be cautious here...)\n";
$yn = "";
while (!$yn)
 {
  print "(y/n) [y] --->";
  $yn = <STDIN>;
  chop $yn;
  $yn = "y" unless $yn;
  $yn = "" if ($yn !~ /^[YynN]/);
 }
$DefaultAction = "Do" if ($yn =~ /[Yy]/);
$DefaultAction = "Dont" unless $DefaultAction;
$KnownBlockers = join(",",@modlist);
open(DRC,">$DESPAMRC");
print DRC "DefaultAction: $DefaultAction\n";
print DRC "KnownBlockers: $KnownBlockers\n";
print DRC "DontBlockBy: $DontBlockBy\n";
close(DRC);
print "Done - despam is now active.\n";

sub MakeProcmailRC
 {
  ($haveoldfile) = @_;
  print "What would you like to do with unwanted mail?\n";
  print "Junk it, mail it to someone, or save it (junk, mail, save) ";
  print "[mail] --->";
  while (!$action)
   {
    $action = <STDIN>;
    chop $action;
    $action = "mail" unless $action;
    $action =~ y/[A-Z]/[a-z]/;
    $action = "" unless $action =~ /^[jms]/;
    print "Invalid option.  Please try again --->" if !$action;
   }
  if ($action =~ /^m/)
   {
    $yn = "n";
    while ($yn !~ /^[Yy]/)
     {
      print "Who do you want to send the junk to? [abuse] --->";
      $recipient = <STDIN>;
      chop $recipient;
      $recipient = "abuse" unless $recipient;
      print "$recipient: is this correct? (y/n) [y] --->";
      $yn = <STDIN>;
      chop $yn;
      $yn = "y" unless $yn;
     }
   }
  elsif ($action =~ /^s/)
   {
    $yn = "n";
    while ($yn !~ /^[Yy]/)
     {
      print "Where do you want to save it? [$HOME/UCE] --->";
      $recipient = <STDIN>;
      chop $recipient;
      $recipient = "$HOME/UCE" unless $recipient;
      print "$recipient: is this correct? (y/n) [y] --->";
      $yn = <STDIN>;
      chop $yn;
      $yn = "y" unless $yn;
     }
   }

  system("cp $PROCMAILRC /tmp/procmailrc.$$") if $haveoldfile;
  open(PMRC,">$PROCMAILRC");
  print PMRC ":0 HB\n";
  print PMRC "*!? $DESPAM\n";
  if ($action =~ /^j/)
   {
    print PMRC "/dev/null\n";
   }
  elsif ($action =~ /^m/)
   {
    print PMRC "!$recipient\n";
   }
  else
   {
    print PMRC "$recipient\n";
   }
  print PMRC "\n";
  if ($haveoldfile)
   {
    open (TMPF,"/tmp/procmailrc.$$");
    while (<TMPF>)
     {
      print PMRC;
     }
    close(TMPF);
    unlink "/tmp/procmailrc.$$";
   }
  close(PMRC);
 }
