From b906f671237ade1278abeff7c1baac6dac4193ad Mon Sep 17 00:00:00 2001
From: Matt Sergeant <matt@sergeant.org>
Date: Mon, 20 Jun 2005 18:46:38 +0000
Subject: [PATCH] Ported to support Apache::Qpsmtpd

git-svn-id: https://svn.perl.org/qpsmtpd/trunk@439 958fd67b-6ff1-0310-b445-bb7760255be9
---
 plugins/check_earlytalker | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/plugins/check_earlytalker b/plugins/check_earlytalker
index b44192b..f21748b 100644
--- a/plugins/check_earlytalker
+++ b/plugins/check_earlytalker
@@ -62,12 +62,46 @@ sub register {
         'defer-reject' => 0,
         @args,
   };
-  $self->register_hook('connect', 'connect_handler');
+  if ($qp->{conn} && $qp->{conn}->isa('Apache2::Connection')) {
+      require APR::Const;
+      APR::Const->import(qw(POLLIN SUCCESS));
+      $self->register_hook('connect', 'apr_connect_handler');
+  }
+  else {
+      $self->register_hook('connect', 'connect_handler');
+  }
   $self->register_hook('mail', 'mail_handler')
     if $self->{_args}->{'defer-reject'};
   1;
 }
 
+sub apr_connect_handler {
+    my ($self, $transaction) = @_;
+    
+    return DECLINED if ($self->qp->connection->notes('whitelistclient'));
+    my $ip = $self->qp->connection->remote_ip;
+
+    my $c = $self->qp->{conn};
+    my $socket = $c->client_socket;
+    my $timeout = $self->{_args}->{'wait'} * 1_000_000;
+    
+    my $rc = $socket->poll($c->pool, $timeout, APR::Const::POLLIN());
+    if ($rc == APR::Const::SUCCESS()) {
+        $self->log(LOGNOTICE, "remote host started talking before we said hello [$ip]");
+        if ($self->{_args}->{'defer-reject'}) {
+            $self->qp->connection->notes('earlytalker', 1);
+        }
+        else {
+            my $msg = 'Connecting host started transmitting before SMTP greeting';
+            return (DENY,$msg) if $self->{_args}->{'action'} eq 'deny';
+            return (DENYSOFT,$msg) if $self->{_args}->{'action'} eq 'denysoft';
+        }
+    }
+    else {
+        $self->log(LOGINFO, "remote host said nothing spontaneous, proceeding");
+    }
+}
+
 sub connect_handler {
   my ($self, $transaction) = @_;
   my $in = new IO::Select;