1 # Copyright (C) 2016 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 constant MAX_MID_SIZE => 244; # max term size - 1 in Xapian
11 sub No ($) { "*** We only accept plain-text mail, No $_[0] ***" }
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') ],
20 our $INVALID_FN = qr/\0/;
23 sub ACCEPT { scalar @_ > 1 ? $_[1] : 1 }
26 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
29 $glob =~ s!(.)!$patmap{$1} || "\Q$1"!ge;
34 my ($class, %opts) = @_;
35 my $self = bless { err => '', %opts }, $class;
36 foreach my $f (qw(reject_suffix reject_type)) {
38 $self->{$f} = $DEFAULTS{$f} unless exists $self->{$f};
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;
46 if (defined $self->{reject_type}) {
47 my $tmp = $self->{reject_type};
49 my ($type, $msg) = split(':', $_, 2);
51 $msg ||= "Unacceptable Content-Type: $type";
52 my $re = glob2pat($type);
53 [ qr/\b$re\b/i, $msg ];
56 $self->{reject_type} = $rt;
61 my ($self, $reason) = @_;
62 $self->{err} = $reason;
66 sub err ($) { $_[0]->{err} }
68 # by default, scrub is a no-op, see PublicInbox::Filter::Vger::scrub
69 # for an example of the override
71 my ($self, $mime) = @_;
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);