#!/usr/bin/perl
# This is an small wrapper script around Mail::SpamAssassin for the use
# with the exim MTA.
# Modified to eliminate the buggy Mail::Audit use.
# This modification also eliminates the need to re-submit the message
# to Exim for delivery to the mail spool. Requires localuser director
# to use the spamcheck transport so it knows where to append to.
# It is released under the Terms of GNU General Public License (GPL) so
# use at your own risk!

use Mail::SpamAssassin;
use Mail::SpamAssassin::NoMailAudit;

$savespam = 0;                      #1/0 should spam be saved centrally?
$spamfile = '/var/mail/spam';       #If you said 1 above - where to put
                                    #that nasty spam? Be sure this mailbox
                                    #is writable by the user who runs this
                                    #script (e.g. mail)
#These are given as command line arguments by exim:
$sender = shift(@ARGV);
$sender = '<>' if $sender eq '';
$recpt = '';
while (@ARGV){
  $recpt = $recpt.' '.shift(@ARGV);
}
####### Main script ###########################################################
#open LOG, ">>/log2";
$mail = Mail::SpamAssassin::NoMailAudit->new();

$spamtest = Mail::SpamAssassin->new();
$status = $spamtest->check ($mail);

#Add the X-Spam Headers:
$status->rewrite_mail ();
#Auto Report to Vipul's Razor:
$status->handle_auto_report ();

#print LOG "Recpt = '$recpt'\n";

  my ($name) = ($recpt =~ /^\s+([^@]+)@/);

if ($status->is_spam ()) {
  if ($savespam) {
    $mail->accept($spamfile);    # Deliver to spamfile

  } else {

#print LOG "name = '$name'\n";
    if ($name eq 'sandy') {
         $mail->accept("/usr/mail/$name.spam");   # Deliver to user's mailbox
 
   } else {
         $mail->accept();        # Deliver to user's mailbox
    }
  }
} else {
  $mail->accept();            # to default incoming mailbox

}

