#!/usr/bin/perl -w
###########################################################################
#
#      exim.pl
#
# Copyright (c) 2003 Roman Festchook <roma@polesye.net>
# http://noc.polesye.net/greylist/
#
# Really quick and dirty perl script to implement Greylist antispam tech 
# (http://projects.puremagic.com/greylisting/) to the Exim.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2 from
# June 1991 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
###########################################################################
# ChangeLog:
#
# 26/06/03 first release
#
###########################################################################


use strict;
use DBI;

sub greylist() {
my $lp = Exim::expand_string('$local_part');
my $sender_addr = Exim::expand_string('$sender_address');
my $host = Exim::expand_string('$sender_host_address');

# put your mysql connect options here
my $mydb = "mydb";
my $myhost = "myhost.com";
my $myuser = "myuser";
my $mypasswd = "mypass";



my $date = localtime;
my $LOGFILE = "/var/log/isp";

open(FILE, ">>$LOGFILE");



my $isp= DBI->connect("DBI:mysql:$mydb:$myhost", "$myuser", "$mypasswd") || print FILE "$date Can't connect\n";

my $sth = $isp->prepare("select UNIX_TIMESTAMP(block_expires)-UNIX_TIMESTAMP() from greylist.relaytofrom where rcpt_to='$lp' and mail_from='$sender_addr' and relay_ip='$host'")
  || print FILE "Can't prepare retrieving dates";
$sth->execute || print FILE "Can't execute retrieving dates";
my @status_array = $sth->fetchrow_array;
$sth->finish;

unless (@status_array) {
$sth = $isp->prepare("insert into greylist.relaytofrom values (NULL, '$host', '$sender_addr', '$lp', DATE_ADD(NOW()+0, INTERVAL 55 MINUTE), DATE_ADD(NOW()+0, INTERVAL 36 DAY), 1, 0, 0, 'AUTO', NOW()+0, NULL)")
  || print FILE "Can't prepare inserting triplet\n";
$sth->execute || print FILE "Can't execute inserting triplet [".$sth->errstr()."]\n";
close(FILE);
$isp->disconnect;
return "fail";
} else {
    if ($status_array[0]>0) {
    $sth = $isp->prepare("update greylist.relaytofrom set blocked_count=blocked_count+1 where rcpt_to='$lp' and mail_from='$sender_addr' and relay_ip='$host'")
  || print FILE "Can't prepare retrieving dates";
$sth->execute || print FILE "Can't execute retrieving dates";
    close(FILE);
    $isp->disconnect;
    return "fail";
    } else {
    $sth = $isp->prepare("update greylist.relaytofrom set passed_count=passed_count+1,  record_expires=DATE_ADD(NOW()+0, INTERVAL 36 DAY) where rcpt_to='$lp' and mail_from='$sender_addr' and relay_ip='$host'")
	|| print FILE "Can't prepare retrieving dates";
	$sth->execute || print FILE "Can't execute retrieving dates";
    }
}

    close(FILE);
    $isp->disconnect;
return "yes";
}
