#!perl -w

=head1 NAME

parse_addr_withhelo

=head1 SYNOPSIS

strict RFC 821 forbids parameters after the

   MAIL FROM:<user@example.net>
 and
   RCPT TO:<someone@example.com>

load this plugin to enforce, else the default EHLO parsing with
parameters is done.

=cut

sub hook_mail_parse {
    my $self = shift;
    return (OK, \&_parse) if ($self->qp->connection->hello eq 'helo');
    return (DECLINED);
}

sub hook_rcpt_parse {
    my $self = shift;
    return (OK, \&_parse) if ($self->qp->connection->hello eq 'helo');
    return (DECLINED);
}

sub _parse {
    my ($self,$cmd,$line) = @_;
    $self->log(LOGDEBUG, "_parse() cmd=[$cmd], line=[$line]");
    if ($cmd eq 'mail') {
        return(DENY, "Syntax error in command")
          unless ($line =~ s/^from:\s*//i);
    }
    else { # cmd eq 'rcpt'
        return(DENY, "Syntax error in command")
          unless ($line =~ s/^to:\s*//i);
    }

    if ($line =~ s/^(<.*>)\s*//) {
        my $addr = $1;
        return (DENY, "No parameters allowed in ".uc($cmd))
          if ($line =~ /^\S/);
        return (OK, $addr, ());
    }

    ## now, no <> are given
    $line =~ s/\s*$//;
    if ($line =~ /\@/) {
        return (DENY, "No parameters allowed in ".uc($cmd))
          if ($line =~ /\@\S+\s+\S/);
        return (OK, $line, ());
    }

    if ($cmd eq "mail") {
        return (OK, "<>") unless $line; # 'MAIL FROM:' -> 'MAIL FROM:<>'
        return (DENY, "Could not parse your MAIL FROM command");
    }
    else {
        return (DENY, "Could not parse your RCPT TO command")
          unless $line =~ /^(postmaster|abuse)$/i;
    }
}