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