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
13 use constant cmd => qw/ssoma-mda -1/;
15 # drop plus addressing for matching
18 $str_addr =~ s/\+.*\@/\@/;
22 # do not allow Bcc, only Cc and To if recipient is set
24 my ($klass, $filter, $address) = @_;
25 my Email::Simple $simple = $filter->simple;
26 my $mid = $simple->header("Message-ID");
27 return 0 if (length($mid) > MAX_MID_SIZE);
28 return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
29 return 0 unless usable_str(length('u@h'), $filter->from);
30 return 0 unless usable_str(length(':o'), $simple->header("Subject"));
31 return 0 unless usable_date($simple->header("Date"));
32 return 0 if length($simple->as_string) > MAX_SIZE;
33 alias_specified($filter, $address);
38 defined($str) && length($str) >= $len;
42 my @t = eval { strptime(@_) };
47 my ($filter, $address) = @_;
49 my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
51 my @recip = Email::Address->parse($_);
52 lc(__drop_plus($recip[0]->address)) => 1;
55 foreach my $line ($filter->cc, $filter->to) {
56 foreach my $addr (Email::Address->parse($line)) {
57 if ($ok{lc(__drop_plus($addr->address))}) {
65 sub set_list_headers {
66 my ($class, $simple, $dst) = @_;
67 my $pa = $dst->{-primary_address};
69 $simple->header_set("List-Id", "<$pa>"); # RFC2919
71 # remove Delivered-To: prevent training loops
72 # The rest are taken from Mailman 2.1.15, some may be used for phishing
73 foreach my $h (qw(delivered-to approved approve x-approved x-approve
74 urgent return-receipt-to disposition-notification-to
75 x-confirm-reading-to x-pmrqc)) {
76 $simple->header_set($h);
80 # returns a 3-element array: name, email, date
82 my ($class, $mime) = @_;
84 my $from = $mime->header('From');
85 my @from = Email::Address->parse($from);
86 my $name = $from[0]->name;
87 my $email = $from[0]->address;
88 ($name, $email, $mime->header('Date'));