#!/usr/bin/perl

=head1 NAME

badsubject - Block messages based upon their subjects.

=head1 DESCRIPTION

This plugin will reject messages based upon a blacklist
located in the file /etc/qpsmtpd/badsubject.

=cut

=head1 LICENSE

Submitted to the public domain.

=cut

=head1 AUTHOR

Steve Kemp

http://www.steve.org.uk/Software/qpsmtpd/

=cut


use strict;
use warnings;

use Qpsmtpd::Constants;




=begin doc

  The handler which is called when the message is received.

=end doc

=cut


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

    #
    #  Get the subject of the messages.
    #
    my $subject = $transaction->header->get("Subject") || undef;

    #
    #  No subject?  Ignore.
    #
    return DECLINED unless ( defined($subject) && length($subject) );

    #
    # strip the trailing newline.
    #
    $subject =~ s/[\r\n]//g;

    #
    #  Load the patterns
    #
    my @patterns = $self->qp->config("badsubject") or
      return (DECLINED);


    #
    #  For each pattern see if we get a match.
    #
    foreach my $pattern (@patterns)
    {

        #
        # skip empty patterns, or those starting with '#'.
        #
        next
          if ( ( !length($pattern) ) ||
               ( $pattern =~ /^([ \t]+)$/ ) ||
               ( $pattern =~ /^#/ ) );


        #
        #  If matched reject..
        #
        if ( $subject =~ /\Q$pattern\E/i )
        {
            $self->log( LOGWARN, "badsubject:$subject subject-match:$pattern" );
            return ( DENY, "badsubject: $subject" );
        }
    }

    #
    #  No match: Log the subject in case it is clearly bogus and should
    # be dropped
    #
    $self->log( LOGWARN, "Subject: $subject" );

    return (DECLINED);
}