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;
16 sub compile_watchheaders ($) {
19 if (my $whs = $ibx->{watchheader}) {
21 my ($k, $v) = split(/:/, $_, 2);
22 # XXX should this be case-insensitive?
23 # Or, mutt-style, case-sensitive iff
24 # a capital letter exists?
25 push @$watch_hdrs, [ $k, qr/\Q$v\E/ ];
28 if (my $list_ids = $ibx->{listid}) {
30 # RFC2919 section 6 stipulates
31 # "case insensitive equality"
32 my $re = qr/<[ \t]*\Q$_\E[ \t]*>/i;
33 push @$watch_hdrs, ['List-Id', $re ];
36 $ibx->{-watchheaders} = $watch_hdrs if scalar @$watch_hdrs;
40 my ($class, $config) = @_;
41 my (%mdmap, @mdir, $spamc);
44 # "publicinboxwatch" is the documented namespace
45 # "publicinboxlearn" is legacy but may be supported
47 foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) {
48 my $k = "$pfx.watchspam";
49 defined(my $dirs = $config->{$k}) or next;
50 $dirs = [ $dirs ] if !ref($dirs);
51 for my $dir (@$dirs) {
52 if (is_maildir($dir)) {
53 # skip "new", no MUA has seen it, yet.
55 my $old = $mdmap{$cur};
57 foreach my $ibx (@$old) {
59 "$cur already watched for `$ibx->{name}'
66 $mdmap{$cur} = 'watchspam';
68 warn "unsupported $k=$dir\n";
73 my $k = 'publicinboxwatch.spamcheck';
75 my $spamcheck = PublicInbox::Spamcheck::get($config, $k, $default);
76 $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
78 $config->each_inbox(sub {
79 # need to make all inboxes writable for spam removal:
80 my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
82 my $watch = $ibx->{watch} or return;
83 if (is_maildir($watch)) {
84 compile_watchheaders($ibx);
85 my $new = "$watch/new";
86 my $cur = "$watch/cur";
87 push @mdir, $new unless $uniq{$new}++;
88 push @mdir, $cur unless $uniq{$cur}++;
90 push @{$mdmap{$new} ||= []}, $ibx;
91 push @{$mdmap{$cur} ||= []}, $ibx;
93 warn "watch unsupported: $k=$watch\n";
98 my $mdre = join('|', map { quotemeta($_) } @mdir);
99 $mdre = qr!\A($mdre)/!;
101 spamcheck => $spamcheck,
107 opendirs => {}, # dirname => dirhandle (in progress scans)
113 my $importers = $self->{importers};
114 foreach my $im (values %$importers) {
120 my ($self, $scan_re, $paths) = @_;
122 my $path = $_->{path};
123 if ($path =~ $scan_re) {
126 _try_path($self, $path);
129 _done_for_now($self);
133 my ($self, $path) = @_;
134 # path must be marked as (S)een
135 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
136 my $mime = mime_from_path($path) or return;
137 $self->{config}->each_inbox(sub {
140 my $im = _importer_for($self, $ibx);
141 $im->remove($mime, 'spam');
142 if (my $scrub = $ibx->filter($im)) {
143 my $scrubbed = $scrub->scrub($mime, 1);
145 $scrubbed == REJECT() and return;
146 $im->remove($scrubbed, 'spam');
150 warn "error removing spam at: ", $path,
151 " from ", $ibx->{name}, ': ', $@, "\n";
157 my ($self, $path) = @_;
158 return unless PublicInbox::InboxWritable::is_maildir_path($path);
159 if ($path !~ $self->{mdre}) {
160 warn "unrecognized path: $path\n";
163 my $inboxes = $self->{mdmap}->{$1};
165 warn "unmappable dir: $1\n";
168 if (!ref($inboxes) && $inboxes eq 'watchspam') {
169 return _remove_spam($self, $path);
172 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
173 local $SIG{__WARN__} = sub {
174 $warn_cb->("path: $path\n");
177 foreach my $ibx (@$inboxes) {
178 my $mime = mime_from_path($path) or next;
179 my $im = _importer_for($self, $ibx);
181 # any header match means it's eligible for the inbox:
182 if (my $watch_hdrs = $ibx->{-watchheaders}) {
184 my $hdr = $mime->header_obj;
185 for my $wh (@$watch_hdrs) {
186 my @v = $hdr->header_raw($wh->[0]);
187 $ok = grep(/$wh->[1]/, @v) and last;
192 if (my $scrub = $ibx->filter($im)) {
193 my $ret = $scrub->scrub($mime) or next;
194 $ret == REJECT() and next;
197 $im->add($mime, $self->{spamcheck});
201 sub quit { trigger_scan($_[0], 'quit') }
205 my $scan = File::Temp->newdir("public-inbox-watch.$$.scan.XXXXXX",
207 my $scandir = $self->{scandir} = $scan->dirname;
208 my $re = qr!\A$scandir/!;
209 my $cb = sub { _try_fsn_paths($self, $re, \@_) };
211 # lazy load here, we may support watching via IMAP IDLE
213 eval { require Filesys::Notify::Simple } or
214 die "Filesys::Notify::Simple is currently required for $0\n";
215 my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
216 $fsn->wait($cb) until $self->{quit};
220 my ($self, $base) = @_;
221 my $dir = $self->{scandir} or return;
222 open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
223 close $fh or die "close $dir/$base failed: $!\n";
227 my ($self, $path) = @_;
228 if ($path =~ /quit\z/) {
229 %{$self->{opendirs}} = ();
230 _done_for_now($self);
231 delete $self->{scandir};
235 # else: $path =~ /(cont|full)\z/
236 return if $self->{quit};
238 my $opendirs = $self->{opendirs};
239 my @dirnames = keys %$opendirs;
240 foreach my $dir (@dirnames) {
241 my $dh = delete $opendirs->{$dir};
243 while (my $fn = readdir($dh)) {
244 _try_path($self, "$dir/$fn");
247 $opendirs->{$dir} = $dh if $n < 0;
249 if ($path =~ /full\z/) {
250 foreach my $dir (@{$self->{mdir}}) {
251 next if $opendirs->{$dir}; # already in progress
252 my $ok = opendir(my $dh, $dir);
254 warn "failed to open $dir: $!\n";
258 while (my $fn = readdir($dh)) {
259 _try_path($self, "$dir/$fn");
262 $opendirs->{$dir} = $dh if $n < 0;
265 _done_for_now($self);
266 # do we have more work to do?
267 trigger_scan($self, 'cont') if keys %$opendirs;
271 my ($self, $ibx) = @_;
272 my $importers = $self->{importers};
273 my $im = $importers->{"$ibx"} ||= $ibx->importer(0);
274 if (scalar(keys(%$importers)) > 2) {
275 delete $importers->{"$ibx"};
276 _done_for_now($self);
279 $importers->{"$ibx"} = $im;
287 if ($sc->spamcheck($mime, \$tmp)) {
288 return PublicInbox::Eml->new(\$tmp);
290 warn $mime->header('Message-ID')." failed spam check\n";
296 $_[0] =~ s!\Amaildir:!! or return;