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