sub spamassassin {
  #Only run SA if mail is from a "remote" SMTP client, or QS_SPAMASSASSIN 
  #is defined via tcpserver...
  if (defined($ENV{'RELAYCLIENT'}) && !defined($ENV{'QS_SPAMASSASSIN'})) {
    &debug("spamassassin: don't scan as RELAYCLIENT implies this was sent by a local user");
    return;
  }
  #SpamAssassin client scanner
  my ($spamassassin_found,$spamassassin_status);
  my ($start_spamassassin_time)=[gettimeofday];
  my ($sa_tag,$DD,$stop_spamassassin_time,$spamassassin_time,$cmdline_recip,$sa_fast);
  my ($sa_status)=0;
  my ($sa_score)=0; my ($sa_max)=0;

  if ($msg_size > 250000) {
    &debug("spamassassin: message too big - skip it");
    $sa_score=$sa_max="?";
    $tag_score .= "SA:0($sa_score/$sa_max):";
    $sa_comment = "No, hits=$sa_score required=$sa_max" if ($sa_fast);
    return;
  }

  #Cleanup $one_recip so it's usable from the commandline...
  #any char that isn't supported to changed into an '_'
  ($cmdline_recip=$one_recip)=~s/[^0-9a-z\.\_\-\=\+\@]/_/gi;
  $cmdline_recip=~/^([0-9a-z\.\_\-\=\+\@]+)$/i;
  $cmdline_recip=tolower($1);

  $sa_fast=1 if ($spamc_options =~ /\-c /);
  $spamc_options="$spamc_options -u \"$cmdline_recip\"" if ($cmdline_recip ne "");
  &debug("SA: run $spamc_binary $spamc_options < $scandir/$wmaildir/new/$file_id");
  open(SIN,"<$scandir/$wmaildir/new/$file_id")||&error_condition("cannot open $scandir/$wmaildir/new/$file_id - $!");
  open(SOUT,"|$spamc_binary $spamc_options > $scandir/$wmaildir/new/$file_id.spamc")||&error_condition("cannot open for write $scandir/$wmaildir/new/$file_id.spamc - $!");

  print SOUT "X-Envelope-From: $headers{'MAILFROM'}\n";
  while (<SIN>) {
    print SOUT;
  }
  close(SIN)||&error_condition("cannot close $scandir/$wmaildir/new/$file_id - $!");
  close SOUT;
  $spamassassin_status=($? >> 8);
  $sa_status=$spamassassin_status if ($sa_fast);
  open(SA,"<$scandir/$wmaildir/new/$file_id.spamc")||&error_condition("cannot open for read $scandir/$wmaildir/new/$file_id.spamc - $!");
  while (<SA>) {
    if ($sa_fast) {
      chomp;
      ($sa_score,$sa_max)=split(/\//,$_,2);
      $sa_tag++;
      last;
    } else {
      #X-Spam-Status: No, hits=2.8 required=5.0
      if (/^X-Spam-Status: (Yes|No), (hits|score)=(-?[\d\.]*) required=([\d\.]*)/) {
	$sa_tag++;
	$sa_status=1 if ($1 eq "Yes");
	$sa_score=$3;$sa_max=$4;
      }
    }
  }
  close SA ;

  $sa_score='?' if (!$sa_score);
  $sa_max='?' if (!$sa_max);

  if (!$sa_fast && -s "$scandir/$wmaildir/new/$file_id.spamc" && $spamassassin_status == 0) {
    &debug("SA: overwriting $scandir/$wmaildir/new/$file_id with $scandir/$wmaildir/new/$file_id.spamc");
    rename ("$scandir/$wmaildir/new/$file_id.spamc","$scandir/$wmaildir/new/$file_id");
  } else {
    unlink("$scandir/$wmaildir/new/$file_id.spamc");
  }
  if ($sa_max > $sa_score || ($sa_score == 0)) {
    $tag_score .= "SA:0($sa_score/$sa_max):";
    $sa_comment = "No, hits=$sa_score required=$sa_max" if ($sa_fast);
  } else {
    $tag_score .= "SA:1($sa_score/$sa_max):";
    $sa_comment = "Yes, hits=$sa_score required=$sa_max" if ($sa_fast);
    &debug("SA: yup, this smells like SPAM");
  }	
  if ($sa_score > 0) {
    $sa_score=int($sa_score);
    #Keep it RFC compliant
    $sa_score=100 if ($sa_score > 100);
    my $si=0;
    if ($sa_fast) {
      while ($si < $sa_score) {
	$si++;
	$sa_level .= $sa_symbol;
      }
    }
  }
  $stop_spamassassin_time=[gettimeofday];
  $spamassassin_time = tv_interval ($start_spamassassin_time, $stop_spamassassin_time);
  &debug("spamassassin: finished scan of dir \"$ENV{'TMPDIR'}\" in $spamassassin_time secs");
}


