]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MDA.pm
e1207b56e3aad365f9989c73bd72fd38bc7e3d12
[public-inbox.git] / lib / PublicInbox / MDA.pm
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)
3 #
4 # For the -mda script (mail delivery agent)
5 package PublicInbox::MDA;
6 use strict;
7 use warnings;
8 use Email::Simple;
9 use Email::Address;
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
14 our @BAD_HEADERS = (
15         # postfix
16         qw(delivered-to x-original-to), # prevent training loops
17
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),
23         # Pegasus mail:
24         qw(x-pmrqc)
25 );
26
27 # drop plus addressing for matching
28 sub __drop_plus {
29         my ($str_addr) = @_;
30         $str_addr =~ s/\+.*\@/\@/;
31         $str_addr;
32 }
33
34 # do not allow Bcc, only Cc and To if recipient is set
35 sub precheck {
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;
40         my $mid = $mid[0];
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);
48 }
49
50 sub usable_str {
51         my ($len, $str) = @_;
52         defined($str) && length($str) >= $len;
53 }
54
55 sub usable_date {
56         my @t = eval { strptime(@_) };
57         scalar @t;
58 }
59
60 sub alias_specified {
61         my ($filter, $address) = @_;
62
63         my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
64         my %ok = map {
65                 my @recip = Email::Address->parse($_);
66                 lc(__drop_plus($recip[0]->address)) => 1;
67         } @address;
68
69         foreach my $line ($filter->cc, $filter->to) {
70                 foreach my $addr (Email::Address->parse($line)) {
71                         if ($ok{lc(__drop_plus($addr->address))}) {
72                                 return 1;
73                         }
74                 }
75         }
76         return 0;
77 }
78
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
84         }
85
86         $simple->header_set($_) foreach @BAD_HEADERS;
87 }
88
89 1;