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