#!perl -w

=head1 NAME

count_unrecognized_commands - and disconnect after too many

=head1 DESCRIPTION

Disconnect the client if it sends too many unrecognized commands.
Good for rejecting spam sent through open HTTP proxies.

=head1 CONFIGURATION

Takes one parameter, the number of allowed unrecognized commands
before we disconnect the client.  Defaults to 4.

=cut

use strict;
use warnings;

use Qpsmtpd::Constants;

sub register {
    my ($self, $qp ) = (shift, shift);

    $self->{_unrec_cmd_max} = shift || 4;

    if ( scalar @_ ) {
        $self->log(LOGWARN, "Ignoring additional arguments.");
    }
}

sub hook_unrecognized_command {
  my ($self, $cmd) = @_[0,2];
  
    my $count = $self->connection->notes('unrec_cmd_count') || 0;
       $count = $count + 1;
                $self->connection->notes('unrec_cmd_count', $count);

    if ( $count < $self->{_unrec_cmd_max} ) {
        $self->log(LOGINFO, "'$cmd', ($count)");
        return DECLINED;
    };

    $self->log(LOGINFO, "fail, '$cmd' ($count)");
    return (DENY_DISCONNECT, "Closing connection, $count unrecognized commands. Perhaps you should read RFC 2821?" );
}