#!/usr/local/bin/perl -w 
use strict;

use Imager::Graph::Pie;

my @pat = (
    [ 'queued',			'\|250 Queued', ],
    [ 'badmailfrom',		'550 Mail from .* not accepted here', ],
    [ 'mail-from-temp',		'\|450 Could not resolve ', ],
    [ 'spamcop',		'\|550 Blocked - see http://www.spamcop.net/bl.shtml', ],
    [ 'dsn.rfc-ignorant',	'\|550 Mail from .* rejected because it does not accept bounces. This violates RFC 821/2505/2821 http://www.rfc-ignorant.org/' ],
    [ 'whois',			'\|550 Inaccurate or missing WHOIS data', ],
    [ 'badrcptto',		'\|550 mail to .* not accepted here', ],
    [ 'relay',			'\|550 relaying denied', ],
    [ 'spamassassin',		'\|552 spam score exceeded', ],
    [ 'ordb',			'\|550 This mail was handled by an open relay - please visit <http://ORDB.org/lookup/', ],
    [ 'greylisting',		'\|450 This mail is temporarily denied', ],
    [ 'netcetera',		'\|550 Blocked. Contact spam@netcetera.dk Include this in the subject:' ],
    [ 'klez',			'\|552 Klez Virus Detected', ],
    [ 'no-such-user',		'\|550 no such user', ],
    [ 'early-talker',		'450 Don\'t be rude and talk before I say hello\!', ],
    [ 'queue-error',		'\|451 Unable to queue message', ],
    [ 'spamhaus',		'\|550 http://www.spamhaus.org/SBL/sbl.lasso', ],
    [ 'fqdn-required',		'\|450 FQDN required in the envelope sender', ],
    [ 'badhelo',		'\|550 Uh-huh. You\'re .* and I\'m a boil on the bottom of the Marquess of Queensbury\'s great-aunt.' ],
    [ 'message-too-big',	'\|552 Message too big' ],
    [ 'message-denied',		'\|552 Message denied' ],
);

my %h;
line: while (<>) {
    for my $pat (@pat) {
	if (/$pat->[1]/) {
	    #print "$_\tmatched $pat->[1] => $pat->[0]\n";
	    $h{$pat->[0]}++;
	    next line;
	}
    }
    $h{OTHER}++;
}

my @data;
my @labels;
for my $res (sort { $h{$b} <=> $h{$a} } keys %h) {
    print "$res $h{$res}\n";
    push @data, $h{$res};
    push @labels, $res;
}

my $chart = Imager::Graph::Pie->new;
$chart->{_style}{pie}{maxsegment} = 0.02;
my $font = Imager::Font->new(file=>'/usr/local/fonts/truetype/l_10646.ttf', aa=>1);
my $img = $chart->draw(labels 	=> \@labels,
		       data	=> \@data,
		       features	=> [ 'legend', 'labelspconly', 'outline' ],
		       pie	=> { maxsegment => 0.02 },
		       style	=> 'fount_rad',
		       font	=> $font,
		       width	=> 640,
		       height	=> 480,
		      );
$img->write(file => 'transaction-results.png');

