]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MDA.pm
remove ssoma 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 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
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 Email::Simple $simple = $filter->simple;
25         my @mid = $simple->header('Message-ID');
26         return 0 if scalar(@mid) != 1;
27         my $mid = $mid[0];
28         return 0 if (length($mid) > MAX_MID_SIZE);
29         return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
30         return 0 unless usable_str(length('u@h'), $filter->from);
31         return 0 unless usable_str(length(':o'), $simple->header("Subject"));
32         return 0 unless usable_date($simple->header("Date"));
33         return 0 if length($simple->as_string) > MAX_SIZE;
34         alias_specified($filter, $address);
35 }
36
37 sub usable_str {
38         my ($len, $str) = @_;
39         defined($str) && length($str) >= $len;
40 }
41
42 sub usable_date {
43         my @t = eval { strptime(@_) };
44         scalar @t;
45 }
46
47 sub alias_specified {
48         my ($filter, $address) = @_;
49
50         my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
51         my %ok = map {
52                 my @recip = Email::Address->parse($_);
53                 lc(__drop_plus($recip[0]->address)) => 1;
54         } @address;
55
56         foreach my $line ($filter->cc, $filter->to) {
57                 foreach my $addr (Email::Address->parse($line)) {
58                         if ($ok{lc(__drop_plus($addr->address))}) {
59                                 return 1;
60                         }
61                 }
62         }
63         return 0;
64 }
65
66 sub set_list_headers {
67         my ($class, $simple, $dst) = @_;
68         unless (defined $simple->header('List-Id')) {
69                 my $pa = $dst->{-primary_address};
70                 $simple->header_set("List-Id", "<$pa>"); # RFC2919
71         }
72
73         foreach my $h (qw(delivered-to), # prevent training loops
74                         # The rest are taken from Mailman 2.1.15
75                         # could contain passwords:
76                         qw(approved approve x-approved x-approve urgent),
77                         # could be used phishing:
78                         qw(return-receipt-to disposition-notification-to
79                            x-confirm-reading-to),
80                         # Pegasus mail:
81                         qw(x-pmrqc)) {
82                 $simple->header_set($h);
83         }
84 }
85
86 1;