#!/usr/bin/perl

#### config ################################################

# where is the file quarantine.log?
my $log = "/home/www/alex/cgi-bin/quarantine.log";

# do you want to summerize sender/recipient statistic by name (1)
# or by domain only (0)?
my $show_sender_name = 0;
my $show_recipient_name = 1;

# how wide shall the bars be?
my $max_width = 400;

# how many entries do you want to display (0=all)?
my $max_display = {
		   date      => 12,
		   virus     => 0,
		   sender    => 20,
		   recipient => 20,
		  };

############################################################

## modules
use strict;
use CGI;
use HTML::Template;

## read template and log file, create cgi object
my $q = CGI->new();
my $tmpl_file = read_template();
my $info = read_log($log, $show_sender_name, $show_recipient_name);

## init some variables
my $total = $info->{total};

## fill template
my $template = HTML::Template->new(scalarref => \$tmpl_file);
$template->param(
		 #cgi            => $ENV{SCRIPT_NAME},
		 #message        => "",
                 server         => "mail.zeitform.de",
		 total          => $total,
		 max_date       => $max_display->{date},
		 max_virus      => $max_display->{virus},
		 max_sender     => $max_display->{sender},
		 max_recipient  => $max_display->{recipient},
		 show_sender    => $show_sender_name,
                 show_recipient => $show_recipient_name,
		);


### collect information
foreach my $type (qw(date virus sender recipient)) {

  my @loop;
  my $i=0;
  foreach (sort { $type eq "date" ? &by_date
                                  : $info->{$type}->{$b} <=> $info->{$type}->{$a} }
                keys %{$info->{$type}}) {

    last if $max_display->{$type} && $i++ >= $max_display->{$type};
    push @loop, {
		  name    => $_ || "<i>unknown</i>",
		  count   => $info->{$type}->{$_} || 0,
		  percent => percent($info->{$type}->{$_}, $total) || 0,
		  width   => width($info->{$type}->{$_}, $total) || 0,
		 };
  }
  $template->param( "${type}_loop" => [ @loop ], );
}


## output
print $q->header();
print $template->output;
exit;

