]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox.pm
b05fd8c5607853af1050054162818b6ecbdd4706
[public-inbox.git] / lib / PublicInbox.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;
4 use strict;
5 use warnings;
6 use Email::Address;
7 use constant MAX_SIZE => 1024 * 500; # same as spamc default
8
9 # drop plus addressing for matching
10 sub __drop_plus {
11         my ($str_addr) = @_;
12         $str_addr =~ s/\+.*\@/\@/;
13         $str_addr;
14 }
15
16 # do not allow Bcc, only Cc and To if recipient is set
17 sub precheck {
18         my ($klass, $filter, $recipient) = @_;
19         my $simple = $filter->simple;
20         return 0 unless $simple->header("Message-ID");
21         return 0 unless defined($filter->from);
22         return 0 unless $simple->header("Subject");
23         return 0 if length($simple->as_string) > MAX_SIZE;
24         recipient_specified($filter, $recipient);
25 }
26
27 sub recipient_specified {
28         my ($filter, $recipient) = @_;
29         defined($recipient) or return 1; # for mass imports
30         my @recip = Email::Address->parse($recipient);
31         my $oaddr = __drop_plus($recip[0]->address);
32         $oaddr = qr/\b\Q$oaddr\E\b/i;
33         my @to = Email::Address->parse($filter->to);
34         my @cc = Email::Address->parse($filter->cc);
35         foreach my $addr (@to, @cc) {
36                 if (__drop_plus($addr->address) =~ $oaddr) {
37                         return 1;
38                 }
39         }
40         return 0;
41 }
42
43 1;