#!perl -w

=head1 NAME

check_bogus_bounce - Check that a bounce message isn't bogus

=head1 DESCRIPTION

This plugin is designed to reject bogus bounce messages.

In our case a bogus bounce message is defined as a bounce message
which has more than a single recipient.

=head1 CONFIGURATION

Only a single argument is recognized and is assumed to be the default
action.  Valid settings are:

=over 8

=item log

Merely log the receipt of the bogus bounce (the default behaviour).

=item deny

Deny with a hard error code.

=item denysoft

Deny with a soft error code.

=back

=head1 AUTHOR

2010 - Steve Kemp - http://steve.org.uk/Software/qpsmtpd/

=cut

=begin doc

Look for our single expected argument and configure "action" appropriately.

=end doc

=cut

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

    if ( @_ % 2 ) {
        $self->{_args}{action} = shift;
    }
    else {
        $self->{_args} = { @_ };
    };

    if ( ! defined $self->{_args}{reject} ) {
        $self->{_args}{reject} = 0;     # legacy default
    };

    # we only need to check for deferral, default is DENY
    if ( $self->{_args}{action} =~ /soft/i ) {
        $self->{_args}{reject_type} = 'temp';
    }
}

=begin doc

Handle the detection of bounces here.

If we find a match then we'll react with our expected action.

=end doc

=cut

sub hook_data_post {
    my ($self, $transaction) = (@_);

    #
    # Find the sender, and return unless it wasn't a bounce.
    #
    my $sender = $transaction->sender->address || undef;
    if ( $sender && $sender ne '<>') {
        $self->log(LOGINFO, "pass, not a null sender");
        return DECLINED;
    };

    #
    #  Get the recipients.
    #
    my @to = $transaction->recipients || ();
    if (scalar @to == 1) {
        $self->log(LOGINFO, "pass, only 1 recipient");
        return DECLINED;
    };

    #
    #  at this point we know:
    #
    #   1.  It is a bounce, via the null-envelope.
    #   2.  It is a bogus bounce, because there are more than one recipients.
    #
    $self->log(LOGINFO, "fail, bogus bounce for :" . join(',', @to));

    $self->get_reject( "fail, this is a bogus bounce" );
}