#!/usr/bin/perl -w
#
# skerrors
# ========
# The skerrors filter is used by logwatch to list any errors
# output by spamkiller. This filter is implemented as a perl script. 
#
# Copyright 2009, 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.0";

#
# 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, 2009\n\n");
   printf("Syntax:   $prgnam\n");
   printf("Function: Report spamkiller errors.\n");
   printf("          Input is read from STDIN.\n");
   printf("\n");
   printf("Options:  none\n");
   exit;
}

#
# Scan the maillog(s)
# ===================
#
while ($line = <STDIN>)
{
   if ($line =~ /spamkiller/)
   {
      #
      # Anything logged by spamkiller that doesn't contain 'Discarded',
      # 'Passed to', 'From:', 'Subject:' or 'Rules:' is reporting an  
      # error or exception: write it out for checking.
      # 
      if ($line !~ /(Discarded|Passed to|Quarantined|From:|Subject:|Rules:)/)
      {
         @fld = split(/\s/, $line);
         $n = @fld - 1;
         $error = join(" ", @fld[4..$n]);
	 printf("$error\n");
      }
   }
}

