1 # Copyright (C) 2016-2019 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;
10 use PublicInbox::Spawn qw(spawn);
11 use PublicInbox::InboxWritable;
13 use PublicInbox::Filter::Base;
14 use PublicInbox::Spamcheck;
15 *REJECT = *PublicInbox::Filter::Base::REJECT;
18 my ($class, $config) = @_;
19 my (%mdmap, @mdir, $spamc);
22 # "publicinboxwatch" is the documented namespace
23 # "publicinboxlearn" is legacy but may be supported
25 foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) {
26 my $k = "$pfx.watchspam";
27 defined(my $dirs = $config->{$k}) or next;
28 $dirs = [ $dirs ] if !ref($dirs);
29 for my $dir (@$dirs) {
30 if (is_maildir($dir)) {
31 # skip "new", no MUA has seen it, yet.
33 my $old = $mdmap{$cur};
35 foreach my $ibx (@$old) {
37 "$cur already watched for `$ibx->{name}'
44 $mdmap{$cur} = 'watchspam';
46 warn "unsupported $k=$dir\n";
51 my $k = 'publicinboxwatch.spamcheck';
53 my $spamcheck = PublicInbox::Spamcheck::get($config, $k, $default);
54 $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
56 $config->each_inbox(sub {
57 # need to make all inboxes writable for spam removal:
58 my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
60 my $watch = $ibx->{watch} or return;
61 if (is_maildir($watch)) {
62 if (my $wm = $ibx->{watchheader}) {
63 my ($k, $v) = split(/:/, $wm, 2);
64 $ibx->{-watchheader} = [ $k, qr/\Q$v\E/ ];
66 my $new = "$watch/new";
67 my $cur = "$watch/cur";
68 push @mdir, $new unless $uniq{$new}++;
69 push @mdir, $cur unless $uniq{$cur}++;
71 push @{$mdmap{$new} ||= []}, $ibx;
72 push @{$mdmap{$cur} ||= []}, $ibx;
74 warn "watch unsupported: $k=$watch\n";
79 my $mdre = join('|', map { quotemeta($_) } @mdir);
80 $mdre = qr!\A($mdre)/!;
82 spamcheck => $spamcheck,
88 opendirs => {}, # dirname => dirhandle (in progress scans)
94 my $importers = $self->{importers};
95 foreach my $im (values %$importers) {
101 my ($self, $scan_re, $paths) = @_;
103 my $path = $_->{path};
104 if ($path =~ $scan_re) {
107 _try_path($self, $path);
110 _done_for_now($self);
114 my ($self, $path) = @_;
115 # path must be marked as (S)een
116 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
117 my $mime = _path_to_mime($path) or return;
118 $self->{config}->each_inbox(sub {
121 my $im = _importer_for($self, $ibx);
122 $im->remove($mime, 'spam');
123 if (my $scrub = $ibx->filter($im)) {
124 my $scrubbed = $scrub->scrub($mime, 1);
126 $scrubbed == REJECT() and return;
127 $im->remove($scrubbed, 'spam');
131 warn "error removing spam at: ", $path,
132 " from ", $ibx->{name}, ': ', $@, "\n";
138 my ($self, $path) = @_;
139 return unless PublicInbox::InboxWritable::is_maildir_path($path);
140 if ($path !~ $self->{mdre}) {
141 warn "unrecognized path: $path\n";
144 my $inboxes = $self->{mdmap}->{$1};
146 warn "unmappable dir: $1\n";
149 if (!ref($inboxes) && $inboxes eq 'watchspam') {
150 return _remove_spam($self, $path);
153 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
154 local $SIG{__WARN__} = sub {
155 $warn_cb->("path: $path\n");
158 foreach my $ibx (@$inboxes) {
159 my $mime = _path_to_mime($path) or next;
160 my $im = _importer_for($self, $ibx);
162 my $wm = $ibx->{-watchheader};
164 my $v = $mime->header_obj->header_raw($wm->[0]);
165 next unless ($v && $v =~ $wm->[1]);
168 if (my $scrub = $ibx->filter($im)) {
169 my $ret = $scrub->scrub($mime) or next;
170 $ret == REJECT() and next;
173 $im->add($mime, $self->{spamcheck});
177 sub quit { trigger_scan($_[0], 'quit') }
181 my $scan = File::Temp->newdir("public-inbox-watch.$$.scan.XXXXXX",
183 my $scandir = $self->{scandir} = $scan->dirname;
184 my $re = qr!\A$scandir/!;
185 my $cb = sub { _try_fsn_paths($self, $re, \@_) };
187 # lazy load here, we may support watching via IMAP IDLE
189 require Filesys::Notify::Simple;
190 my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
191 $fsn->wait($cb) until $self->{quit};
195 my ($self, $base) = @_;
196 my $dir = $self->{scandir} or return;
197 open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
198 close $fh or die "close $dir/$base failed: $!\n";
202 my ($self, $path) = @_;
203 if ($path =~ /quit\z/) {
204 %{$self->{opendirs}} = ();
205 _done_for_now($self);
206 delete $self->{scandir};
210 # else: $path =~ /(cont|full)\z/
211 return if $self->{quit};
213 my $opendirs = $self->{opendirs};
214 my @dirnames = keys %$opendirs;
215 foreach my $dir (@dirnames) {
216 my $dh = delete $opendirs->{$dir};
218 while (my $fn = readdir($dh)) {
219 _try_path($self, "$dir/$fn");
222 $opendirs->{$dir} = $dh if $n < 0;
224 if ($path =~ /full\z/) {
225 foreach my $dir (@{$self->{mdir}}) {
226 next if $opendirs->{$dir}; # already in progress
227 my $ok = opendir(my $dh, $dir);
229 warn "failed to open $dir: $!\n";
233 while (my $fn = readdir($dh)) {
234 _try_path($self, "$dir/$fn");
237 $opendirs->{$dir} = $dh if $n < 0;
240 _done_for_now($self);
241 # do we have more work to do?
242 trigger_scan($self, 'cont') if keys %$opendirs;
247 if (open my $fh, '<', $path) {
251 return PublicInbox::MIME->new(\$str);
252 } elsif ($!{ENOENT}) {
255 warn "failed to open $path: $!\n";
261 my ($self, $ibx) = @_;
262 my $importers = $self->{importers};
263 my $im = $importers->{"$ibx"} ||= $ibx->importer(0);
264 if (scalar(keys(%$importers)) > 2) {
265 delete $importers->{"$ibx"};
266 _done_for_now($self);
269 $importers->{"$ibx"} = $im;
277 if ($sc->spamcheck($mime, \$tmp)) {
278 return PublicInbox::MIME->new(\$tmp);
280 warn $mime->header('Message-ID')." failed spam check\n";
286 $_[0] =~ s!\Amaildir:!! or return;