#!/usr/bin/perl
use strict;
use DBI;

my $DATABASE="greylist";

my $greylist_dbh = DBI->connect("DBI:mysql:database=$DATABASE;host=localhost","greylist","YOURPASSWORD",
        { RaiseError => 1, AutoCommit => 0 })
        or die $DBI::errstr;

my $sql = qq{ SELECT id, recipient, sender, ip, created, modified, reset, accepted, count from greylist_data
           WHERE sender like ? OR recipient like ? OR ip like ? };
my $sth = $greylist_dbh->prepare($sql);

my $arg = $ARGV[0];

$sth->execute($arg,$arg,$arg);

my ($id, $recipient, $sender, $ip, $created, $modified, $reset, $accepted, $count);
$sth->bind_columns(\$id, \$recipient, \$sender, \$ip, \$created, \$modified, \$reset, \$accepted, \$count);

my $count=0;
while($sth->fetch) {
	print "id, recipient, sender, ip, created, modified, reset, accepted, count\n\n" if $count == 0;
	$count++;

	print "$id, $recipient, $sender, $ip, " . localtime($created) . ", " . localtime($modified) . ", " . localtime($reset) . ", " . localtime($accepted) . ", $count\n";
}
$sth->finish();
$greylist_dbh->disconnect();
print("Printed $count records.\n") if ($count);
