1 # Copyright (C) 2016 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 Email::MIME::ContentType;
11 $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
13 use PublicInbox::Import;
15 use PublicInbox::Spawn qw(spawn);
18 my ($class, $config) = @_;
19 my (%mdmap, @mdir, $spamc);
21 # "publicinboxwatch" is the documented namespace
22 # "publicinboxlearn" is legacy but may be supported
24 foreach my $pfx (qw(publicinboxwatch publicinboxlearn)) {
25 my $k = "$pfx.watchspam";
26 if (my $spamdir = $config->{$k}) {
27 if ($spamdir =~ s/\Amaildir://) {
29 # skip "new", no MUA has seen it, yet.
30 my $cur = "$spamdir/cur";
32 $mdmap{$cur} = 'watchspam';
34 warn "unsupported $k=$spamdir\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,
89 $_->done foreach values %{$_[0]->{importers}};
93 my ($self, $paths) = @_;
94 _try_path($self, $_->{path}) foreach @$paths;
99 my ($self, $path) = @_;
100 # path must be marked as (S)een
101 $path =~ /:2,[A-R]*S[T-Za-z]*\z/ or return;
102 my $mime = _path_to_mime($path) or return;
104 $self->{config}->each_inbox(sub {
107 my $im = _importer_for($self, $ibx);
109 if (my $scrub = _scrubber_for($ibx)) {
110 my $scrubbed = $scrub->scrub($mime) or return;
111 $scrubbed == 100 and return;
112 $im->remove($scrubbed);
116 warn "error removing spam at: ", $path,
117 " from ", $ibx->{name}, ': ', $@, "\n";
122 # used to hash the relevant portions of a message when there are conflicts
126 my $dig = Digest::SHA->new('SHA-1');
127 $dig->add($mime->header_obj->header_raw('Subject'));
128 $dig->add($mime->body_raw);
134 # probably a bad idea, but we inject a Message-Id if
135 # one is missing, here..
136 my $mid = $mime->header_obj->header_raw('Message-Id');
137 if (!defined $mid || $mid =~ /\A\s*\z/) {
138 $mid = '<' . _hash_mime2($mime) . '@generated>';
139 $mime->header_set('Message-Id', $mid);
144 my ($self, $path) = @_;
145 my @p = split(m!/+!, $path);
146 return if $p[-1] !~ /\A[a-zA-Z0-9][\w:,=\.]+\z/;
147 if ($p[-1] =~ /:2,([A-Z]+)\z/i) {
149 return if $flags =~ /[DT]/; # no [D]rafts or [T]rashed mail
151 return unless -f $path;
152 if ($path !~ $self->{mdre}) {
153 warn "unrecognized path: $path\n";
156 my $inbox = $self->{mdmap}->{$1};
158 warn "unmappable dir: $1\n";
161 if (!ref($inbox) && $inbox eq 'watchspam') {
162 return _remove_spam($self, $path);
164 my $im = _importer_for($self, $inbox);
165 my $mime = _path_to_mime($path) or return;
166 $mime->header_set($_) foreach @PublicInbox::MDA::BAD_HEADERS;
167 my $wm = $inbox->{-watchheader};
169 my $v = $mime->header_obj->header_raw($wm->[0]);
170 return unless ($v && $v =~ $wm->[1]);
172 if (my $scrub = _scrubber_for($inbox)) {
173 my $ret = $scrub->scrub($mime) or return;
174 $ret == 100 and return;
179 $im->add($mime, $self->{spamcheck});
184 my $cb = sub { _try_fsn_paths($self, \@_) };
185 my $mdir = $self->{mdir};
187 # lazy load here, we may support watching via IMAP IDLE
189 require Filesys::Notify::Simple;
190 my $watcher = Filesys::Notify::Simple->new($mdir);
191 $watcher->wait($cb) while (1);
196 my $mdir = $self->{mdir};
197 foreach my $dir (@$mdir) {
198 my $ok = opendir(my $dh, $dir);
200 warn "failed to open $dir: $!\n";
203 while (my $fn = readdir($dh)) {
204 _try_path($self, "$dir/$fn");
208 _done_for_now($self);
213 if (open my $fh, '<', $path) {
217 return PublicInbox::MIME->new(\$str);
218 } elsif ($!{ENOENT}) {
221 warn "failed to open $path: $!\n";
227 my ($self, $inbox) = @_;
228 my $im = $inbox->{-import} ||= eval {
229 my $git = $inbox->git;
230 my $name = $inbox->{name};
231 my $addr = $inbox->{-primary_address};
232 PublicInbox::Import->new($git, $name, $addr, $inbox);
235 my $importers = $self->{importers};
236 if (scalar(keys(%$importers)) > 2) {
237 delete $importers->{"$im"};
238 _done_for_now($self);
241 $importers->{"$im"} = $im;
246 my $f = $inbox->{filter};
247 if ($f && $f =~ /::/) {
248 my @args = (-inbox => $inbox);
249 # basic line splitting, only
250 # Perhaps we can have proper quote splitting one day...
251 ($f, @args) = split(/\s+/, $f) if $f =~ /\s+/;
257 # e.g: PublicInbox::Filter::Vger->new(@args)
258 return $f->new(@args);
269 if ($sc->spamcheck($mime, \$tmp)) {
270 return PublicInbox::MIME->new(\$tmp);
272 warn $mime->header('Message-ID')." failed spam check\n";