]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WatchMaildir.pm
update copyrights for 2018
[public-inbox.git] / lib / PublicInbox / WatchMaildir.pm
index 0e2a6d2c3c5d6f6ea50580b5ad8e25eb9314278f..a3fab428fc3a4c14bfa3ef33fa4690bfabf20c8d 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # ref: https://cr.yp.to/proto/maildir.html
@@ -91,13 +91,18 @@ sub new {
 
 sub _done_for_now {
        my ($self) = @_;
+       my $importers = $self->{importers};
+       foreach my $im (values %$importers) {
+               $im->done if $im->{nchg};
+       }
+
        my $opendirs = $self->{opendirs};
 
        # spamdir scanning means every importer remains open
        my $spamdir = $self->{spamdir};
        return if defined($spamdir) && $opendirs->{$spamdir};
 
-       foreach my $im (values %{$self->{importers}}) {
+       foreach my $im (values %$importers) {
                # not done if we're scanning
                next if $opendirs->{$im->{git}->{git_dir}};
                $im->done;
@@ -127,11 +132,11 @@ sub _remove_spam {
                my ($ibx) = @_;
                eval {
                        my $im = _importer_for($self, $ibx);
-                       $im->remove($mime);
+                       $im->remove($mime, 'spam');
                        if (my $scrub = _scrubber_for($ibx)) {
                                my $scrubbed = $scrub->scrub($mime) or return;
                                $scrubbed == 100 and return;
-                               $im->remove($scrubbed);
+                               $im->remove($scrubbed, 'spam');
                        }
                };
                if ($@) {
@@ -201,7 +206,7 @@ sub _try_path {
        $im->add($mime, $self->{spamcheck});
 }
 
-sub quit { $_[0]->{quit} = 1 }
+sub quit { trigger_scan($_[0], 'quit') }
 
 sub watch {
        my ($self) = @_;
@@ -215,18 +220,27 @@ sub watch {
        # in the future...
        require Filesys::Notify::Simple;
        my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
-       $fsn->wait($cb) until ($self->{quit});
+       $fsn->wait($cb) until $self->{quit};
 }
 
 sub trigger_scan {
        my ($self, $base) = @_;
-       my $dir = $self->{scandir} or die "not watch-ing, yet\n";
+       my $dir = $self->{scandir} or return;
        open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
        close $fh or die "close $dir/$base failed: $!\n";
 }
 
 sub scan {
        my ($self, $path) = @_;
+       if ($path =~ /quit\z/) {
+               %{$self->{opendirs}} = ();
+               _done_for_now($self);
+               delete $self->{scandir};
+               $self->{quit} = 1;
+               return;
+       }
+       # else: $path =~ /(cont|full)\z/
+       return if $self->{quit};
        my $max = 10;
        my $opendirs = $self->{opendirs};
        my @dirnames = keys %$opendirs;
@@ -255,11 +269,9 @@ sub scan {
                        $opendirs->{$dir} = $dh if $n < 0;
                }
        }
-       if (keys %$opendirs) { # do we have more work to do?
-               trigger_scan($self, 'cont');
-       } else {
-               _done_for_now($self);
-       }
+       _done_for_now($self);
+       # do we have more work to do?
+       trigger_scan($self, 'cont') if keys %$opendirs;
 }
 
 sub _path_to_mime {