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