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>
4 # base class for creating per-list or per-project filters
5 package PublicInbox::Filter::Base;
8 use PublicInbox::MsgIter;
9 use parent qw(Exporter);
10 our @EXPORT_OK = qw(REJECT); # we may export IGNORE if/when needed
12 sub No ($) { "*** We only accept plain-text mail, No $_[0] ***" }
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') ],
21 our $INVALID_FN = qr/\0/;
24 sub ACCEPT { scalar @_ > 1 ? $_[1] : 1 }
27 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
30 $glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
35 my ($class, %opts) = @_;
36 my $self = bless { err => '', %opts }, $class;
37 foreach my $f (qw(reject_suffix reject_type)) {
39 $self->{$f} = $DEFAULTS{$f} unless exists $self->{$f};
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;
47 if (defined $self->{reject_type}) {
48 my $tmp = $self->{reject_type};
50 my ($type, $msg) = split(':', $_, 2);
52 $msg ||= "Unacceptable Content-Type: $type";
53 my $re = glob2pat($type);
54 [ qr/\b$re\b/i, $msg ];
57 $self->{reject_type} = $rt;
62 my ($self, $reason) = @_;
63 $self->{err} = $reason;
67 sub err ($) { $_[0]->{err} }
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.
73 my ($self, $mime, $for_remove) = @_;
79 my ($self, $mime) = @_;
81 my $rt = $self->{reject_type};
82 my $reject_suffix = $self->{reject_suffix} || $INVALID_FN;
86 my ($part, $depth, @idx) = @{$_[0]};
88 my $ct = $part->content_type || 'text/plain';
89 foreach my $p (@$rt) {
95 my $fn = $part->filename;
96 if (defined($fn) && $fn =~ $reject_suffix) {
103 push @r, sort keys %type;
106 push @r, 'Rejected suffixes(s): '.join(', ', sort keys %sfx);
109 @r ? $self->reject(join("\n", @r)) : $self->scrub($mime);