#!/usr/bin/perl -w

# Dominik Heeg @ 29.10.2010 | rewrite from 2008/Debian Sarge for using DBI instead of Mysql.pm
# a simple script to realize POP Before SMTP Auth with Plesk and spamdyke

$mysql_host = "localhost";
$mysql_db = "psa";
$mysql_user = "admin";
$mysql_pw = `cat /etc/psa/.psa.shadow | tr -d "\n"`;
$table_file = "/var/qmail/spamdyke/pop-before-smtp";

# Optional lines for making the script quite
#open STDIN, '/dev/null' or die ERROR("Can't find /dev/null: $!");
#open STDOUT, '>/dev/null';
#open STDERR, '>/dev/null';

#
## Main ##
#
use DBI;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init( { level => $DEBUG,
                            file    => ">>/var/log/pop-before-smtp.log" } );

$dbh = DBI->connect( "dbi:mysql:$mysql_db:$mysql_host", "$mysql_user", "$mysql_pw") || die ERROR("Can't connect to the MySQL Server: $DBI::errstr");
$sql = qq{SELECT DISTINCT ip_address FROM smtp_poplocks};
$sth = $dbh->prepare( $sql );
$sth->execute() || die ERROR("Can't execute MySQL Query: $DBI::errstr");

system("rm $table_file");
open(FILE,">> $table_file");
while (@sqlout = $sth->fetchrow_array()) {
 foreach(@sqlout) {
  print FILE "$_\n";
 }
}
close(FILE);

$sth->finish();
$dbh->disconnect();
exit();

