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;
8 use Date::Parse qw(strptime);
9 use constant MAX_SIZE => 1024 * 500; # same as spamc default, should be tunable
10 use constant cmd => qw/ssoma-mda -1/;
12 # drop plus addressing for matching
15 $str_addr =~ s/\+.*\@/\@/;
19 # do not allow Bcc, only Cc and To if recipient is set
21 my ($klass, $filter, $address) = @_;
22 my $simple = $filter->simple;
23 my $mid = $simple->header("Message-ID");
24 return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
25 return 0 unless usable_str(length('u@h'), $filter->from);
26 return 0 unless usable_str(length(':o'), $simple->header("Subject"));
27 return 0 unless usable_date($simple->header("Date"));
28 return 0 if length($simple->as_string) > MAX_SIZE;
29 alias_specified($filter, $address);
34 defined($str) && length($str) >= $len;
38 my @t = eval { strptime(@_) };
43 my ($filter, $address) = @_;
45 my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
47 my @recip = Email::Address->parse($_);
48 lc(__drop_plus($recip[0]->address)) => 1;
51 foreach my $line ($filter->cc, $filter->to) {
52 foreach my $addr (Email::Address->parse($line)) {
53 if ($ok{lc(__drop_plus($addr->address))}) {
61 sub set_list_headers {
62 my ($class, $simple, $dst) = @_;
63 my $pa = $dst->{-primary_address};
65 $simple->header_set("List-Id", "<$pa>"); # RFC2919
67 # remove Delivered-To: prevent training loops
68 # The rest are taken from Mailman 2.1.15, some may be used for phishing
69 foreach my $h (qw(delivered-to approved approve x-approved x-approve
70 urgent return-receipt-to disposition-notification-to
71 x-confirm-reading-to x-pmrqc)) {
72 $simple->header_set($h);
75 # Remove any "DomainKeys" (or similar) header lines.
76 # Any modifications (including List-Id) will cause a message
78 foreach my $h (qw(domainkey-signature dkim-signature
79 authentication-results)) {
80 $simple->header_set($h);
84 # returns a 3-element array: name, email, date
86 my ($class, $mime) = @_;
88 my $from = $mime->header('From');
89 my @from = Email::Address->parse($from);
90 my $name = $from[0]->name;
91 defined $name or $name = '';
92 my $email = $from[0]->address;
93 defined $email or $email = '';
94 ($name, $email, $mime->header('Date'));