#!/usr/bin/perl -w
#
# spamkdiscards
# =============
# The spamkdiscards filter is used by logwatch to list details of spam 
# discarded by spamkiller. This filter is implemented as a perl script. 
#
# Copyright 2010, Martin Gregorie.
#
# This file is part of the Spamkiller package.
#
# Spamkiller is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# Spamkiller is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Spamkiller.  If not, see <http://www.gnu.org/licenses/>.
#              
# Parse and validate the command line options
# ===========================================
# Set defaults and version
#

$help = 0;
$version = "1.1";

#
# Parse the command line
#
foreach $arg (@ARGV)
{
   if ($arg eq "-?")
   {
      $help = 1;
   }
   else
   {
      printf("Error: unknown option %s\n", $arg);
      exit;
   }
}

#
# Display help if requested and exit.
#
if ($help)
{
   $prgnam = $0;
   $prgnam =~ s/^.*\///;
   printf("\n$prgnam - $version (c) Martin Gregorie, 2010,2011\n\n");
   printf("Syntax:   $prgnam\n");
   printf("Function: List spam messages discarded by spamkiller.\n");
   printf("          Input is read from STDIN.\n");
   printf("\n");
   printf("Options:  none\n");
   exit;
}

#
# Scan the maillog(s)
# ===================
#
$from = "** Unknown **";
$subject = "";
$rules = "";
$quarantine = "";

while ($line = <STDIN>)
{
   if ($line =~ /spamkiller/)
   {
      @fld = split(/\s/, $line);
      $n = @fld - 1;
      $value = join(" ", @fld[7..$n]);

      if ($line =~ /From:/ )
      {
         $from = $value;
      }

      if ($line =~ /Rules:/ )
      {
         $rules = $value;
      }

      if ($line =~ /Subject:/ )
      {
         $subject = $value;
      }

      if ($line =~ /Quarantined/ )
      {
         $quarantine = $value;
      }

      if ($line =~ /(Discarded|Quarantined)/ )
      {
         if ($quarantine eq "")
	 {
            $payload = $subject . "<SEP>" . $rules;
         }
         else
         {
            $payload = $subject . "<SEP>" . $rules .  "<SEP>" . $quarantine;
         }

         $spamlist{$from} = $payload; 
      }

      if ($line =~ /(Discarded|Quarantined|Passed to)/ )
      {
         $from = "** Unknown **";
         $subject = "";
         $quarantine = "";
      }
   }
}

foreach $spamref (sort keys %spamlist)
{
   @fld = split(/<SEP>/, $spamlist{$spamref}); 
   $n = @fld;
   
   $_ = $spamref; /\s*(.+)\s*/; $spamref=$1;
   $_ = $fld[0]; /\s*(.{1,70})/; $fld[0]=$1;
   printf("%s\n%4s%s\n", $spamref, " ", $fld[0]);
   if ($n >= 2)
   {
      @rules = split(/,/, $fld[1]);
      $rline = "";
      foreach $rule (@rules)
      {
          $_ = $rule; 
          /\s*(\S+)\s*/;
          $rule = $1;
	  $n1 = (length $rline . $rule);
          if ($n1 < 70)
          {
             if ( $rline eq "" )
             {
                $rline = $rule;
             }
             else
             {
                $rline = $rline . "," . $rule;
             }
          }
          else
          {
             printf("    %s\n", $rline);
             $rline = "";
          }
      }

      if ($rline ne "")
      {
         printf("    %s\n", $rline);
      }
          
   }

   if ($n == 3)
   {
      @q = split(m!/!, $fld[2]);
      printf("    Quarantined as %s\n", $q[1]);
   } 
}
