]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Watch.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / Watch.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # ref: https://cr.yp.to/proto/maildir.html
5 #       https://wiki2.dovecot.org/MailboxFormat/Maildir
6 package PublicInbox::Watch;
7 use strict;
8 use v5.10.1;
9 use PublicInbox::Eml;
10 use PublicInbox::InboxWritable qw(eml_from_path);
11 use PublicInbox::MdirReader;
12 use PublicInbox::NetReader;
13 use PublicInbox::Filter::Base qw(REJECT);
14 use PublicInbox::Spamcheck;
15 use PublicInbox::DS qw(now add_timer awaitpid);
16 use PublicInbox::MID qw(mids);
17 use PublicInbox::ContentHash qw(content_hash);
18 use POSIX qw(_exit WNOHANG);
19
20 sub compile_watchheaders ($) {
21         my ($ibx) = @_;
22         my $watch_hdrs = [];
23         if (my $whs = $ibx->{watchheader}) {
24                 for (@$whs) {
25                         my ($k, $v) = split(/:/, $_, 2);
26                         # XXX should this be case-insensitive?
27                         # Or, mutt-style, case-sensitive iff
28                         # a capital letter exists?
29                         push @$watch_hdrs, [ $k, qr/\Q$v\E/ ];
30                 }
31         }
32         if (my $list_ids = $ibx->{listid}) {
33                 for (@$list_ids) {
34                         # RFC2919 section 6 stipulates
35                         # "case insensitive equality"
36                         my $re = qr/<[ \t]*\Q$_\E[ \t]*>/i;
37                         push @$watch_hdrs, ['List-Id', $re ];
38                 }
39         }
40         $ibx->{-watchheaders} = $watch_hdrs if scalar @$watch_hdrs;
41 }
42
43 sub new {
44         my ($class, $cfg) = @_;
45         my (%mdmap, $spamc);
46         my (%imap, %nntp); # url => [inbox objects] or 'watchspam'
47         my (@imap, @nntp);
48
49         # "publicinboxwatch" is the documented namespace
50         # "publicinboxlearn" is legacy but may be supported
51         # indefinitely...
52         foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) {
53                 my $k = "$pfx.watchspam";
54                 my $dirs = $cfg->get_all($k) // next;
55                 for my $dir (@$dirs) {
56                         my $uri;
57                         if (is_maildir($dir)) {
58                                 # skip "new", no MUA has seen it, yet.
59                                 $mdmap{"$dir/cur"} = 'watchspam';
60                         } elsif ($uri = imap_uri($dir)) {
61                                 $imap{$$uri} = 'watchspam';
62                                 push @imap, $uri;
63                         } elsif ($uri = nntp_uri($dir)) {
64                                 $nntp{$$uri} = 'watchspam';
65                                 push @nntp, $uri;
66                         } else {
67                                 warn "unsupported $k=$dir\n";
68                         }
69                 }
70         }
71
72         my $k = 'publicinboxwatch.spamcheck';
73         my $default = undef;
74         my $spamcheck = PublicInbox::Spamcheck::get($cfg, $k, $default);
75         $spamcheck = _spamcheck_cb($spamcheck) if $spamcheck;
76
77         $cfg->each_inbox(sub {
78                 # need to make all inboxes writable for spam removal:
79                 my $ibx = $_[0] = PublicInbox::InboxWritable->new($_[0]);
80
81                 my $watches = $ibx->{watch} or return;
82                 $watches = PublicInbox::Config::_array($watches);
83                 for my $watch (@$watches) {
84                         my $uri;
85                         if (is_maildir($watch)) {
86                                 compile_watchheaders($ibx);
87                                 my ($new, $cur) = ("$watch/new", "$watch/cur");
88                                 my $cur_dst = $mdmap{$cur} //= [];
89                                 return if is_watchspam($cur, $cur_dst, $ibx);
90                                 push @{$mdmap{$new} //= []}, $ibx;
91                                 push @$cur_dst, $ibx;
92                         } elsif ($uri = imap_uri($watch)) {
93                                 my $cur_dst = $imap{$$uri} //= [];
94                                 return if is_watchspam($uri, $cur_dst, $ibx);
95                                 compile_watchheaders($ibx);
96                                 push(@imap, $uri) if 1 == push(@$cur_dst, $ibx);
97                         } elsif ($uri = nntp_uri($watch)) {
98                                 my $cur_dst = $nntp{$$uri} //= [];
99                                 return if is_watchspam($uri, $cur_dst, $ibx);
100                                 compile_watchheaders($ibx);
101                                 push(@nntp, $uri) if 1 == push(@$cur_dst, $ibx);
102                         } else {
103                                 warn "watch unsupported: $k=$watch\n";
104                         }
105                 }
106         });
107
108         my $mdre;
109         if (scalar keys %mdmap) {
110                 $mdre = join('|', map { quotemeta($_) } keys %mdmap);
111                 $mdre = qr!\A($mdre)/!;
112         }
113         return unless $mdre || scalar(keys %imap) || scalar(keys %nntp);
114
115         bless {
116                 max_batch => 10, # avoid hogging locks for too long
117                 spamcheck => $spamcheck,
118                 mdmap => \%mdmap,
119                 mdre => $mdre,
120                 pi_cfg => $cfg,
121                 imap => scalar keys %imap ? \%imap : undef,
122                 nntp => scalar keys %nntp? \%nntp : undef,
123                 imap_order => scalar(@imap) ? \@imap : undef,
124                 nntp_order => scalar(@nntp) ? \@nntp: undef,
125                 importers => {},
126                 opendirs => {}, # dirname => dirhandle (in progress scans)
127                 ops => [], # 'quit', 'full'
128         }, $class;
129 }
130
131 sub _done_for_now {
132         my ($self) = @_;
133         local $PublicInbox::DS::in_loop = 0; # waitpid() synchronously
134         for my $im (values %{$self->{importers}}) {
135                 next if !$im; # $im may be undef during cleanup
136                 eval { $im->done };
137                 warn "$im->{ibx}->{name} ->done: $@\n" if $@;
138         }
139 }
140
141 sub remove_eml_i { # each_inbox callback
142         my ($ibx, $self, $eml, $loc) = @_;
143
144         eval {
145                 # try to avoid taking a lock or unnecessary spawning
146                 my $im = $self->{importers}->{"$ibx"};
147                 my $scrubbed;
148                 if ((!$im || !$im->active) && $ibx->over) {
149                         if (content_exists($ibx, $eml)) {
150                                 # continue
151                         } elsif (my $scrub = $ibx->filter($im)) {
152                                 $scrubbed = $scrub->scrub($eml, 1);
153                                 if ($scrubbed && $scrubbed != REJECT &&
154                                           !content_exists($ibx, $scrubbed)) {
155                                         return;
156                                 }
157                         } else {
158                                 return;
159                         }
160                 }
161
162                 $im //= _importer_for($self, $ibx); # may spawn fast-import
163                 $im->remove($eml, 'spam');
164                 $scrubbed //= do {
165                         my $scrub = $ibx->filter($im);
166                         $scrub ? $scrub->scrub($eml, 1) : undef;
167                 };
168                 if ($scrubbed && $scrubbed != REJECT) {
169                         $im->remove($scrubbed, 'spam');
170                 }
171         };
172         if ($@) {
173                 warn "error removing spam at: $loc from $ibx->{name}: $@\n";
174                 _done_for_now($self);
175         }
176 }
177
178 sub _remove_spam {
179         my ($self, $path) = @_;
180         # path must be marked as (S)een
181         $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
182         my $eml = eml_from_path($path) or return;
183         local $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
184         $self->{pi_cfg}->each_inbox(\&remove_eml_i, $self, $eml, $path);
185 }
186
187 sub import_eml ($$$) {
188         my ($self, $ibx, $eml) = @_;
189
190         # any header match means it's eligible for the inbox:
191         if (my $watch_hdrs = $ibx->{-watchheaders}) {
192                 my $ok;
193                 for my $wh (@$watch_hdrs) {
194                         my @v = $eml->header_raw($wh->[0]);
195                         $ok = grep(/$wh->[1]/, @v) and last;
196                 }
197                 return unless $ok;
198         }
199         eval {
200                 my $im = _importer_for($self, $ibx);
201                 if (my $scrub = $ibx->filter($im)) {
202                         my $scrubbed = $scrub->scrub($eml) or return;
203                         $scrubbed == REJECT and return;
204                         $eml = $scrubbed;
205                 }
206                 $im->add($eml, $self->{spamcheck});
207         };
208         if ($@) {
209                 warn "$ibx->{name} add failed: $@\n";
210                 _done_for_now($self);
211         }
212 }
213
214 sub _try_path {
215         my ($self, $path) = @_;
216         my $fl = PublicInbox::MdirReader::maildir_path_flags($path) // return;
217         return if $fl =~ /[DT]/; # no Drafts or Trash
218         if ($path !~ $self->{mdre}) {
219                 warn "unrecognized path: $path\n";
220                 return;
221         }
222         my $inboxes = $self->{mdmap}->{$1};
223         unless ($inboxes) {
224                 warn "unmappable dir: $1\n";
225                 return;
226         }
227         my $warn_cb = $SIG{__WARN__} || \&CORE::warn;
228         local $SIG{__WARN__} = sub {
229                 my $pfx = ($_[0] // '') =~ /^([A-Z]: )/g ? $1 : '';
230                 $warn_cb->($pfx, "path: $path\n", @_);
231         };
232         if (!ref($inboxes) && $inboxes eq 'watchspam') {
233                 return _remove_spam($self, $path);
234         }
235         foreach my $ibx (@$inboxes) {
236                 my $eml = eml_from_path($path) or next;
237                 import_eml($self, $ibx, $eml);
238         }
239 }
240
241 sub quit_done ($) {
242         my ($self) = @_;
243         return unless $self->{quit};
244
245         # don't have reliable wakeups, keep signalling
246         my $live = grep { kill('QUIT', $_) } keys %{$self->{pids}};
247         add_timer(0.01, \&quit_done, $self) if $live;
248         $live == 0;
249 }
250
251 sub quit {
252         my ($self) = @_;
253         $self->{quit} = 1;
254         %{$self->{opendirs}} = ();
255         _done_for_now($self);
256         quit_done($self);
257         if (my $idle_mic = $self->{idle_mic}) {
258                 eval { $idle_mic->done };
259                 if ($@) {
260                         warn "IDLE DONE error: $@\n";
261                         eval { $idle_mic->disconnect };
262                         warn "IDLE LOGOUT error: $@\n" if $@;
263                 }
264         }
265 }
266
267 sub watch_fs_init ($) {
268         my ($self) = @_;
269         my $done = sub {
270                 delete $self->{done_timer};
271                 _done_for_now($self);
272         };
273         my $cb = sub { # called by PublicInbox::DirIdle::event_step
274                 _try_path($self, $_[0]->fullname);
275                 $self->{done_timer} //= PublicInbox::DS::requeue($done);
276         };
277         require PublicInbox::DirIdle;
278         # inotify_create + EPOLL_CTL_ADD
279         my $dir_idle = PublicInbox::DirIdle->new($cb);
280         $dir_idle->add_watches([keys %{$self->{mdmap}}]);
281 }
282
283 sub net_cb { # NetReader::(nntp|imap)_each callback
284         my ($uri, $art, $kw, $eml, $self, $inboxes) = @_;
285         return if grep(/\Adraft\z/, @$kw);
286         local $self->{cur_uid} = $art; # IMAP UID or NNTP article
287         if (ref($inboxes)) {
288                 my @ibx = @$inboxes;
289                 my $last = pop @ibx;
290                 for my $ibx (@ibx) {
291                         my $tmp = PublicInbox::Eml->new(\($eml->as_string));
292                         import_eml($self, $ibx, $tmp);
293                 }
294                 import_eml($self, $last, $eml);
295         } elsif ($inboxes eq 'watchspam') {
296                 if ($uri->scheme =~ /\Aimaps?\z/ && !grep(/\Aseen\z/, @$kw)) {
297                         return;
298                 }
299                 $self->{pi_cfg}->each_inbox(\&remove_eml_i,
300                                 $self, $eml, "$uri #$art");
301         } else {
302                 die "BUG: destination unknown $inboxes";
303         }
304 }
305
306 sub imap_fetch_all ($$) {
307         my ($self, $uri) = @_;
308         my $warn_cb = $SIG{__WARN__} || \&CORE::warn;
309         $self->{incremental} = 1;
310         $self->{on_commit} = [ \&_done_for_now, $self ];
311         local $self->{cur_uid};
312         local $SIG{__WARN__} = sub {
313                 my $pfx = ($_[0] // '') =~ /^([A-Z]: |# )/g ? $1 : '';
314                 my $uid = $self->{cur_uid};
315                 $warn_cb->("$pfx$uri", $uid ? ("UID:$uid") : (), "\n", @_);
316         };
317         PublicInbox::NetReader::imap_each($self, $uri, \&net_cb, $self,
318                                         $self->{imap}->{$$uri});
319 }
320
321 sub imap_idle_once ($$$$) {
322         my ($self, $mic, $intvl, $uri) = @_;
323         my $i = $intvl //= (29 * 60);
324         my $end = now() + $intvl;
325         warn "# $uri idling for ${intvl}s\n";
326         local $0 = "IDLE $0";
327         return if $self->{quit};
328         unless ($mic->idle) {
329                 return if $self->{quit};
330                 return "E: IDLE failed on $uri: $!";
331         }
332         $self->{idle_mic} = $mic; # for ->quit
333         my @res;
334         until ($self->{quit} || !$mic->IsConnected ||
335                         grep(/^\* [0-9]+ EXISTS/, @res) || $i <= 0) {
336                 @res = $mic->idle_data($i);
337                 $i = $end - now();
338         }
339         delete $self->{idle_mic};
340         unless ($self->{quit}) {
341                 $mic->IsConnected or return "E: IDLE disconnected on $uri";
342                 $mic->done or return "E: IDLE DONE failed on $uri: $!";
343         }
344         undef;
345 }
346
347 # idles on a single URI
348 sub watch_imap_idle_1 ($$$) {
349         my ($self, $uri, $intvl) = @_;
350         my $sec = uri_section($uri);
351         my $mic_arg = $self->{net_arg}->{$sec} or
352                         die "BUG: no Mail::IMAPClient->new arg for $sec";
353         my $mic;
354         local $0 = $uri->mailbox." $sec";
355         until ($self->{quit}) {
356                 $mic //= PublicInbox::NetReader::mic_new(
357                                         $self, $mic_arg, $sec, $uri);
358                 my $err;
359                 if ($mic && $mic->IsConnected) {
360                         local $self->{mics_cached}->{$sec} = $mic;
361                         my $m = imap_fetch_all($self, $uri);
362                         $m == $mic or die "BUG: wrong mic";
363                         $mic->IsConnected and
364                                 $err = imap_idle_once($self, $mic, $intvl, $uri)
365                 } else {
366                         $err = "E: not connected: $!";
367                 }
368                 if ($err && !$self->{quit}) {
369                         warn $err, "\n";
370                         $mic = undef;
371                         sleep 60 unless $self->{quit};
372                 }
373         }
374 }
375
376 sub watch_atfork_child ($) {
377         my ($self) = @_;
378         delete $self->{pids};
379         delete $self->{opendirs};
380         PublicInbox::DS->Reset;
381         my $sig = delete $self->{sig};
382         $sig->{CHLD} = 'DEFAULT';
383         @SIG{keys %$sig} = values %$sig;
384         PublicInbox::DS::sig_setmask($self->{oldset});
385 }
386
387 sub watch_atfork_parent ($) { _done_for_now($_[0]) }
388
389 sub imap_idle_requeue { # DS::add_timer callback
390         my ($self, $uri, $intvl) = @_;
391         return if $self->{quit};
392         push @{$self->{idle_todo}}, $uri, $intvl;
393         event_step($self);
394 }
395
396 sub imap_idle_reap { # awaitpid callback
397         my ($pid, $self, $uri, $intvl) = @_;
398         delete $self->{pids}->{$pid};
399         return if $self->{quit};
400         warn "W: PID=$pid on $uri died: \$?=$?\n" if $?;
401         add_timer(60, \&imap_idle_requeue, $self, $uri, $intvl);
402 }
403
404 sub imap_idle_fork {
405         my ($self, $uri, $intvl) = @_;
406         return if $self->{quit};
407         my $seed = rand(0xffffffff);
408         my $pid = fork // die "fork: $!";
409         if ($pid == 0) {
410                 srand($seed);
411                 eval { Net::SSLeay::randomize() };
412                 watch_atfork_child($self);
413                 watch_imap_idle_1($self, $uri, $intvl);
414                 _exit(0);
415         }
416         $self->{pids}->{$pid} = undef;
417         awaitpid($pid, \&imap_idle_reap, $self, $uri, $intvl);
418 }
419
420 sub event_step {
421         my ($self) = @_;
422         return if $self->{quit};
423         my $idle_todo = $self->{idle_todo};
424         if ($idle_todo && @$idle_todo) {
425                 watch_atfork_parent($self);
426                 eval {
427                         while (my ($uri, $intvl) = splice(@$idle_todo, 0, 2)) {
428                                 imap_idle_fork($self, $uri, $intvl);
429                         }
430                 };
431                 die $@ if $@;
432         }
433         fs_scan_step($self) if $self->{mdre};
434 }
435
436 sub watch_imap_fetch_all ($$) {
437         my ($self, $uris) = @_;
438         for my $uri (@$uris) {
439                 imap_fetch_all($self, $uri);
440                 last if $self->{quit};
441         }
442 }
443
444 sub watch_nntp_fetch_all ($$) {
445         my ($self, $uris) = @_;
446         $self->{incremental} = 1;
447         $self->{on_commit} = [ \&_done_for_now, $self ];
448         my $warn_cb = $SIG{__WARN__} || \&CORE::warn;
449         local $self->{cur_uid};
450         my $uri = '';
451         local $SIG{__WARN__} = sub {
452                 my $pfx = ($_[0] // '') =~ /^([A-Z]: |# )/g ? $1 : '';
453                 my $art = $self->{cur_uid};
454                 $warn_cb->("$pfx$uri", $art ? ("ARTICLE $art") : (), "\n", @_);
455         };
456         for $uri (@$uris) {
457                 PublicInbox::NetReader::nntp_each($self, $uri, \&net_cb, $self,
458                                         $self->{nntp}->{$$uri});
459                 last if $self->{quit};
460         }
461 }
462
463 sub poll_fetch_fork { # DS::add_timer callback
464         my ($self, $intvl, $uris) = @_;
465         return if $self->{quit};
466         watch_atfork_parent($self);
467         my @nntp;
468         my @imap = grep { # push() always returns > 0
469                 $_->scheme =~ m!\Aimaps?!i ? 1 : (push(@nntp, $_) < 0)
470         } @$uris;
471         my $seed = rand(0xffffffff);
472         my $pid = fork // die "fork: $!";
473         if ($pid == 0) {
474                 srand($seed);
475                 eval { Net::SSLeay::randomize() };
476                 watch_atfork_child($self);
477                 watch_imap_fetch_all($self, \@imap) if @imap;
478                 watch_nntp_fetch_all($self, \@nntp) if @nntp;
479                 _exit(0);
480         }
481         $self->{pids}->{$pid} = undef;
482         awaitpid($pid, \&poll_fetch_reap, $self, $intvl, $uris);
483 }
484
485 sub poll_fetch_reap { # awaitpid callback
486         my ($pid, $self, $intvl, $uris) = @_;
487         delete $self->{pids}->{$pid};
488         return if $self->{quit};
489         if ($?) {
490                 warn "W: PID=$pid died: \$?=$?\n", map { "$_\n" } @$uris;
491         }
492         warn("# will check $_ in ${intvl}s\n") for @$uris;
493         add_timer($intvl, \&poll_fetch_fork, $self, $intvl, $uris);
494 }
495
496 sub watch_imap_init ($$) {
497         my ($self, $poll) = @_;
498         my $mics = PublicInbox::NetReader::imap_common_init($self);
499         my $idle = []; # [ uri1, intvl1, uri2, intvl2 ]
500         for my $uri (@{$self->{imap_order}}) {
501                 my $sec = uri_section($uri);
502                 my $mic = $mics->{$sec};
503                 my $intvl = $self->{cfg_opt}->{$sec}->{pollInterval};
504                 if ($mic->has_capability('IDLE') && !$intvl) {
505                         $intvl = $self->{cfg_opt}->{$sec}->{idleInterval};
506                         push @$idle, $uri, $intvl;
507                 } else {
508                         push @{$poll->{$intvl || 120}}, $uri;
509                 }
510         }
511         if (scalar @$idle) {
512                 $self->{idle_todo} = $idle;
513                 PublicInbox::DS::requeue($self); # ->event_step to fork
514         }
515 }
516
517 sub watch_nntp_init ($$) {
518         my ($self, $poll) = @_;
519         PublicInbox::NetReader::nntp_common_init($self);
520         for my $uri (@{$self->{nntp_order}}) {
521                 my $sec = uri_section($uri);
522                 my $intvl = $self->{cfg_opt}->{$sec}->{pollInterval};
523                 push @{$poll->{$intvl || 120}}, $uri;
524         }
525 }
526
527 sub watch { # main entry point
528         my ($self, $sig, $oldset) = @_;
529         $self->{oldset} = $oldset;
530         $self->{sig} = $sig;
531         my $poll = {}; # intvl_seconds => [ uri1, uri2 ]
532         watch_imap_init($self, $poll) if $self->{imap};
533         watch_nntp_init($self, $poll) if $self->{nntp};
534         while (my ($intvl, $uris) = each %$poll) {
535                 # poll all URIs for a given interval sequentially
536                 add_timer(0, \&poll_fetch_fork, $self, $intvl, $uris);
537         }
538         watch_fs_init($self) if $self->{mdre};
539         PublicInbox::DS->SetPostLoopCallback(sub { !$self->quit_done });
540         PublicInbox::DS::event_loop($sig, $oldset); # calls ->event_step
541         _done_for_now($self);
542 }
543
544 sub trigger_scan {
545         my ($self, $op) = @_;
546         push @{$self->{ops}}, $op;
547         PublicInbox::DS::requeue($self);
548 }
549
550 sub fs_scan_step {
551         my ($self) = @_;
552         return if $self->{quit};
553         my $op = shift @{$self->{ops}};
554         local $PublicInbox::DS::in_loop = 0; # waitpid() synchronously
555
556         # continue existing scan
557         my $opendirs = $self->{opendirs};
558         my @dirnames = keys %$opendirs;
559         foreach my $dir (@dirnames) {
560                 my $dh = delete $opendirs->{$dir};
561                 my $n = $self->{max_batch};
562                 while (my $fn = readdir($dh)) {
563                         _try_path($self, "$dir/$fn");
564                         last if --$n < 0;
565                 }
566                 $opendirs->{$dir} = $dh if $n < 0;
567         }
568         if ($op && $op eq 'full') {
569                 foreach my $dir (keys %{$self->{mdmap}}) {
570                         next if $opendirs->{$dir}; # already in progress
571                         my $ok = opendir(my $dh, $dir);
572                         unless ($ok) {
573                                 warn "failed to open $dir: $!\n";
574                                 next;
575                         }
576                         my $n = $self->{max_batch};
577                         while (my $fn = readdir($dh)) {
578                                 _try_path($self, "$dir/$fn");
579                                 last if --$n < 0;
580                         }
581                         $opendirs->{$dir} = $dh if $n < 0;
582                 }
583         }
584         _done_for_now($self);
585         # do we have more work to do?
586         PublicInbox::DS::requeue($self) if keys %$opendirs;
587 }
588
589 sub scan {
590         my ($self, $op) = @_;
591         push @{$self->{ops}}, $op;
592         fs_scan_step($self);
593 }
594
595 sub _importer_for {
596         my ($self, $ibx) = @_;
597         my $importers = $self->{importers};
598         my $im = $importers->{"$ibx"} ||= $ibx->importer(0);
599         if (scalar(keys(%$importers)) > 2) {
600                 delete $importers->{"$ibx"};
601                 _done_for_now($self);
602         }
603
604         $importers->{"$ibx"} = $im;
605 }
606
607 # XXX consider sharing with V2Writable, this only requires read-only access
608 sub content_exists ($$) {
609         my ($ibx, $eml) = @_;
610         my $over = $ibx->over or return;
611         my $mids = mids($eml);
612         my $chash = content_hash($eml);
613         my ($id, $prev);
614         for my $mid (@$mids) {
615                 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
616                         my $cmp = $ibx->smsg_eml($smsg) or return;
617                         return 1 if $chash eq content_hash($cmp);
618                 }
619         }
620         undef;
621 }
622
623 sub _spamcheck_cb {
624         my ($sc) = @_;
625         sub { # this gets called by (V2Writable||Import)->add
626                 my ($mime, $ibx) = @_;
627                 return if content_exists($ibx, $mime);
628                 my $tmp = '';
629                 if ($sc->spamcheck($mime, \$tmp)) {
630                         return PublicInbox::Eml->new(\$tmp);
631                 }
632                 warn $mime->header('Message-ID')." failed spam check\n";
633                 undef;
634         }
635 }
636
637 sub is_maildir {
638         $_[0] =~ s!\Amaildir:!! or return;
639         $_[0] =~ tr!/!/!s;
640         $_[0] =~ s!/\z!!;
641         $_[0];
642 }
643
644 sub is_watchspam {
645         my ($cur, $ws, $ibx) = @_;
646         if ($ws && !ref($ws) && $ws eq 'watchspam') {
647                 warn <<EOF;
648 E: $cur is a spam folder and cannot be used for `$ibx->{name}' input
649 EOF
650                 return 1;
651         }
652         undef;
653 }
654
655 sub folder_select { 'select' } # for PublicInbox::NetReader
656
657 1;