#!/usr/bin/perl

=pod

=head1 SYNOPSIS

This plugin checks the badmailfrom_patterns config. This allows
special patterns to be denied (e.g. FQDN-VERP, percent hack, bangs,
double ats).

=head1 CONFIG

Configuration is placed in the following file:

F<config/badmailfrom_patterns>

Patterns are stored in the format pattern\sresponse, where pattern
is a Perl pattern expression. Don't forget to anchor the pattern
(front ^ and back $) if you want to restrict it from matching
anywhere in the string.

 ^streamsendbouncer@.*\.mailengine1\.com$    Your right-hand side VERP doesn't fool me
 ^return.*@.*\.pidplate\.biz$                I don' want it regardless of subdomain
 ^admin.*\.ppoonn400\.com$

=head1 AUTHOR

Johan Almqvist <johan-qpsmtpd@almqvist.net> based on L<check_badmailfrom>

This software is free software and may be distributed under the same
terms as qpsmtpd itself.

=cut

sub hook_mail {
    my ($self, $transaction, $sender, %param) = @_;

    my @badmailfrom = $self->qp->config("badmailfrom_patterns")
      or return (DECLINED);

    return (DECLINED) if ($sender->format eq "<>");

    my $host = lc $sender->host;
    my $from = lc($sender->user) . '@' . $host;

    for (@badmailfrom) {
        my ($pattern, $response) = split /\s+/, $_, 2;
        next unless $from =~ /$pattern/;
        $response = "Your envelope sender is in my badmailfrom_patterns list"
          unless $response;
        $transaction->notes('badmailfrom_patterns', $response);
    }
    return (DECLINED);
}

sub hook_rcpt {
    my ($self, $transaction, $rcpt, %param) = @_;
    my $note = $transaction->notes('badmailfrom_patterns');
    if ($note) {
        $self->log(LOGINFO, $note);
        return (DENY, $note);
    }
    return (DECLINED);
}