############################################################
sub by_date {
############################################################

  my @a = split(/\//, $a);
  my @b = split(/\//, $b);

  $b[1] <=> $a[1] || $b[0] <=> $a[0];
}

############################################################
sub width {
############################################################

  my $a = shift || 0;
  my $b = shift || error("illegal division by 0");

  return sprintf("%d", $max_width*$a/$b+0.5); # round?
}

############################################################
sub percent {
############################################################

  my $a = shift || 0;
  my $b = shift || error("illegal division by 0");

  return sprintf("%.2f", 100*$a/$b+0.005); # round?
}

############################################################
sub read_log {
############################################################

  my $log_file = shift || error("Missing parameter \"log_file\" in sub routine call");
  my $sender_name = shift || 0;
  my $recipient_name = shift || 0;

  my $info;
  my $total = 0;

  open LOG, $log or error("Can't read $log");
  while (<LOG>) {

    my ($date, $sender, $recipient, $subject, $virus, $scanner) = split("\t", $_);
    # 0 = date and time
    # 1 = sender
    # 2 = recipient
    # 3 = subject
    # 4 = virus
    # 5 = scanner

    $total++;

    # date (just get month and year)
    $date =~ s!^\d+/(\d+/\d+).+$!$1!;
    $info->{date}->{$date}++;

    # sender
    $sender = "" if $sender =~ /^\s*$/;
    $sender =~ s/^.+@// unless $sender_name; # just get the domain
    $info->{sender}->{$sender}++;

    # recipient (splitted if more then one)
    foreach (split /,\s*/, $recipient) {
      $_ =~ s/^.+@// unless $recipient_name; # just get the domain
      $info->{recipient}->{$_}++;
    }

    # virus type (cut away whitespace, the word "the" and trailing "!!!"
    $virus =~ s/^\s*//;
    $virus =~ s/^the\s*//;
    $virus =~ s/\s*!+\s*$//;
    $info->{virus}->{$virus}++;

  }

  close LOG;
  $info->{total} = $total;
  return $info;
}


############################################################
sub error {
############################################################

  print $q->header();
  print "Fehler: ", shift;
  exit 1;

}

############################################################
sub read_template {
############################################################
  return <<'EOT';
<html>
<head>
  <title>Virus Information for <TMPL_VAR NAME=server></title>
<style type="text/css">
<!--
h1 {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 16px;
                line-height: 20px;
                font-weight: 700;
                color: #000000;
                }
p,td {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
                line-height: 18px;
                font-weight: 200;
                color: #000000; 
                }
small {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 10px;
                line-height: 15px;
                font-weight: 200;
                color: #000000; 
                }

.title {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 14px;
                line-height: 20px;
                font-weight: 700;
                color: #000000; 
                vertical-align: top;
		background-color: #CCCCCC;
                }
.right {
                text-align: right;
                }
-->
  </style>


</head>
<body>
  <h1>Analysis of Scanned Viruses for <TMPL_VAR NAME=server></h1>

  <table>

    <tr>
      <td colspan="4" class="title">&nbsp;Virus Detections listed by month
	<TMPL_IF NAME=max_date>(last <TMPL_VAR NAME=max_date> months)</TMPL_IF></td>
    </tr>
    <TMPL_LOOP NAME=date_loop>
    <tr>
      <td class="right"><TMPL_VAR NAME=name></td>
      <td width="400"><img src="/qsa/dot.gif" alt="<TMPL_VAR NAME=count>" height="8" width="<TMPL_VAR NAME=width>"></td>
      <td class="right"><TMPL_VAR NAME=count></td>
      <td class="right">(<TMPL_VAR NAME=percent>%)</td>
    </tr>
    </TMPL_LOOP>
    <tr>
      <td class="right"><b>Total</b></td>
      <td>&nbsp;</td>
      <td class="right"><b><TMPL_VAR NAME=total></b></td>
      <td class="right">&nbsp;</td>
    </tr>

    <tr>
      <td colspan="4" class="title">&nbsp;Detected Virus Types 
	<TMPL_IF NAME=max_virus>(TOP <TMPL_VAR NAME=max_virus>)</TMPL_IF></td>
    </tr>
    <TMPL_LOOP NAME=virus_loop>
    <tr>
      <td class="right"><TMPL_VAR NAME=name></td>
      <td><img src="/qsa/dot.gif" alt="<TMPL_VAR NAME=count>" height="8" width="<TMPL_VAR NAME=width>"></td>
      <td class="right"><TMPL_VAR NAME=count></td>
      <td class="right">(<TMPL_VAR NAME=percent>%)</td>
    </tr>
    </TMPL_LOOP>
    <tr>
      <td class="right"><b>Total</b></td>
      <td>&nbsp;</td>
      <td class="right"><b><TMPL_VAR NAME=total></b></td>
      <td class="right">&nbsp;</td>
    </tr>

    <tr>
      <td colspan="4" class="title">&nbsp;Obvious Virus Senders 
        <TMPL_IF NAME=show_sender>by name<TMPL_ELSE>by domain</TMPL_IF>
	<TMPL_IF NAME=max_sender>(TOP <TMPL_VAR NAME=max_sender>)</TMPL_IF></td>
    </tr>
    <TMPL_LOOP NAME=sender_loop>
    <tr>
      <td class="right"><TMPL_VAR NAME=name></td>
      <td><img src="/qsa/dot.gif" alt="<TMPL_VAR NAME=count>" height="8" width="<TMPL_VAR NAME=width>"></td>
      <td class="right"><TMPL_VAR NAME=count></td>
      <td class="right">(<TMPL_VAR NAME=percent>%)</td>
    </tr>
    </TMPL_LOOP>
    <tr>
      <td class="right"><b>Total</b></td>
      <td>&nbsp;</td>
      <td class="right"><b><TMPL_VAR NAME=total></b></td>
      <td class="right">&nbsp;</td>
    </tr>

    <tr>
      <td colspan="4" class="title">&nbsp;Designated Virus Recipients
        <TMPL_IF NAME=show_recipient>by name<TMPL_ELSE>by domain</TMPL_IF>
	<TMPL_IF NAME=max_recipient>(TOP <TMPL_VAR NAME=max_recipient>)</TMPL_IF></td>
    </tr>
    <TMPL_LOOP NAME=recipient_loop>
    <tr>
      <td class="right"><TMPL_VAR NAME=name></td>
      <td><img src="/qsa/dot.gif" alt="<TMPL_VAR NAME=count>" height="8" width="<TMPL_VAR NAME=width>"></td>
      <td class="right"><TMPL_VAR NAME=count></td>
      <td class="right">(<TMPL_VAR NAME=percent>%)</td>
    </tr>
    </TMPL_LOOP>
    <tr>
      <td class="right"><b>Total</b></td>
      <td>&nbsp;</td>
      <td class="right"><b><TMPL_VAR NAME=total></b></td>
      <td class="right">&nbsp;</td>
    </tr>

  </table>
  <small><br>&copy; 2002 <a href="http://www.zeitform.de">zeitform Internet Dienste</a> - all rights reserved</small>

</body>
</html>
EOT
}
