X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FWatchMaildir.pm;h=1823c24862e6e6da36112ce86f123692f1b82e1d;hb=a465cc132b8d1ad96dbd0f51ad6da2ce75c79568;hp=cb64f899fb27e277b8a6032100f18683c35ff5e3;hpb=d983e59299602acac5d700093ac76c87dbaad10c;p=public-inbox.git diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm index cb64f899..1823c248 100644 --- a/lib/PublicInbox/WatchMaildir.pm +++ b/lib/PublicInbox/WatchMaildir.pm @@ -1,9 +1,12 @@ # Copyright (C) 2016 all contributors # License: AGPL-3.0+ +# +# ref: https://cr.yp.to/proto/maildir.html +# http://wiki2.dovecot.org/MailboxFormat/Maildir 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; @@ -13,17 +16,38 @@ use PublicInbox::Spawn qw(spawn); sub new { my ($class, $config) = @_; - my (%mdmap, @mdir); - 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'; + my (%mdmap, @mdir, $spamc); + + # "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"; + } + } + } + + my $k = 'publicinboxwatch.spamcheck'; + my $spamcheck = $config->{$k}; + if ($spamcheck) { + if ($spamcheck eq 'spamc') { + $spamcheck = 'PublicInbox::Spamcheck::Spamc'; + } + if ($spamcheck =~ /::/) { + eval "require $spamcheck"; + $spamcheck = _spamcheck_cb($spamcheck->new); } else { - warn "unsupported $k=$spamdir\n"; + warn "unsupported $k=$spamcheck\n"; + $spamcheck = undef; } } foreach $k (keys %$config) { @@ -52,9 +76,11 @@ sub new { my $mdre = join('|', map { quotemeta($_) } @mdir); $mdre = qr!\A($mdre)/!; bless { + spamcheck => $spamcheck, mdmap => \%mdmap, mdir => \@mdir, mdre => $mdre, + config => $config, importers => {}, }, $class; } @@ -69,19 +95,23 @@ sub _try_fsn_paths { _done_for_now($self); } -sub _check_spam { +sub _remove_spam { my ($self, $path) = @_; + $path =~ /:2,[A-R]*S[T-Z]*\z/i 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); + } + }; + warn "error removing spam at $path from $ibx->{name}\n" if $@; + }) } # used to hash the relevant portions of a message when there are conflicts @@ -107,6 +137,13 @@ sub _force_mid { sub _try_path { my ($self, $path) = @_; + my @p = split(m!/+!, $path); + return if $p[-1] !~ /\A[a-zA-Z0-9][\w:,=\.]+\z/; + if ($p[-1] =~ /:2,([A-Z]+)\z/i) { + my $flags = $1; + return if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail + } + return unless -f $path; if ($path !~ $self->{mdre}) { warn "unrecognized path: $path\n"; return; @@ -117,7 +154,7 @@ sub _try_path { return; } if (!ref($inbox) && $inbox eq 'watchspam') { - return _check_spam($self, $path); + return _remove_spam($self, $path); } my $im = _importer_for($self, $inbox); my $mime = _path_to_mime($path) or return; @@ -132,7 +169,7 @@ sub _try_path { } _force_mid($mime); - $im->add($mime); + $im->add($mime, $self->{spamcheck}); } sub watch { @@ -140,6 +177,8 @@ sub watch { my $cb = sub { _try_fsn_paths($self, \@_) }; my $mdir = $self->{mdir}; + # lazy load here, we may support watching via IMAP IDLE + # in the future... require Filesys::Notify::Simple; my $watcher = Filesys::Notify::Simple->new($mdir); $watcher->wait($cb) while (1); @@ -155,13 +194,7 @@ sub scan { next; } while (my $fn = readdir($dh)) { - next unless $fn =~ /\A[a-zA-Z0-9][\w:,=\.]+\z/; - $fn = "$dir/$fn"; - if (-f $fn) { - _try_path($self, $fn); - } else { - warn "not a file: $fn\n"; - } + _try_path($self, "$dir/$fn"); } closedir $dh; } @@ -174,7 +207,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 { @@ -189,23 +222,49 @@ sub _importer_for { my $git = $inbox->git; my $name = $inbox->{name}; my $addr = $inbox->{-primary_address}; - PublicInbox::Import->new($git, $name, $addr); + 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; } +sub _spamcheck_cb { + my ($sc) = @_; + sub { + my ($mime) = @_; + my $tmp = ''; + if ($sc->spamcheck($mime, \$tmp)) { + return PublicInbox::MIME->new(\$tmp); + } + warn $mime->header('Message-ID')." failed spam check\n"; + undef; + } +} + 1;