]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Filter/Base.pm
filter/rubylang: do not set altid on spam training
[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.  The $for_remove arg is set to
70 # disable altid setting for spam removal.
71 sub scrub {
72         my ($self, $mime, $for_remove) = @_;
73         $self->ACCEPT($mime);
74 }
75
76 # for MDA
77 sub delivery {
78         my ($self, $mime) = @_;
79
80         my $rt = $self->{reject_type};
81         my $reject_suffix = $self->{reject_suffix} || $INVALID_FN;
82         my (%sfx, %type);
83
84         msg_iter($mime, sub {
85                 my ($part, $depth, @idx) = @{$_[0]};
86
87                 my $ct = $part->content_type || 'text/plain';
88                 foreach my $p (@$rt) {
89                         if ($ct =~ $p->[0]) {
90                                 $type{$p->[1]} = 1;
91                         }
92                 }
93
94                 my $fn = $part->filename;
95                 if (defined($fn) && $fn =~ $reject_suffix) {
96                         $sfx{$1} = 1;
97                 }
98         });
99
100         my @r;
101         if (keys %type) {
102                 push @r, sort keys %type;
103         }
104         if (keys %sfx) {
105                 push @r, 'Rejected suffixes(s): '.join(', ', sort keys %sfx);
106         }
107
108         @r ? $self->reject(join("\n", @r)) : $self->scrub($mime);
109 }
110
111 1;