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