#!/usr/bin/perl

use DB_File::Lock;
use Fcntl qw(:flock O_RDWR O_CREAT) ;

$database_ref="/var/mta/liste_blanche.db";
$log="/var/log/maillog";

#
#	We are renewing old timestamp in DB
#

tie(%db_ref, 'DB_File::Lock', $database_ref,O_CREAT|O_RDWR, 0644, $DB_BTREE, 'write' ) || die "Erreur d ecriture de $database_ref";

open(LOG,$log) or die "Cannot open file $log: $!\n";
while (<LOG>)
	{
	chop;
	#
	#	If this is a whitelisted line
	#
	if (/whitelisted.*key=.*timestamp=/)
		{
		($dump,$dump,$dump,$dump,$dump,$dump,$dump,$key,$timestamp,$dump)=split();
		$key=~ s/key=//;
		$key=~ s/,//;
		$timestamp=~ s/timestamp=//;
		$db_ref{$key}=$timestamp;
		}
	}
close(LOG);

#
#	We are removing old and known spammy sessions
#
$now=time();
$oldest_allowed=$now - (36 * 24 * 3600);
foreach (keys (%db_ref))
	{
	$key=$_;
	$timestamp=$db_ref{$key};
	if ($timestamp < $oldest_allowed)
		{
		delete $db_ref{$key};
		}
	elsif ($key =~ /(yandex\.ru)|(\.moosq\.com)|(inet.microsoft\.com)|(linux\.site)|(radar\.axi\.fr)/)
		{
		delete $db_ref{$key};
		}
	}

untie(%db_ref);
