]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MDA.pm
replace most uses of PublicInbox::MIME with Eml
[public-inbox.git] / lib / PublicInbox / MDA.pm
1 # Copyright (C) 2013-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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 PublicInbox::MsgTime;
9 use constant MAX_SIZE => 1024 * 500; # same as spamc default, should be tunable
10 use constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian
11
12 our @BAD_HEADERS = (
13         # postfix
14         qw(delivered-to x-original-to), # prevent training loops
15
16         # The rest are taken from Mailman 2.1.15:
17         # could contain passwords:
18         qw(approved approve x-approved x-approve urgent),
19         # could be used phishing:
20         qw(return-receipt-to disposition-notification-to x-confirm-reading-to),
21         # Pegasus mail:
22         qw(x-pmrqc)
23 );
24
25 # drop plus addressing for matching
26 sub __drop_plus {
27         my ($str_addr) = @_;
28         $str_addr =~ s/\+.*\@/\@/;
29         $str_addr;
30 }
31
32 # do not allow Bcc, only Cc and To if recipient is set
33 sub precheck {
34         my ($klass, $simple, $address) = @_;
35         my @mid = $simple->header('Message-ID');
36         return 0 if scalar(@mid) != 1;
37         my $mid = $mid[0];
38         return 0 if (length($mid) > MAX_MID_SIZE);
39         return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
40         return 0 unless usable_str(length('u@h'), $simple->header("From"));
41         return 0 unless usable_str(length(':o'), $simple->header("Subject"));
42         return 0 unless usable_date($simple->header("Date"));
43         return 0 if length($simple->as_string) > MAX_SIZE;
44         alias_specified($simple, $address);
45 }
46
47 sub usable_str {
48         my ($len, $str) = @_;
49         defined($str) && length($str) >= $len;
50 }
51
52 sub usable_date {
53         defined(eval { PublicInbox::MsgTime::str2date_zone($_[0]) });
54 }
55
56 sub alias_specified {
57         my ($simple, $address) = @_;
58
59         my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
60         my %ok = map {
61                 lc(__drop_plus($_)) => 1;
62         } @address;
63
64         foreach my $line ($simple->header('Cc'), $simple->header('To')) {
65                 my @addrs = ($line =~ /([^,<\s]+\@[^,>\s]+)/g);
66                 foreach my $addr (@addrs) {
67                         if ($ok{lc(__drop_plus($addr))}) {
68                                 return 1;
69                         }
70                 }
71         }
72         return 0;
73 }
74
75 sub set_list_headers {
76         my ($class, $simple, $dst) = @_;
77         unless (defined $simple->header('List-Id')) {
78                 my $pa = $dst->{-primary_address};
79                 $pa =~ tr/@/./; # RFC2919
80                 $simple->header_set("List-Id", "<$pa>");
81         }
82 }
83
84 sub inboxes_for_list_id ($$) {
85         my ($klass, $config, $simple) = @_;
86
87         # newer Email::Simple allows header_raw, as does Email::MIME:
88         my @list_ids = $simple->can('header_raw') ?
89                         $simple->header_raw('List-Id') :
90                         $simple->header('List-Id');
91         my @dests;
92         for my $list_id (@list_ids) {
93                 $list_id =~ /<[ \t]*(.+)?[ \t]*>/ or next;
94                 if (my $ibx = $config->lookup_list_id($1)) {
95                         push @dests, $ibx;
96                 }
97         }
98         if (scalar(@list_ids) > 1) {
99                 warn "W: multiple List-IDs in message:\n";
100                 warn "W: List-ID: $_\n" for @list_ids
101         }
102         \@dests;
103 }
104
105 1;