X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FWatchMaildir.pm;h=c436742cfa6244a191ea62503ac6771cb2a57edb;hb=bb588e4ae5d21ebaaacef18d8360590a97421d9b;hp=f34419a399005b6bbbfb7bbb27cb9a22442519fe;hpb=4e6710183bc331020a406077a1873f2713973f07;p=public-inbox.git diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm index f34419a3..c436742c 100644 --- a/lib/PublicInbox/WatchMaildir.pm +++ b/lib/PublicInbox/WatchMaildir.pm @@ -6,7 +6,7 @@ package PublicInbox::WatchMaildir; use strict; use warnings; -use Email::MIME; +use PublicInbox::MIME; use Email::MIME::ContentType; $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect use PublicInbox::Git; @@ -18,21 +18,25 @@ sub new { my ($class, $config) = @_; my (%mdmap, @mdir, $spamc); - # XXX is "publicinboxlearn" really a good namespace for this? - my $k = 'publicinboxlearn.watchspam'; - if (my $spamdir = $config->{$k}) { - if ($spamdir =~ s/\Amaildir://) { - $spamdir =~ s!/+\z!!; - # skip "new", no MUA has seen it, yet. - my $cur = "$spamdir/cur"; - push @mdir, $cur; - $mdmap{$cur} = 'watchspam'; - } else { - warn "unsupported $k=$spamdir\n"; + # "publicinboxwatch" is the documented namespace + # "publicinboxlearn" is legacy but may be supported + # indefinitely... + foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) { + my $k = "$pfx.watchspam"; + if (my $spamdir = $config->{$k}) { + if ($spamdir =~ s/\Amaildir://) { + $spamdir =~ s!/+\z!!; + # skip "new", no MUA has seen it, yet. + my $cur = "$spamdir/cur"; + push @mdir, $cur; + $mdmap{$cur} = 'watchspam'; + } else { + warn "unsupported $k=$spamdir\n"; + } } } - $k = 'publicinboxwatch.spamcheck'; + my $k = 'publicinboxwatch.spamcheck'; my $spamcheck = $config->{$k}; if ($spamcheck) { if ($spamcheck eq 'spamc') { @@ -76,6 +80,7 @@ sub new { mdmap => \%mdmap, mdir => \@mdir, mdre => $mdre, + config => $config, importers => {}, }, $class; } @@ -92,18 +97,25 @@ sub _try_fsn_paths { sub _remove_spam { my ($self, $path) = @_; - $path =~ /:2,[A-R]*S[T-Z]*\z/i or return; + # path must be marked as (S)een + $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return; my $mime = _path_to_mime($path) or return; _force_mid($mime); - foreach my $inbox (values %{$self->{mdmap}}) { - next unless ref $inbox; - my $im = _importer_for($self, $inbox); - $im->remove($mime); - if (my $scrub = _scrubber_for($inbox)) { - my $scrubbed = $scrub->scrub($mime) or next; - $im->remove($scrubbed); + $self->{config}->each_inbox(sub { + my ($ibx) = @_; + eval { + my $im = _importer_for($self, $ibx); + $im->remove($mime); + if (my $scrub = _scrubber_for($ibx)) { + my $scrubbed = $scrub->scrub($mime) or return; + $im->remove($scrubbed); + } + }; + if ($@) { + warn "error removing spam at: ", $path, + " from ", $ibx->{name}, ': ', $@, "\n"; } - } + }) } # used to hash the relevant portions of a message when there are conflicts @@ -199,7 +211,7 @@ sub _path_to_mime { local $/; my $str = <$fh>; $str or return; - return Email::MIME->new(\$str); + return PublicInbox::MIME->new(\$str); } elsif ($!{ENOENT}) { return; } else { @@ -216,18 +228,31 @@ sub _importer_for { my $addr = $inbox->{-primary_address}; PublicInbox::Import->new($git, $name, $addr, $inbox); }; - $self->{importers}->{"$im"} = $im; + + my $importers = $self->{importers}; + if (scalar(keys(%$importers)) > 2) { + delete $importers->{"$im"}; + _done_for_now($self); + } + + $importers->{"$im"} = $im; } sub _scrubber_for { my ($inbox) = @_; my $f = $inbox->{filter}; if ($f && $f =~ /::/) { + my @args; + # basic line splitting, only + # Perhaps we can have proper quote splitting one day... + ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/; + eval "require $f"; if ($@) { warn $@; } else { - return $f->new; + # e.g: PublicInbox::Filter::Vger->new(@args) + return $f->new(@args); } } undef; @@ -239,7 +264,7 @@ sub _spamcheck_cb { my ($mime) = @_; my $tmp = ''; if ($sc->spamcheck($mime, \$tmp)) { - return Email::MIME->new(\$tmp); + return PublicInbox::MIME->new(\$tmp); } warn $mime->header('Message-ID')." failed spam check\n"; undef;