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, $spamdir);
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 ($dir =~ s/\Amaildir://) {
33 # skip "new", no MUA has seen it, yet.
36 my $old = $mdmap{$cur};
38 foreach my $ibx (@$old) {
40 "$cur already watched for `$ibx->{name}'
47 $mdmap{$cur} = 'watchspam';
49 warn "unsupported $k=$dir\n";
54 my $k = 'publicinboxwatch.spamcheck';
56 my $spamcheck = PublicInbox::Spamcheck::get($config, $k, $default);
57 $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
59 $config->each_inbox(sub {
60 # need to make all inboxes writable for spam removal:
61 my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
63 my $watch = $ibx->{watch} or return;
64 if ($watch =~ s/\Amaildir://) {
66 if (my $wm = $ibx->{watchheader}) {
67 my ($k, $v) = split(/:/, $wm, 2);
68 $ibx->{-watchheader} = [ $k, qr/\Q$v\E/ ];
70 my $new = "$watch/new";
71 my $cur = "$watch/cur";
72 push @mdir, $new unless $uniq{$new}++;
73 push @mdir, $cur unless $uniq{$cur}++;
75 push @{$mdmap{$new} ||= []}, $ibx;
76 push @{$mdmap{$cur} ||= []}, $ibx;
78 warn "watch unsupported: $k=$watch\n";
83 my $mdre = join('|', map { quotemeta($_) } @mdir);
84 $mdre = qr!\A($mdre)/!;
86 spamcheck => $spamcheck,
93 opendirs => {}, # dirname => dirhandle (in progress scans)
99 my $importers = $self->{importers};
100 foreach my $im (values %$importers) {
106 my ($self, $scan_re, $paths) = @_;
108 my $path = $_->{path};
109 if ($path =~ $scan_re) {
112 _try_path($self, $path);
115 _done_for_now($self);
119 my ($self, $path) = @_;
120 # path must be marked as (S)een
121 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
122 my $mime = _path_to_mime($path) or return;
123 $self->{config}->each_inbox(sub {
126 my $im = _importer_for($self, $ibx);
127 $im->remove($mime, 'spam');
128 if (my $scrub = $ibx->filter) {
129 my $scrubbed = $scrub->scrub($mime, 1);
131 $scrubbed == REJECT() and return;
132 $im->remove($scrubbed, 'spam');
136 warn "error removing spam at: ", $path,
137 " from ", $ibx->{name}, ': ', $@, "\n";
143 my ($self, $path) = @_;
144 return unless PublicInbox::InboxWritable::is_maildir_path($path);
145 if ($path !~ $self->{mdre}) {
146 warn "unrecognized path: $path\n";
149 my $inboxes = $self->{mdmap}->{$1};
151 warn "unmappable dir: $1\n";
154 if (!ref($inboxes) && $inboxes eq 'watchspam') {
155 return _remove_spam($self, $path);
157 foreach my $ibx (@$inboxes) {
158 my $mime = _path_to_mime($path) or next;
159 my $im = _importer_for($self, $ibx);
161 my $wm = $ibx->{-watchheader};
163 my $v = $mime->header_obj->header_raw($wm->[0]);
164 next unless ($v && $v =~ $wm->[1]);
166 if (my $scrub = $ibx->filter) {
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";