about summary refs log tree commit homepage
path: root/lib/PublicInbox/WatchMaildir.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-01-05 00:35:42 +0000
committerEric Wong <e@80x24.org>2019-01-05 08:32:45 +0000
commited3b90b7a203fe5513894d01d478f6104cdff897 (patch)
treeb12be504375306e0208424757286dc30b7f6df6a /lib/PublicInbox/WatchMaildir.pm
parent4a7f4550a4e9f39f801879c5f339abc56984b393 (diff)
downloadpublic-inbox-ed3b90b7a203fe5513894d01d478f6104cdff897.tar.gz
Not sure what I was smoking when I originally wrote this code.

cf. https://public-inbox.org/meta/874li887mp.fsf@vuxu.org/
Diffstat (limited to 'lib/PublicInbox/WatchMaildir.pm')
-rw-r--r--lib/PublicInbox/WatchMaildir.pm54
1 files changed, 34 insertions, 20 deletions
diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm
index b558cda1..064cedf0 100644
--- a/lib/PublicInbox/WatchMaildir.pm
+++ b/lib/PublicInbox/WatchMaildir.pm
@@ -20,6 +20,7 @@ use PublicInbox::Spamcheck;
 sub new {
         my ($class, $config) = @_;
         my (%mdmap, @mdir, $spamc, $spamdir);
+        my %uniq;
 
         # "publicinboxwatch" is the documented namespace
         # "publicinboxlearn" is legacy but may be supported
@@ -32,7 +33,17 @@ sub new {
                                 # skip "new", no MUA has seen it, yet.
                                 my $cur = "$dir/cur";
                                 $spamdir = $cur;
+                                my $old = $mdmap{$cur};
+                                if (ref($old)) {
+                                        foreach my $ibx (@$old) {
+                                                warn <<"";
+"$cur already watched for `$ibx->{name}'
+
+                                        }
+                                        die;
+                                }
                                 push @mdir, $cur;
+                                $uniq{$cur}++;
                                 $mdmap{$cur} = 'watchspam';
                         } else {
                                 warn "unsupported $k=$dir\n";
@@ -58,10 +69,11 @@ sub new {
                         }
                         my $new = "$watch/new";
                         my $cur = "$watch/cur";
-                        push @mdir, $new, $cur;
-                        die "$new already in use\n" if $mdmap{$new};
-                        die "$cur already in use\n" if $mdmap{$cur};
-                        $mdmap{$new} = $mdmap{$cur} = $ibx;
+                        push @mdir, $new unless $uniq{$new}++;
+                        push @mdir, $cur unless $uniq{$cur}++;
+
+                        push @{$mdmap{$new} ||= []}, $ibx;
+                        push @{$mdmap{$cur} ||= []}, $ibx;
                 } else {
                         warn "watch unsupported: $k=$watch\n";
                 }
@@ -134,28 +146,30 @@ sub _try_path {
                 warn "unrecognized path: $path\n";
                 return;
         }
-        my $inbox = $self->{mdmap}->{$1};
-        unless ($inbox) {
+        my $inboxes = $self->{mdmap}->{$1};
+        unless ($inboxes) {
                 warn "unmappable dir: $1\n";
                 return;
         }
-        if (!ref($inbox) && $inbox eq 'watchspam') {
+        if (!ref($inboxes) && $inboxes eq 'watchspam') {
                 return _remove_spam($self, $path);
         }
-        my $im = _importer_for($self, $inbox);
-        my $mime = _path_to_mime($path) or return;
-        my $wm = $inbox->{-watchheader};
-        if ($wm) {
-                my $v = $mime->header_obj->header_raw($wm->[0]);
-                return unless ($v && $v =~ $wm->[1]);
-        }
-        if (my $scrub = $inbox->filter) {
-                my $ret = $scrub->scrub($mime) or return;
-                $ret == REJECT() and return;
-                $mime = $ret;
-        }
+        foreach my $ibx (@$inboxes) {
+                my $mime = _path_to_mime($path) or next;
+                my $im = _importer_for($self, $ibx);
 
-        $im->add($mime, $self->{spamcheck});
+                my $wm = $ibx->{-watchheader};
+                if ($wm) {
+                        my $v = $mime->header_obj->header_raw($wm->[0]);
+                        next unless ($v && $v =~ $wm->[1]);
+                }
+                if (my $scrub = $ibx->filter) {
+                        my $ret = $scrub->scrub($mime) or next;
+                        $ret == REJECT() and next;
+                        $mime = $ret;
+                }
+                $im->add($mime, $self->{spamcheck});
+        }
 }
 
 sub quit { trigger_scan($_[0], 'quit') }