]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MDA.pm
remove Email::Address dependency
[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 Date::Parse qw(strptime);
10 use constant MAX_SIZE => 1024 * 500; # same as spamc default, should be tunable
11 use constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian
12
13 our @BAD_HEADERS = (
14         # postfix
15         qw(delivered-to x-original-to), # prevent training loops
16
17         # The rest are taken from Mailman 2.1.15:
18         # could contain passwords:
19         qw(approved approve x-approved x-approve urgent),
20         # could be used phishing:
21         qw(return-receipt-to disposition-notification-to x-confirm-reading-to),
22         # Pegasus mail:
23         qw(x-pmrqc)
24 );
25
26 # drop plus addressing for matching
27 sub __drop_plus {
28         my ($str_addr) = @_;
29         $str_addr =~ s/\+.*\@/\@/;
30         $str_addr;
31 }
32
33 # do not allow Bcc, only Cc and To if recipient is set
34 sub precheck {
35         my ($klass, $filter, $address) = @_;
36         my Email::Simple $simple = $filter->simple;
37         my @mid = $simple->header('Message-ID');
38         return 0 if scalar(@mid) != 1;
39         my $mid = $mid[0];
40         return 0 if (length($mid) > MAX_MID_SIZE);
41         return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
42         return 0 unless usable_str(length('u@h'), $filter->from);
43         return 0 unless usable_str(length(':o'), $simple->header("Subject"));
44         return 0 unless usable_date($simple->header("Date"));
45         return 0 if length($simple->as_string) > MAX_SIZE;
46         alias_specified($filter, $address);
47 }
48
49 sub usable_str {
50         my ($len, $str) = @_;
51         defined($str) && length($str) >= $len;
52 }
53
54 sub usable_date {
55         my @t = eval { strptime(@_) };
56         scalar @t;
57 }
58
59 sub alias_specified {
60         my ($filter, $address) = @_;
61
62         my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
63         my %ok = map {
64                 lc(__drop_plus($_)) => 1;
65         } @address;
66
67         foreach my $line ($filter->cc, $filter->to) {
68                 my @addrs = ($line =~ /([^<\s]+\@[^>\s]+)/g);
69                 foreach my $addr (@addrs) {
70                         if ($ok{lc(__drop_plus($addr))}) {
71                                 return 1;
72                         }
73                 }
74         }
75         return 0;
76 }
77
78 sub set_list_headers {
79         my ($class, $simple, $dst) = @_;
80         unless (defined $simple->header('List-Id')) {
81                 my $pa = $dst->{-primary_address};
82                 $simple->header_set("List-Id", "<$pa>"); # RFC2919
83         }
84
85         $simple->header_set($_) foreach @BAD_HEADERS;
86 }
87
88 1;