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);
17 my ($class, $config) = @_;
18 my (%mdmap, @mdir, $spamc, $spamdir);
20 # "publicinboxwatch" is the documented namespace
21 # "publicinboxlearn" is legacy but may be supported
23 foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) {
24 my $k = "$pfx.watchspam";
25 if (my $dir = $config->{$k}) {
26 if ($dir =~ s/\Amaildir://) {
28 # skip "new", no MUA has seen it, yet.
32 $mdmap{$cur} = 'watchspam';
34 warn "unsupported $k=$dir\n";
39 my $k = 'publicinboxwatch.spamcheck';
40 my $spamcheck = $config->{$k};
42 if ($spamcheck eq 'spamc') {
43 $spamcheck = 'PublicInbox::Spamcheck::Spamc';
45 if ($spamcheck =~ /::/) {
46 eval "require $spamcheck";
47 $spamcheck = _spamcheck_cb($spamcheck->new);
49 warn "unsupported $k=$spamcheck\n";
53 foreach $k (keys %$config) {
54 $k =~ /\Apublicinbox\.([^\.]+)\.watch\z/ or next;
56 my $watch = $config->{$k};
57 if ($watch =~ s/\Amaildir://) {
59 my $inbox = $config->lookup_name($name);
60 if (my $wm = $inbox->{watchheader}) {
61 my ($k, $v) = split(/:/, $wm, 2);
62 $inbox->{-watchheader} = [ $k, qr/\Q$v\E/ ];
64 my $new = "$watch/new";
65 my $cur = "$watch/cur";
66 push @mdir, $new, $cur;
67 die "$new already in use\n" if $mdmap{$new};
68 die "$cur already in use\n" if $mdmap{$cur};
69 $mdmap{$new} = $mdmap{$cur} = $inbox;
71 warn "watch unsupported: $k=$watch\n";
76 my $mdre = join('|', map { quotemeta($_) } @mdir);
77 $mdre = qr!\A($mdre)/!;
79 spamcheck => $spamcheck,
86 opendirs => {}, # dirname => dirhandle (in progress scans)
92 my $importers = $self->{importers};
93 foreach my $im (values %$importers) {
97 my $opendirs = $self->{opendirs};
99 # spamdir scanning means every importer remains open
100 my $spamdir = $self->{spamdir};
101 return if defined($spamdir) && $opendirs->{$spamdir};
103 foreach my $im (values %$importers) {
104 # not done if we're scanning
105 next if $opendirs->{$im->{git}->{git_dir}};
111 my ($self, $scan_re, $paths) = @_;
113 my $path = $_->{path};
114 if ($path =~ $scan_re) {
117 _try_path($self, $path);
120 _done_for_now($self);
124 my ($self, $path) = @_;
125 # path must be marked as (S)een
126 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
127 my $mime = _path_to_mime($path) or return;
128 $self->{config}->each_inbox(sub {
131 my $im = _importer_for($self, $ibx);
132 $im->remove($mime, 'spam');
133 if (my $scrub = _scrubber_for($ibx)) {
134 my $scrubbed = $scrub->scrub($mime) or return;
135 $scrubbed == 100 and return;
136 $im->remove($scrubbed, 'spam');
140 warn "error removing spam at: ", $path,
141 " from ", $ibx->{name}, ': ', $@, "\n";
147 my ($self, $path) = @_;
148 my @p = split(m!/+!, $path);
149 return if $p[-1] !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
150 if ($p[-1] =~ /:2,([A-Z]+)\z/i) {
152 return if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
154 return unless -f $path;
155 if ($path !~ $self->{mdre}) {
156 warn "unrecognized path: $path\n";
159 my $inbox = $self->{mdmap}->{$1};
161 warn "unmappable dir: $1\n";
164 if (!ref($inbox) && $inbox eq 'watchspam') {
165 return _remove_spam($self, $path);
167 my $im = _importer_for($self, $inbox);
168 my $mime = _path_to_mime($path) or return;
169 $mime->header_set($_) foreach @PublicInbox::MDA::BAD_HEADERS;
170 my $wm = $inbox->{-watchheader};
172 my $v = $mime->header_obj->header_raw($wm->[0]);
173 return unless ($v && $v =~ $wm->[1]);
175 if (my $scrub = _scrubber_for($inbox)) {
176 my $ret = $scrub->scrub($mime) or return;
177 $ret == 100 and return;
181 $im->add($mime, $self->{spamcheck});
184 sub quit { trigger_scan($_[0], 'quit') }
188 my $scan = File::Temp->newdir("public-inbox-watch.$$.scan.XXXXXX",
190 my $scandir = $self->{scandir} = $scan->dirname;
191 my $re = qr!\A$scandir/!;
192 my $cb = sub { _try_fsn_paths($self, $re, \@_) };
194 # lazy load here, we may support watching via IMAP IDLE
196 require Filesys::Notify::Simple;
197 my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
198 $fsn->wait($cb) until $self->{quit};
202 my ($self, $base) = @_;
203 my $dir = $self->{scandir} or return;
204 open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
205 close $fh or die "close $dir/$base failed: $!\n";
209 my ($self, $path) = @_;
210 if ($path =~ /quit\z/) {
211 %{$self->{opendirs}} = ();
212 _done_for_now($self);
213 delete $self->{scandir};
217 # else: $path =~ /(cont|full)\z/
218 return if $self->{quit};
220 my $opendirs = $self->{opendirs};
221 my @dirnames = keys %$opendirs;
222 foreach my $dir (@dirnames) {
223 my $dh = delete $opendirs->{$dir};
225 while (my $fn = readdir($dh)) {
226 _try_path($self, "$dir/$fn");
229 $opendirs->{$dir} = $dh if $n < 0;
231 if ($path =~ /full\z/) {
232 foreach my $dir (@{$self->{mdir}}) {
233 next if $opendirs->{$dir}; # already in progress
234 my $ok = opendir(my $dh, $dir);
236 warn "failed to open $dir: $!\n";
240 while (my $fn = readdir($dh)) {
241 _try_path($self, "$dir/$fn");
244 $opendirs->{$dir} = $dh if $n < 0;
247 _done_for_now($self);
248 # do we have more work to do?
249 trigger_scan($self, 'cont') if keys %$opendirs;
254 if (open my $fh, '<', $path) {
258 return PublicInbox::MIME->new(\$str);
259 } elsif ($!{ENOENT}) {
262 warn "failed to open $path: $!\n";
268 my ($self, $inbox) = @_;
269 my $im = $inbox->{-import} ||= eval {
270 my $git = $inbox->git;
271 my $name = $inbox->{name};
272 my $addr = $inbox->{-primary_address};
273 PublicInbox::Import->new($git, $name, $addr, $inbox);
276 my $importers = $self->{importers};
277 if (scalar(keys(%$importers)) > 2) {
278 delete $importers->{"$im"};
279 _done_for_now($self);
282 $importers->{"$im"} = $im;
287 my $f = $inbox->{filter};
288 if ($f && $f =~ /::/) {
289 my @args = (-inbox => $inbox);
290 # basic line splitting, only
291 # Perhaps we can have proper quote splitting one day...
292 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
298 # e.g: PublicInbox::Filter::Vger->new(@args)
299 return $f->new(@args);
310 if ($sc->spamcheck($mime, \$tmp)) {
311 return PublicInbox::MIME->new(\$tmp);
313 warn $mime->header('Message-ID')." failed spam check\n";