]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Filter/Base.pm
5d070132a4101c78408018283eb8734247806974
[public-inbox.git] / lib / PublicInbox / Filter / Base.pm
1 # Copyright (C) 2016-2018 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 use constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian
10
11 sub No ($) { "*** We only accept plain-text mail, No $_[0] ***" }
12
13 our %DEFAULTS = (
14         reject_suffix => [ qw(exe bat cmd com pif scr vbs cpl zip swf swfl) ],
15         reject_type => [ 'text/html:'.No('HTML'), 'text/xhtml:'.No('HTML'),
16                 'application/vnd.*:'.No('vendor-specific formats'),
17                 'image/*:'.No('images'), 'video/*:'.No('video'),
18                 'audio/*:'.No('audio') ],
19 );
20 our $INVALID_FN = qr/\0/;
21
22 sub REJECT () { 100 }
23 sub ACCEPT { scalar @_ > 1 ? $_[1] : 1 }
24 sub IGNORE () { 0 }
25
26 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
27 sub glob2pat {
28         my ($glob) = @_;
29         $glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
30         $glob;
31 }
32
33 sub new {
34         my ($class, %opts) = @_;
35         my $self = bless { err => '', %opts }, $class;
36         foreach my $f (qw(reject_suffix reject_type)) {
37                 # allow undef:
38                 $self->{$f} = $DEFAULTS{$f} unless exists $self->{$f};
39         }
40         if (defined $self->{reject_suffix}) {
41                 my $tmp = $self->{reject_suffix};
42                 $tmp = join('|', map { glob2pat($_) } @$tmp);
43                 $self->{reject_suffix} = qr/\.($tmp)\s*\z/i;
44         }
45         my $rt = [];
46         if (defined $self->{reject_type}) {
47                 my $tmp = $self->{reject_type};
48                 @$rt = map {
49                         my ($type, $msg) = split(':', $_, 2);
50                         $type = lc $type;
51                         $msg ||= "Unacceptable Content-Type: $type";
52                         my $re = glob2pat($type);
53                         [ qr/\b$re\b/i, $msg ];
54                 } @$tmp;
55         }
56         $self->{reject_type} = $rt;
57         $self;
58 }
59
60 sub reject ($$) {
61         my ($self, $reason) = @_;
62         $self->{err} = $reason;
63         REJECT;
64 }
65
66 sub err ($) { $_[0]->{err} }
67
68 # by default, scrub is a no-op, see PublicInbox::Filter::Vger::scrub
69 # for an example of the override
70 sub scrub {
71         my ($self, $mime) = @_;
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;