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