1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::MDA;
7 use Date::Parse qw(strptime);
8 use constant MAX_SIZE => 1024 * 500; # same as spamc default, should be tunable
9 use constant cmd => qw/ssoma-mda -1/;
11 # drop plus addressing for matching
14 $str_addr =~ s/\+.*\@/\@/;
18 # do not allow Bcc, only Cc and To if recipient is set
20 my ($klass, $filter, $address) = @_;
21 my $simple = $filter->simple;
22 my $mid = $simple->header("Message-ID");
23 return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
24 return 0 unless usable_str(length('u@h'), $filter->from);
25 return 0 unless usable_str(length(':o'), $simple->header("Subject"));
26 return 0 unless usable_date($simple->header("Date"));
27 return 0 if length($simple->as_string) > MAX_SIZE;
28 alias_specified($filter, $address);
33 defined($str) && length($str) >= $len;
37 my @t = eval { strptime(@_) };
42 my ($filter, $address) = @_;
44 my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
46 my @recip = Email::Address->parse($_);
47 lc(__drop_plus($recip[0]->address)) => 1;
50 foreach my $line ($filter->cc, $filter->to) {
51 foreach my $addr (Email::Address->parse($line)) {
52 if ($ok{lc(__drop_plus($addr->address))}) {
60 sub set_list_headers {
61 my ($class, $simple, $dst) = @_;
62 my $pa = $dst->{-primary_address};
64 $simple->header_set("List-Id", "<$pa>"); # RFC2919
66 # remove Delivered-To: prevent training loops
67 # The rest are taken from Mailman 2.1.15, some may be used for phishing
68 foreach my $h (qw(delivered-to approved approve x-approved x-approve
69 urgent return-receipt-to disposition-notification-to
70 x-confirm-reading-to x-pmrqc)) {
71 $simple->header_set($h);
74 # Remove any "DomainKeys" (or similar) header lines.
75 # Any modifications (including List-Id) will cause a message
77 foreach my $h (qw(domainkey-signature dkim-signature
78 authentication-results)) {
79 $simple->header_set($h);
83 # returns a 3-element array: name, email, date
85 my ($class, $mime) = @_;
87 my $from = $mime->header('From');
88 my @from = Email::Address->parse($from);
89 my $name = $from[0]->name;
90 my $email = $from[0]->address;
91 ($name, $email, $mime->header('Date'));