]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox.pm
a4aba34692852263c9d5abe8e927673f958a1444
[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 if length($simple->as_string) > MAX_SIZE;
23         recipient_specified($filter, $recipient);
24 }
25
26 sub recipient_specified {
27         my ($filter, $recipient) = @_;
28         defined($recipient) or return 1; # for mass imports
29         my @recip = Email::Address->parse($recipient);
30         my $oaddr = __drop_plus($recip[0]->address);
31         $oaddr = qr/\b\Q$oaddr\E\b/i;
32         my @to = Email::Address->parse($filter->to);
33         my @cc = Email::Address->parse($filter->cc);
34         foreach my $addr (@to, @cc) {
35                 if (__drop_plus($addr->address) =~ $oaddr) {
36                         return 1;
37                 }
38         }
39         return 0;
40 }
41
42 1;