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;
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 if (my $dir = $config->{$k}) {
28 if (is_maildir($dir)) {
29 # skip "new", no MUA has seen it, yet.
31 my $old = $mdmap{$cur};
33 foreach my $ibx (@$old) {
35 "$cur already watched for `$ibx->{name}'
42 $mdmap{$cur} = 'watchspam';
44 warn "unsupported $k=$dir\n";
49 my $k = 'publicinboxwatch.spamcheck';
51 my $spamcheck = PublicInbox::Spamcheck::get($config, $k, $default);
52 $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
54 $config->each_inbox(sub {
55 # need to make all inboxes writable for spam removal:
56 my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
58 my $watch = $ibx->{watch} or return;
59 if (is_maildir($watch)) {
60 if (my $wm = $ibx->{watchheader}) {
61 my ($k, $v) = split(/:/, $wm, 2);
62 $ibx->{-watchheader} = [ $k, qr/\Q$v\E/ ];
64 my $new = "$watch/new";
65 my $cur = "$watch/cur";
66 push @mdir, $new unless $uniq{$new}++;
67 push @mdir, $cur unless $uniq{$cur}++;
69 push @{$mdmap{$new} ||= []}, $ibx;
70 push @{$mdmap{$cur} ||= []}, $ibx;
72 warn "watch unsupported: $k=$watch\n";
77 my $mdre = join('|', map { quotemeta($_) } @mdir);
78 $mdre = qr!\A($mdre)/!;
80 spamcheck => $spamcheck,
86 opendirs => {}, # dirname => dirhandle (in progress scans)
92 my $importers = $self->{importers};
93 foreach my $im (values %$importers) {
99 my ($self, $scan_re, $paths) = @_;
101 my $path = $_->{path};
102 if ($path =~ $scan_re) {
105 _try_path($self, $path);
108 _done_for_now($self);
112 my ($self, $path) = @_;
113 # path must be marked as (S)een
114 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
115 my $mime = _path_to_mime($path) or return;
116 $self->{config}->each_inbox(sub {
119 my $im = _importer_for($self, $ibx);
120 $im->remove($mime, 'spam');
121 if (my $scrub = $ibx->filter($im)) {
122 my $scrubbed = $scrub->scrub($mime, 1);
124 $scrubbed == REJECT() and return;
125 $im->remove($scrubbed, 'spam');
129 warn "error removing spam at: ", $path,
130 " from ", $ibx->{name}, ': ', $@, "\n";
136 my ($self, $path) = @_;
137 return unless PublicInbox::InboxWritable::is_maildir_path($path);
138 if ($path !~ $self->{mdre}) {
139 warn "unrecognized path: $path\n";
142 my $inboxes = $self->{mdmap}->{$1};
144 warn "unmappable dir: $1\n";
147 if (!ref($inboxes) && $inboxes eq 'watchspam') {
148 return _remove_spam($self, $path);
151 my $warn_cb = $SIG{__WARN__} || sub { print STDERR @_ };
152 local $SIG{__WARN__} = sub {
153 $warn_cb->("path: $path\n");
156 foreach my $ibx (@$inboxes) {
157 my $mime = _path_to_mime($path) or next;
158 my $im = _importer_for($self, $ibx);
160 my $wm = $ibx->{-watchheader};
162 my $v = $mime->header_obj->header_raw($wm->[0]);
163 next unless ($v && $v =~ $wm->[1]);
166 if (my $scrub = $ibx->filter($im)) {
167 my $ret = $scrub->scrub($mime) or next;
168 $ret == REJECT() and next;
171 $im->add($mime, $self->{spamcheck});
175 sub quit { trigger_scan($_[0], 'quit') }
179 my $scan = File::Temp->newdir("public-inbox-watch.$$.scan.XXXXXX",
181 my $scandir = $self->{scandir} = $scan->dirname;
182 my $re = qr!\A$scandir/!;
183 my $cb = sub { _try_fsn_paths($self, $re, \@_) };
185 # lazy load here, we may support watching via IMAP IDLE
187 require Filesys::Notify::Simple;
188 my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
189 $fsn->wait($cb) until $self->{quit};
193 my ($self, $base) = @_;
194 my $dir = $self->{scandir} or return;
195 open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
196 close $fh or die "close $dir/$base failed: $!\n";
200 my ($self, $path) = @_;
201 if ($path =~ /quit\z/) {
202 %{$self->{opendirs}} = ();
203 _done_for_now($self);
204 delete $self->{scandir};
208 # else: $path =~ /(cont|full)\z/
209 return if $self->{quit};
211 my $opendirs = $self->{opendirs};
212 my @dirnames = keys %$opendirs;
213 foreach my $dir (@dirnames) {
214 my $dh = delete $opendirs->{$dir};
216 while (my $fn = readdir($dh)) {
217 _try_path($self, "$dir/$fn");
220 $opendirs->{$dir} = $dh if $n < 0;
222 if ($path =~ /full\z/) {
223 foreach my $dir (@{$self->{mdir}}) {
224 next if $opendirs->{$dir}; # already in progress
225 my $ok = opendir(my $dh, $dir);
227 warn "failed to open $dir: $!\n";
231 while (my $fn = readdir($dh)) {
232 _try_path($self, "$dir/$fn");
235 $opendirs->{$dir} = $dh if $n < 0;
238 _done_for_now($self);
239 # do we have more work to do?
240 trigger_scan($self, 'cont') if keys %$opendirs;
245 if (open my $fh, '<', $path) {
249 return PublicInbox::MIME->new(\$str);
250 } elsif ($!{ENOENT}) {
253 warn "failed to open $path: $!\n";
259 my ($self, $ibx) = @_;
260 my $importers = $self->{importers};
261 my $im = $importers->{"$ibx"} ||= $ibx->importer(0);
262 if (scalar(keys(%$importers)) > 2) {
263 delete $importers->{"$ibx"};
264 _done_for_now($self);
267 $importers->{"$ibx"} = $im;
275 if ($sc->spamcheck($mime, \$tmp)) {
276 return PublicInbox::MIME->new(\$tmp);
278 warn $mime->header('Message-ID')." failed spam check\n";
284 $_[0] =~ s!\Amaildir:!! or return;