1 # Copyright (C) 2013-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # For the -mda script (mail delivery agent)
5 package PublicInbox::MDA;
10 use Date::Parse qw(strptime);
11 use constant MAX_SIZE => 1024 * 500; # same as spamc default, should be tunable
12 use constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian
16 qw(delivered-to x-original-to), # prevent training loops
18 # The rest are taken from Mailman 2.1.15:
19 # could contain passwords:
20 qw(approved approve x-approved x-approve urgent),
21 # could be used phishing:
22 qw(return-receipt-to disposition-notification-to x-confirm-reading-to),
27 # drop plus addressing for matching
30 $str_addr =~ s/\+.*\@/\@/;
34 # do not allow Bcc, only Cc and To if recipient is set
36 my ($klass, $filter, $address) = @_;
37 my Email::Simple $simple = $filter->simple;
38 my @mid = $simple->header('Message-ID');
39 return 0 if scalar(@mid) != 1;
41 return 0 if (length($mid) > MAX_MID_SIZE);
42 return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
43 return 0 unless usable_str(length('u@h'), $filter->from);
44 return 0 unless usable_str(length(':o'), $simple->header("Subject"));
45 return 0 unless usable_date($simple->header("Date"));
46 return 0 if length($simple->as_string) > MAX_SIZE;
47 alias_specified($filter, $address);
52 defined($str) && length($str) >= $len;
56 my @t = eval { strptime(@_) };
61 my ($filter, $address) = @_;
63 my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
65 my @recip = Email::Address->parse($_);
66 lc(__drop_plus($recip[0]->address)) => 1;
69 foreach my $line ($filter->cc, $filter->to) {
70 foreach my $addr (Email::Address->parse($line)) {
71 if ($ok{lc(__drop_plus($addr->address))}) {
79 sub set_list_headers {
80 my ($class, $simple, $dst) = @_;
81 unless (defined $simple->header('List-Id')) {
82 my $pa = $dst->{-primary_address};
83 $simple->header_set("List-Id", "<$pa>"); # RFC2919
86 $simple->header_set($_) foreach @BAD_HEADERS;