]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Filter/Base.pm
filter/base: remove MAX_MID_SIZE constant
[public-inbox.git] / lib / PublicInbox / Filter / Base.pm
1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # base class for creating per-list or per-project filters
5 package PublicInbox::Filter::Base;
6 use strict;
7 use warnings;
8 use PublicInbox::MsgIter;
9
10 sub No ($) { "*** We only accept plain-text mail, No $_[0] ***" }
11
12 our %DEFAULTS = (
13         reject_suffix => [ qw(exe bat cmd com pif scr vbs cpl zip swf swfl) ],
14         reject_type => [ 'text/html:'.No('HTML'), 'text/xhtml:'.No('HTML'),
15                 'application/vnd.*:'.No('vendor-specific formats'),
16                 'image/*:'.No('images'), 'video/*:'.No('video'),
17                 'audio/*:'.No('audio') ],
18 );
19 our $INVALID_FN = qr/\0/;
20
21 sub REJECT () { 100 }
22 sub ACCEPT { scalar @_ > 1 ? $_[1] : 1 }
23 sub IGNORE () { 0 }
24
25 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
26 sub glob2pat {
27         my ($glob) = @_;
28         $glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
29         $glob;
30 }
31
32 sub new {
33         my ($class, %opts) = @_;
34         my $self = bless { err => '', %opts }, $class;
35         foreach my $f (qw(reject_suffix reject_type)) {
36                 # allow undef:
37                 $self->{$f} = $DEFAULTS{$f} unless exists $self->{$f};
38         }
39         if (defined $self->{reject_suffix}) {
40                 my $tmp = $self->{reject_suffix};
41                 $tmp = join('|', map { glob2pat($_) } @$tmp);
42                 $self->{reject_suffix} = qr/\.($tmp)\s*\z/i;
43         }
44         my $rt = [];
45         if (defined $self->{reject_type}) {
46                 my $tmp = $self->{reject_type};
47                 @$rt = map {
48                         my ($type, $msg) = split(':', $_, 2);
49                         $type = lc $type;
50                         $msg ||= "Unacceptable Content-Type: $type";
51                         my $re = glob2pat($type);
52                         [ qr/\b$re\b/i, $msg ];
53                 } @$tmp;
54         }
55         $self->{reject_type} = $rt;
56         $self;
57 }
58
59 sub reject ($$) {
60         my ($self, $reason) = @_;
61         $self->{err} = $reason;
62         REJECT;
63 }
64
65 sub err ($) { $_[0]->{err} }
66
67 # by default, scrub is a no-op, see PublicInbox::Filter::Vger::scrub
68 # for an example of the override.  The $for_remove arg is set to
69 # disable altid setting for spam removal.
70 sub scrub {
71         my ($self, $mime, $for_remove) = @_;
72         $self->ACCEPT($mime);
73 }
74
75 # for MDA
76 sub delivery {
77         my ($self, $mime) = @_;
78
79         my $rt = $self->{reject_type};
80         my $reject_suffix = $self->{reject_suffix} || $INVALID_FN;
81         my (%sfx, %type);
82
83         msg_iter($mime, sub {
84                 my ($part, $depth, @idx) = @{$_[0]};
85
86                 my $ct = $part->content_type || 'text/plain';
87                 foreach my $p (@$rt) {
88                         if ($ct =~ $p->[0]) {
89                                 $type{$p->[1]} = 1;
90                         }
91                 }
92
93                 my $fn = $part->filename;
94                 if (defined($fn) && $fn =~ $reject_suffix) {
95                         $sfx{$1} = 1;
96                 }
97         });
98
99         my @r;
100         if (keys %type) {
101                 push @r, sort keys %type;
102         }
103         if (keys %sfx) {
104                 push @r, 'Rejected suffixes(s): '.join(', ', sort keys %sfx);
105         }
106
107         @r ? $self->reject(join("\n", @r)) : $self->scrub($mime);
108 }
109
110 1;