#!/usr/bin/perl -w 

=head1 NAME

transaction-results3

=head1 SYNOPSIS

transaction-results3 file ...

=head1 DESCRIPTION

Parses one or more qpsmtpd log files and creates statistics about the transaction results.

The results are printed to stdout with one record per line.
Each record contains the recipient domain name, a keyword indicating the result
(for example "queued" if the mail was accepted, "spamassassin", if it was rejected due
to exceeding the spamasssassin threshold, etc.) and the number of transaction with
this domain and result. The three fields are separated by " | ".

=cut

use strict;

use parselog;
use Data::Dumper;


for (@ARGV) {
    parse(\&save, $_);
}

my %h;

sub save {
    my ($s) = @_;
    unless ($s->{rcpts}) {
	$s->{rcpts}{unknown}{unknown} = 1;
	if (defined($s->{finalresult})) {
	    $s->{rcpts}{unknown}{finalresult} = $s->{finalresult};
	} else {
	    $s->{rcpts}{unknown}{finalresult} = '500 empty transaction';
	    #print Dumper($s);
	}
    }
    rcpt:
    for my $r (keys %{$s->{rcpts}}) {
	my $d = $r;
	$d =~ s/^.*@//;
	$d =~ s/>$//;
	$d = lc($d);

	my $key = msg2key($s->{rcpts}{$r}{finalresult});
	$h{$d}{$key}++;
    }
}


for my $d (sort keys %h) {
    for my $res (sort { $h{$d}{$b} <=> $h{$d}{$a} } keys %{$h{$d}}) {
	print "$d | $res | $h{$d}{$res}\n";
    }
}

