1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # ref: https://cr.yp.to/proto/maildir.html
5 # http://wiki2.dovecot.org/MailboxFormat/Maildir
6 package PublicInbox::WatchMaildir;
11 use PublicInbox::Import;
13 use PublicInbox::Spawn qw(spawn);
14 use PublicInbox::InboxWritable;
16 use PublicInbox::Filter::Base;
17 use PublicInbox::Spamcheck;
18 *REJECT = *PublicInbox::Filter::Base::REJECT;
21 my ($class, $config) = @_;
22 my (%mdmap, @mdir, $spamc);
25 # "publicinboxwatch" is the documented namespace
26 # "publicinboxlearn" is legacy but may be supported
28 foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) {
29 my $k = "$pfx.watchspam";
30 if (my $dir = $config->{$k}) {
31 if (is_maildir($dir)) {
32 # skip "new", no MUA has seen it, yet.
34 my $old = $mdmap{$cur};
36 foreach my $ibx (@$old) {
38 "$cur already watched for `$ibx->{name}'
45 $mdmap{$cur} = 'watchspam';
47 warn "unsupported $k=$dir\n";
52 my $k = 'publicinboxwatch.spamcheck';
54 my $spamcheck = PublicInbox::Spamcheck::get($config, $k, $default);
55 $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
57 $config->each_inbox(sub {
58 # need to make all inboxes writable for spam removal:
59 my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
61 my $watch = $ibx->{watch} or return;
62 if (is_maildir($watch)) {
63 if (my $wm = $ibx->{watchheader}) {
64 my ($k, $v) = split(/:/, $wm, 2);
65 $ibx->{-watchheader} = [ $k, qr/\Q$v\E/ ];
67 my $new = "$watch/new";
68 my $cur = "$watch/cur";
69 push @mdir, $new unless $uniq{$new}++;
70 push @mdir, $cur unless $uniq{$cur}++;
72 push @{$mdmap{$new} ||= []}, $ibx;
73 push @{$mdmap{$cur} ||= []}, $ibx;
75 warn "watch unsupported: $k=$watch\n";
80 my $mdre = join('|', map { quotemeta($_) } @mdir);
81 $mdre = qr!\A($mdre)/!;
83 spamcheck => $spamcheck,
89 opendirs => {}, # dirname => dirhandle (in progress scans)
95 my $importers = $self->{importers};
96 foreach my $im (values %$importers) {
102 my ($self, $scan_re, $paths) = @_;
104 my $path = $_->{path};
105 if ($path =~ $scan_re) {
108 _try_path($self, $path);
111 _done_for_now($self);
115 my ($self, $path) = @_;
116 # path must be marked as (S)een
117 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
118 my $mime = _path_to_mime($path) or return;
119 $self->{config}->each_inbox(sub {
122 my $im = _importer_for($self, $ibx);
123 $im->remove($mime, 'spam');
124 if (my $scrub = $ibx->filter($im)) {
125 my $scrubbed = $scrub->scrub($mime, 1);
127 $scrubbed == REJECT() and return;
128 $im->remove($scrubbed, 'spam');
132 warn "error removing spam at: ", $path,
133 " from ", $ibx->{name}, ': ', $@, "\n";
139 my ($self, $path) = @_;
140 return unless PublicInbox::InboxWritable::is_maildir_path($path);
141 if ($path !~ $self->{mdre}) {
142 warn "unrecognized path: $path\n";
145 my $inboxes = $self->{mdmap}->{$1};
147 warn "unmappable dir: $1\n";
150 if (!ref($inboxes) && $inboxes eq 'watchspam') {
151 return _remove_spam($self, $path);
153 foreach my $ibx (@$inboxes) {
154 my $mime = _path_to_mime($path) or next;
155 my $im = _importer_for($self, $ibx);
157 my $wm = $ibx->{-watchheader};
159 my $v = $mime->header_obj->header_raw($wm->[0]);
160 next unless ($v && $v =~ $wm->[1]);
163 if (my $scrub = $ibx->filter($im)) {
164 my $ret = $scrub->scrub($mime) or next;
165 $ret == REJECT() and next;
168 $im->add($mime, $self->{spamcheck});
172 sub quit { trigger_scan($_[0], 'quit') }
176 my $scan = File::Temp->newdir("public-inbox-watch.$$.scan.XXXXXX",
178 my $scandir = $self->{scandir} = $scan->dirname;
179 my $re = qr!\A$scandir/!;
180 my $cb = sub { _try_fsn_paths($self, $re, \@_) };
182 # lazy load here, we may support watching via IMAP IDLE
184 require Filesys::Notify::Simple;
185 my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
186 $fsn->wait($cb) until $self->{quit};
190 my ($self, $base) = @_;
191 my $dir = $self->{scandir} or return;
192 open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
193 close $fh or die "close $dir/$base failed: $!\n";
197 my ($self, $path) = @_;
198 if ($path =~ /quit\z/) {
199 %{$self->{opendirs}} = ();
200 _done_for_now($self);
201 delete $self->{scandir};
205 # else: $path =~ /(cont|full)\z/
206 return if $self->{quit};
208 my $opendirs = $self->{opendirs};
209 my @dirnames = keys %$opendirs;
210 foreach my $dir (@dirnames) {
211 my $dh = delete $opendirs->{$dir};
213 while (my $fn = readdir($dh)) {
214 _try_path($self, "$dir/$fn");
217 $opendirs->{$dir} = $dh if $n < 0;
219 if ($path =~ /full\z/) {
220 foreach my $dir (@{$self->{mdir}}) {
221 next if $opendirs->{$dir}; # already in progress
222 my $ok = opendir(my $dh, $dir);
224 warn "failed to open $dir: $!\n";
228 while (my $fn = readdir($dh)) {
229 _try_path($self, "$dir/$fn");
232 $opendirs->{$dir} = $dh if $n < 0;
235 _done_for_now($self);
236 # do we have more work to do?
237 trigger_scan($self, 'cont') if keys %$opendirs;
242 if (open my $fh, '<', $path) {
246 return PublicInbox::MIME->new(\$str);
247 } elsif ($!{ENOENT}) {
250 warn "failed to open $path: $!\n";
256 my ($self, $ibx) = @_;
257 my $importers = $self->{importers};
258 my $im = $importers->{"$ibx"} ||= $ibx->importer(0);
259 if (scalar(keys(%$importers)) > 2) {
260 delete $importers->{"$ibx"};
261 _done_for_now($self);
264 $importers->{"$ibx"} = $im;
272 if ($sc->spamcheck($mime, \$tmp)) {
273 return PublicInbox::MIME->new(\$tmp);
275 warn $mime->header('Message-ID')." failed spam check\n";
281 $_[0] =~ s!\Amaildir:!! or return;