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;
7 use Encode qw/decode encode/;
8 use Date::Parse qw(strptime);
9 use constant MAX_SIZE => 1024 * 500; # same as spamc default
10 use constant cmd => qw/ssoma-mda -1/;
12 # drop plus addressing for matching
15 $str_addr =~ s/\+.*\@/\@/;
19 # do not allow Bcc, only Cc and To if recipient is set
21 my ($klass, $filter, $recipient) = @_;
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 recipient_specified($filter, $recipient);
34 defined($str) && length($str) >= $len;
38 my @t = eval { strptime(@_) };
42 sub recipient_specified {
43 my ($filter, $recipient) = @_;
44 defined($recipient) or return 1; # for mass imports
45 my @recip = Email::Address->parse($recipient);
46 my $oaddr = __drop_plus($recip[0]->address);
47 $oaddr = qr/\b\Q$oaddr\E\b/i;
48 my @to = Email::Address->parse($filter->to);
49 my @cc = Email::Address->parse($filter->cc);
50 foreach my $addr (@to, @cc) {
51 if (__drop_plus($addr->address) =~ $oaddr) {
59 sub set_list_headers {
60 my ($class, $simple, $dst) = @_;
61 my $pa = "<$dst->{-primary_address}>";
62 $simple->header_set("List-Id", $pa);
63 $simple->header_set("List-Post", $pa);
65 my $url = $dst->{url};
67 $simple->header_set("List-Archive", "<$url>");
68 $simple->header_set("List-Help", "<${url}help>");
72 # returns a 3-element array: name, email, date
74 my ($class, $simple) = @_;
76 my $from = decode('MIME-Header', $simple->header('From'));
77 $from = encode('utf8', $from);
78 my @from = Email::Address->parse($from);
79 my $name = $from[0]->name;
80 defined $name or $name = '';
81 my $email = $from[0]->address;
82 defined $email or $email = '';
83 ($name, $email, $simple->header('Date'));