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);
15 use PublicInbox::MID qw(mids);
16 use PublicInbox::ContentId qw(content_digest);
19 my ($class, $config) = @_;
20 my (%mdmap, @mdir, $spamc, $spamdir);
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 ($dir =~ s/\Amaildir://) {
30 # skip "new", no MUA has seen it, yet.
34 $mdmap{$cur} = 'watchspam';
36 warn "unsupported $k=$dir\n";
41 my $k = 'publicinboxwatch.spamcheck';
42 my $spamcheck = $config->{$k};
44 if ($spamcheck eq 'spamc') {
45 $spamcheck = 'PublicInbox::Spamcheck::Spamc';
47 if ($spamcheck =~ /::/) {
48 eval "require $spamcheck";
49 $spamcheck = _spamcheck_cb($spamcheck->new);
51 warn "unsupported $k=$spamcheck\n";
55 foreach $k (keys %$config) {
56 $k =~ /\Apublicinbox\.([^\.]+)\.watch\z/ or next;
58 my $watch = $config->{$k};
59 if ($watch =~ s/\Amaildir://) {
61 my $inbox = $config->lookup_name($name);
62 if (my $wm = $inbox->{watchheader}) {
63 my ($k, $v) = split(/:/, $wm, 2);
64 $inbox->{-watchheader} = [ $k, qr/\Q$v\E/ ];
66 my $new = "$watch/new";
67 my $cur = "$watch/cur";
68 push @mdir, $new, $cur;
69 die "$new already in use\n" if $mdmap{$new};
70 die "$cur already in use\n" if $mdmap{$cur};
71 $mdmap{$new} = $mdmap{$cur} = $inbox;
73 warn "watch unsupported: $k=$watch\n";
78 my $mdre = join('|', map { quotemeta($_) } @mdir);
79 $mdre = qr!\A($mdre)/!;
81 spamcheck => $spamcheck,
88 opendirs => {}, # dirname => dirhandle (in progress scans)
94 my $importers = $self->{importers};
95 foreach my $im (values %$importers) {
99 my $opendirs = $self->{opendirs};
101 # spamdir scanning means every importer remains open
102 my $spamdir = $self->{spamdir};
103 return if defined($spamdir) && $opendirs->{$spamdir};
105 foreach my $im (values %$importers) {
106 # not done if we're scanning
107 next if $opendirs->{$im->{git}->{git_dir}};
113 my ($self, $scan_re, $paths) = @_;
115 my $path = $_->{path};
116 if ($path =~ $scan_re) {
119 _try_path($self, $path);
122 _done_for_now($self);
126 my ($self, $path) = @_;
127 # path must be marked as (S)een
128 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
129 my $mime = _path_to_mime($path) or return;
131 $self->{config}->each_inbox(sub {
134 my $im = _importer_for($self, $ibx);
135 $im->remove($mime, 'spam');
136 if (my $scrub = _scrubber_for($ibx)) {
137 my $scrubbed = $scrub->scrub($mime) or return;
138 $scrubbed == 100 and return;
139 $im->remove($scrubbed, 'spam');
143 warn "error removing spam at: ", $path,
144 " from ", $ibx->{name}, ': ', $@, "\n";
151 my $hdr = $mime->header_obj;
152 my $mids = mids($hdr);
154 my $dig = content_digest($mime);
155 my $mid = $dig->clone->hexdigest . '@localhost';
156 $hdr->header_set('Message-Id', $mid);
160 my ($self, $path) = @_;
161 my @p = split(m!/+!, $path);
162 return if $p[-1] !~ /\A[a-zA-Z0-9][\-\w:,=\.]+\z/;
163 if ($p[-1] =~ /:2,([A-Z]+)\z/i) {
165 return if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
167 return unless -f $path;
168 if ($path !~ $self->{mdre}) {
169 warn "unrecognized path: $path\n";
172 my $inbox = $self->{mdmap}->{$1};
174 warn "unmappable dir: $1\n";
177 if (!ref($inbox) && $inbox eq 'watchspam') {
178 return _remove_spam($self, $path);
180 my $im = _importer_for($self, $inbox);
181 my $mime = _path_to_mime($path) or return;
182 $mime->header_set($_) foreach @PublicInbox::MDA::BAD_HEADERS;
183 my $wm = $inbox->{-watchheader};
185 my $v = $mime->header_obj->header_raw($wm->[0]);
186 return unless ($v && $v =~ $wm->[1]);
188 if (my $scrub = _scrubber_for($inbox)) {
189 my $ret = $scrub->scrub($mime) or return;
190 $ret == 100 and return;
195 $im->add($mime, $self->{spamcheck});
198 sub quit { trigger_scan($_[0], 'quit') }
202 my $scan = File::Temp->newdir("public-inbox-watch.$$.scan.XXXXXX",
204 my $scandir = $self->{scandir} = $scan->dirname;
205 my $re = qr!\A$scandir/!;
206 my $cb = sub { _try_fsn_paths($self, $re, \@_) };
208 # lazy load here, we may support watching via IMAP IDLE
210 require Filesys::Notify::Simple;
211 my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
212 $fsn->wait($cb) until $self->{quit};
216 my ($self, $base) = @_;
217 my $dir = $self->{scandir} or return;
218 open my $fh, '>', "$dir/$base" or die "open $dir/$base failed: $!\n";
219 close $fh or die "close $dir/$base failed: $!\n";
223 my ($self, $path) = @_;
224 if ($path =~ /quit\z/) {
225 %{$self->{opendirs}} = ();
226 _done_for_now($self);
227 delete $self->{scandir};
231 # else: $path =~ /(cont|full)\z/
232 return if $self->{quit};
234 my $opendirs = $self->{opendirs};
235 my @dirnames = keys %$opendirs;
236 foreach my $dir (@dirnames) {
237 my $dh = delete $opendirs->{$dir};
239 while (my $fn = readdir($dh)) {
240 _try_path($self, "$dir/$fn");
243 $opendirs->{$dir} = $dh if $n < 0;
245 if ($path =~ /full\z/) {
246 foreach my $dir (@{$self->{mdir}}) {
247 next if $opendirs->{$dir}; # already in progress
248 my $ok = opendir(my $dh, $dir);
250 warn "failed to open $dir: $!\n";
254 while (my $fn = readdir($dh)) {
255 _try_path($self, "$dir/$fn");
258 $opendirs->{$dir} = $dh if $n < 0;
261 _done_for_now($self);
262 # do we have more work to do?
263 trigger_scan($self, 'cont') if keys %$opendirs;
268 if (open my $fh, '<', $path) {
272 return PublicInbox::MIME->new(\$str);
273 } elsif ($!{ENOENT}) {
276 warn "failed to open $path: $!\n";
282 my ($self, $inbox) = @_;
283 my $im = $inbox->{-import} ||= eval {
284 my $git = $inbox->git;
285 my $name = $inbox->{name};
286 my $addr = $inbox->{-primary_address};
287 PublicInbox::Import->new($git, $name, $addr, $inbox);
290 my $importers = $self->{importers};
291 if (scalar(keys(%$importers)) > 2) {
292 delete $importers->{"$im"};
293 _done_for_now($self);
296 $importers->{"$im"} = $im;
301 my $f = $inbox->{filter};
302 if ($f && $f =~ /::/) {
303 my @args = (-inbox => $inbox);
304 # basic line splitting, only
305 # Perhaps we can have proper quote splitting one day...
306 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
312 # e.g: PublicInbox::Filter::Vger->new(@args)
313 return $f->new(@args);
324 if ($sc->spamcheck($mime, \$tmp)) {
325 return PublicInbox::MIME->new(\$tmp);
327 warn $mime->header('Message-ID')." failed spam check\n";