1 # Copyright (C) 2016-2020 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::InboxWritable;
11 use File::Temp 0.19 (); # 0.19 for ->newdir
12 use PublicInbox::Filter::Base qw(REJECT);
13 use PublicInbox::Spamcheck;
14 *mime_from_path = \&PublicInbox::InboxWritable::mime_from_path;
17 my ($class, $config) = @_;
18 my (%mdmap, @mdir, $spamc);
21 # "publicinboxwatch" is the documented namespace
22 # "publicinboxlearn" is legacy but may be supported
24 foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) {
25 my $k = "$pfx.watchspam";
26 defined(my $dirs = $config->{$k}) or next;
27 $dirs = [ $dirs ] if !ref($dirs);
28 for my $dir (@$dirs) {
29 if (is_maildir($dir)) {
30 # skip "new", no MUA has seen it, yet.
32 my $old = $mdmap{$cur};
34 foreach my $ibx (@$old) {
36 "$cur already watched for `$ibx->{name}'
43 $mdmap{$cur} = 'watchspam';
45 warn "unsupported $k=$dir\n";
50 my $k = 'publicinboxwatch.spamcheck';
52 my $spamcheck = PublicInbox::Spamcheck::get($config, $k, $default);
53 $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
55 $config->each_inbox(sub {
56 # need to make all inboxes writable for spam removal:
57 my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
59 my $watch = $ibx->{watch} or return;
60 if (is_maildir($watch)) {
62 if (my $whs = $ibx->{watchheader}) {
64 my ($k, $v) = split(/:/, $_, 2);
65 push @$watch_hdrs, [ $k, qr/\Q$v\E/ ];
68 if (my $list_ids = $ibx->{listid}) {
70 my $re = qr/<[ \t]*\Q$_\E[ \t]*>/;
71 push @$watch_hdrs, ['List-Id', $re ];
74 if (scalar @$watch_hdrs) {
75 $ibx->{-watchheaders} = $watch_hdrs;
77 my $new = "$watch/new";
78 my $cur = "$watch/cur";
79 push @mdir, $new unless $uniq{$new}++;
80 push @mdir, $cur unless $uniq{$cur}++;
82 push @{$mdmap{$new} ||= []}, $ibx;
83 push @{$mdmap{$cur} ||= []}, $ibx;
85 warn "watch unsupported: $k=$watch\n";
90 my $mdre = join('|', map { quotemeta($_) } @mdir);
91 $mdre = qr!\A($mdre)/!;
93 spamcheck => $spamcheck,
99 opendirs => {}, # dirname => dirhandle (in progress scans)
105 my $importers = $self->{importers};
106 foreach my $im (values %$importers) {
112 my ($self, $scan_re, $paths) = @_;
114 my $path = $_->{path};
115 if ($path =~ $scan_re) {
118 _try_path($self, $path);
121 _done_for_now($self);
125 my ($self, $path) = @_;
126 # path must be marked as (S)een
127 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
128 my $mime = mime_from_path($path) or return;
129 $self->{config}->each_inbox(sub {
132 my $im = _importer_for($self, $ibx);
133 $im->remove($mime, 'spam');
134 if (my $scrub = $ibx->filter($im)) {
135 my $scrubbed = $scrub->scrub($mime, 1);
137 $scrubbed == REJECT() and return;
138 $im->remove($scrubbed, 'spam');
142 warn "error removing spam at: ", $path,
143 " from ", $ibx->{name}, ': ', $@, "\n";
149 my ($self, $path) = @_;
150 return unless PublicInbox::InboxWritable::is_maildir_path($path);
151 if ($path !~ $self->{mdre}) {
152 warn "unrecognized path: $path\n";
155 my $inboxes = $self->{mdmap}->{$1};
157 warn "unmappable dir: $1\n";
160 if (!ref($inboxes) && $inboxes eq 'watchspam') {
161 return _remove_spam($self, $path);
164 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
165 local $SIG{__WARN__} = sub {
166 $warn_cb->("path: $path\n");
169 foreach my $ibx (@$inboxes) {
170 my $mime = mime_from_path($path) or next;
171 my $im = _importer_for($self, $ibx);
173 # any header match means it's eligible for the inbox:
174 if (my $watch_hdrs = $ibx->{-watchheaders}) {
176 my $hdr = $mime->header_obj;
177 for my $wh (@$watch_hdrs) {
178 my $v = $hdr->header_raw($wh->[0]);
179 next unless defined($v) && $v =~ $wh->[1];
186 if (my $scrub = $ibx->filter($im)) {
187 my $ret = $scrub->scrub($mime) or next;
188 $ret == REJECT() and next;
191 $im->add($mime, $self->{spamcheck});
195 sub quit { trigger_scan($_[0], 'quit') }
199 my $scan = File::Temp->newdir("public-inbox-watch.$$.scan.XXXXXX",
201 my $scandir = $self->{scandir} = $scan->dirname;
202 my $re = qr!\A$scandir/!;
203 my $cb = sub { _try_fsn_paths($self, $re, \@_) };
205 # lazy load here, we may support watching via IMAP IDLE
207 eval { require Filesys::Notify::Simple } or
208 die "Filesys::Notify::Simple is currently required for $0\n";
209 my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
210 $fsn->wait($cb) until $self->{quit};
214 my ($self, $base) = @_;
215 my $dir = $self->{scandir} or return;
216 open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
217 close $fh or die "close $dir/$base failed: $!\n";
221 my ($self, $path) = @_;
222 if ($path =~ /quit\z/) {
223 %{$self->{opendirs}} = ();
224 _done_for_now($self);
225 delete $self->{scandir};
229 # else: $path =~ /(cont|full)\z/
230 return if $self->{quit};
232 my $opendirs = $self->{opendirs};
233 my @dirnames = keys %$opendirs;
234 foreach my $dir (@dirnames) {
235 my $dh = delete $opendirs->{$dir};
237 while (my $fn = readdir($dh)) {
238 _try_path($self, "$dir/$fn");
241 $opendirs->{$dir} = $dh if $n < 0;
243 if ($path =~ /full\z/) {
244 foreach my $dir (@{$self->{mdir}}) {
245 next if $opendirs->{$dir}; # already in progress
246 my $ok = opendir(my $dh, $dir);
248 warn "failed to open $dir: $!\n";
252 while (my $fn = readdir($dh)) {
253 _try_path($self, "$dir/$fn");
256 $opendirs->{$dir} = $dh if $n < 0;
259 _done_for_now($self);
260 # do we have more work to do?
261 trigger_scan($self, 'cont') if keys %$opendirs;
265 my ($self, $ibx) = @_;
266 my $importers = $self->{importers};
267 my $im = $importers->{"$ibx"} ||= $ibx->importer(0);
268 if (scalar(keys(%$importers)) > 2) {
269 delete $importers->{"$ibx"};
270 _done_for_now($self);
273 $importers->{"$ibx"} = $im;
281 if ($sc->spamcheck($mime, \$tmp)) {
282 return PublicInbox::MIME->new(\$tmp);
284 warn $mime->header('Message-ID')." failed spam check\n";
290 $_[0] =~ s!\Amaildir:!! or return;