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