]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MDA.pm
add various TODO items
[public-inbox.git] / lib / PublicInbox / MDA.pm
1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
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 Encode qw/decode/;
8 use Date::Parse qw(strptime);
9 use constant MAX_SIZE => 1024 * 500; # same as spamc default, should be tunable
10 use constant cmd => qw/ssoma-mda -1/;
11
12 # drop plus addressing for matching
13 sub __drop_plus {
14         my ($str_addr) = @_;
15         $str_addr =~ s/\+.*\@/\@/;
16         $str_addr;
17 }
18
19 # do not allow Bcc, only Cc and To if recipient is set
20 sub precheck {
21         my ($klass, $filter, $address) = @_;
22         my $simple = $filter->simple;
23         my $mid = $simple->header("Message-ID");
24         return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
25         return 0 unless usable_str(length('u@h'), $filter->from);
26         return 0 unless usable_str(length(':o'), $simple->header("Subject"));
27         return 0 unless usable_date($simple->header("Date"));
28         return 0 if length($simple->as_string) > MAX_SIZE;
29         alias_specified($filter, $address);
30 }
31
32 sub usable_str {
33         my ($len, $str) = @_;
34         defined($str) && length($str) >= $len;
35 }
36
37 sub usable_date {
38         my @t = eval { strptime(@_) };
39         scalar @t;
40 }
41
42 sub alias_specified {
43         my ($filter, $address) = @_;
44
45         my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
46         my %ok = map {
47                 my @recip = Email::Address->parse($_);
48                 lc(__drop_plus($recip[0]->address)) => 1;
49         } @address;
50
51         foreach my $line ($filter->cc, $filter->to) {
52                 foreach my $addr (Email::Address->parse($line)) {
53                         if ($ok{lc(__drop_plus($addr->address))}) {
54                                 return 1;
55                         }
56                 }
57         }
58         return 0;
59 }
60
61 # RFC2919 and RFC2369
62 sub set_list_headers {
63         my ($class, $simple, $dst) = @_;
64         my $pa = "<$dst->{-primary_address}>";
65         $simple->header_set("List-Id", $pa);
66         $simple->header_set("List-Post", $pa);
67
68         my $url = $dst->{url};
69         if (defined $url) {
70                 $simple->header_set("List-Archive", "<$url>");
71                 $simple->header_set("List-Help", "<${url}help>");
72         }
73 }
74
75 # returns a 3-element array: name, email, date
76 sub author_info {
77         my ($class, $mime) = @_;
78
79         my $from = $mime->header('From');
80         my @from = Email::Address->parse($from);
81         my $name = $from[0]->name;
82         defined $name or $name = '';
83         my $email = $from[0]->address;
84         defined $email or $email = '';
85         ($name, $email, $mime->header('Date'));
86 }
87
88 1;