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>
4 # base class for creating per-list or per-project filters
5 package PublicInbox::Filter::Base;
8 use PublicInbox::MsgIter;
10 sub No ($) { "*** We only accept plain-text mail, No $_[0] ***" }
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') ],
19 our $INVALID_FN = qr/\0/;
22 sub ACCEPT { scalar @_ > 1 ? $_[1] : 1 }
25 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
28 $glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
33 my ($class, %opts) = @_;
34 my $self = bless { err => '', %opts }, $class;
35 foreach my $f (qw(reject_suffix reject_type)) {
37 $self->{$f} = $DEFAULTS{$f} unless exists $self->{$f};
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;
45 if (defined $self->{reject_type}) {
46 my $tmp = $self->{reject_type};
48 my ($type, $msg) = split(':', $_, 2);
50 $msg ||= "Unacceptable Content-Type: $type";
51 my $re = glob2pat($type);
52 [ qr/\b$re\b/i, $msg ];
55 $self->{reject_type} = $rt;
60 my ($self, $reason) = @_;
61 $self->{err} = $reason;
65 sub err ($) { $_[0]->{err} }
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.
71 my ($self, $mime, $for_remove) = @_;
77 my ($self, $mime) = @_;
79 my $rt = $self->{reject_type};
80 my $reject_suffix = $self->{reject_suffix} || $INVALID_FN;
84 my ($part, $depth, @idx) = @{$_[0]};
86 my $ct = $part->content_type || 'text/plain';
87 foreach my $p (@$rt) {
93 my $fn = $part->filename;
94 if (defined($fn) && $fn =~ $reject_suffix) {
101 push @r, sort keys %type;
104 push @r, 'Rejected suffixes(s): '.join(', ', sort keys %sfx);
107 @r ? $self->reject(join("\n", @r)) : $self->scrub($mime);