]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/MDA.pm
add various TODO items
[public-inbox.git] / lib / PublicInbox / MDA.pm
index 22879236e0b76dddda14df0aa605e3cdfedbe80f..e2b2f9143083c0b1979d283fda7d2b985096e9a9 100644 (file)
@@ -4,8 +4,10 @@ package PublicInbox::MDA;
 use strict;
 use warnings;
 use Email::Address;
+use Encode qw/decode/;
 use Date::Parse qw(strptime);
-use constant MAX_SIZE => 1024 * 500; # same as spamc default
+use constant MAX_SIZE => 1024 * 500; # same as spamc default, should be tunable
+use constant cmd => qw/ssoma-mda -1/;
 
 # drop plus addressing for matching
 sub __drop_plus {
@@ -16,7 +18,7 @@ sub __drop_plus {
 
 # do not allow Bcc, only Cc and To if recipient is set
 sub precheck {
-       my ($klass, $filter, $recipient) = @_;
+       my ($klass, $filter, $address) = @_;
        my $simple = $filter->simple;
        my $mid = $simple->header("Message-ID");
        return 0 unless usable_str(length('<m@h>'), $mid) && $mid =~ /\@/;
@@ -24,7 +26,7 @@ sub precheck {
        return 0 unless usable_str(length(':o'), $simple->header("Subject"));
        return 0 unless usable_date($simple->header("Date"));
        return 0 if length($simple->as_string) > MAX_SIZE;
-       recipient_specified($filter, $recipient);
+       alias_specified($filter, $address);
 }
 
 sub usable_str {
@@ -37,20 +39,50 @@ sub usable_date {
        scalar @t;
 }
 
-sub recipient_specified {
-       my ($filter, $recipient) = @_;
-       defined($recipient) or return 1; # for mass imports
-       my @recip = Email::Address->parse($recipient);
-       my $oaddr = __drop_plus($recip[0]->address);
-       $oaddr = qr/\b\Q$oaddr\E\b/i;
-       my @to = Email::Address->parse($filter->to);
-       my @cc = Email::Address->parse($filter->cc);
-       foreach my $addr (@to, @cc) {
-               if (__drop_plus($addr->address) =~ $oaddr) {
-                       return 1;
+sub alias_specified {
+       my ($filter, $address) = @_;
+
+       my @address = ref($address) eq 'ARRAY' ? @$address : ($address);
+       my %ok = map {
+               my @recip = Email::Address->parse($_);
+               lc(__drop_plus($recip[0]->address)) => 1;
+       } @address;
+
+       foreach my $line ($filter->cc, $filter->to) {
+               foreach my $addr (Email::Address->parse($line)) {
+                       if ($ok{lc(__drop_plus($addr->address))}) {
+                               return 1;
+                       }
                }
        }
        return 0;
 }
 
+# RFC2919 and RFC2369
+sub set_list_headers {
+       my ($class, $simple, $dst) = @_;
+       my $pa = "<$dst->{-primary_address}>";
+       $simple->header_set("List-Id", $pa);
+       $simple->header_set("List-Post", $pa);
+
+       my $url = $dst->{url};
+       if (defined $url) {
+               $simple->header_set("List-Archive", "<$url>");
+               $simple->header_set("List-Help", "<${url}help>");
+       }
+}
+
+# returns a 3-element array: name, email, date
+sub author_info {
+       my ($class, $mime) = @_;
+
+       my $from = $mime->header('From');
+       my @from = Email::Address->parse($from);
+       my $name = $from[0]->name;
+       defined $name or $name = '';
+       my $email = $from[0]->address;
+       defined $email or $email = '';
+       ($name, $email, $mime->header('Date'));
+}
+
 1;