#!/usr/bin/perl -w
##########################################################################
# $Id: amavis,v 1.4 2003/11/07 01:20:26 kirk Exp $
##########################################################################

########################################################
# This was written and is maintained by:
#    Jim O'Halloran <jim@kendle.com.au>
#
# Please send all comments, suggestions, bug reports,
#    etc, to jim@kendle.com.au.
########################################################

$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'};
$CleanMsgs = 0;
$InfectedMsgs = 0;
$BannedNames = 0;

# Parse logfile
while (defined($ThisLine = <STDIN>)) {
   $ThisLine =~ s/^\([\d]+\) //;
   
   if ( ($ThisLine =~ /^do_ascii/) 
        or ($ThisLine =~ /^Found av scanner/) 
        or ($ThisLine =~ /^Found \$[\S]+[\s]+at/)  
        or ($ThisLine =~ /^No \$[\S]+,[\s]+not using it/) 
        or ($ThisLine =~ /^starting.  amavisd at/) ) {
       # We don't care about these
   } elsif ($ThisLine =~ /^Passed, /) {
      $CleanMsgs++;
   } elsif (($FileName, $From) = ( $ThisLine =~ /^BANNED name\/type \(([^\)]+)\)\, \<([^\>]+)\>/ )) { 
      $BannedNames++;

      if ($Detail == 10) {
          $Banned{$FileName}{$From}++;
      }; # if 

   } elsif (($Virus, $From) = ( $ThisLine =~ /^INFECTED \(([^\)]+)\)\, [\(\<]([^\>\)]+)[\)\>]/ )) {

      $InfectedMsgs++;

      if ($Detail == 9) {
        $Viruses{$Virus}++;
      }; # if

      if ($Detail == 10) {
        $Viruses{$Virus}{$From}++;
      }; # if

   } # elsif
   else {
      # Report any unmatched entries...
      chomp($ThisLine);
      $OtherList{$ThisLine}++;        
   } # else
} # while


# Output report
if ($CleanMsgs > 0) {
   print "\n$CleanMsgs messages checked and passed.\n";
}; # if 

if ($InfectedMsgs > 0) {
   print "$InfectedMsgs virus infected messages were found.\n";
}; # if

if ($BannedNames > 0) {
   print "$BannedNames messages rejected with banned file names.\n";
}; # if

if ((keys %Viruses)) {
   print "\nViruses Detected:\n";
   foreach $Virus (sort keys %Viruses) {
      if ($Detail == 9) {
         print "   $Virus: $Viruses{$Virus} Times(s)\n";
      } else {
         $VirCount = 0;
         $OutString = "";
         foreach $From (sort keys %{ $Viruses{$Virus}}) {
            $VirCount += $Viruses{$Virus}{$From};
            $OutString .= "      $From  $Viruses{$Virus}{$From} Time(s)\n";
         }; # foreach
         print "   $Virus: $VirCount Times(s)  From:\n$OutString\n";
      }; # if 
   }; # foreach

}; # if

if (keys %Banned) {
  print "\nBanned File Names:\n";
   foreach $FileName (sort keys %Banned) {
      $BanCount = 0;
      $OutString = "";
      foreach $From (sort keys %{ $Banned{$FileName}}) {
         $BanCount += $Banned{$FileName}{$From};
         $OutString .= "      $From  $Banned{$FileName}{$From} Time(s)\n";
      }; # foreach
      print "   $FileName: $BanCount Times(s)  From:\n$OutString\n";
   }; # foreach
}; # if


if (keys %OtherList) {
   print "\n\n**Unmatched Entries**\n";
   foreach $line (sort {$OtherList{$b}<=>$OtherList{$a} } keys %OtherList) {
      print "   $line: $OtherList{$line} Time(s)\n";
   }; # foreach
}; # if

exit(0);

