1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Maildirs for now, MH eventually
5 # ref: https://cr.yp.to/proto/maildir.html
6 # https://wiki2.dovecot.org/MailboxFormat/Maildir
7 package PublicInbox::MdirReader;
10 use PublicInbox::InboxWritable qw(eml_from_path);
12 # returns Maildir flags from a basename ('' for no flags, undef for invalid)
13 sub maildir_basename_flags {
14 my (@f) = split(/:/, $_[0], -1);
15 return if (scalar(@f) > 2 || substr($f[0], 0, 1) eq '.');
16 $f[1] // return ''; # "new"
17 $f[1] =~ /\A2,([A-Za-z]*)\z/ ? $1 : undef; # "cur"
20 # same as above, but for full path name
21 sub maildir_path_flags {
23 my $i = rindex($f, '/');
24 $i >= 0 ? maildir_basename_flags(substr($f, $i + 1)) : undef;
27 sub maildir_each_file ($$;@) {
28 my ($dir, $cb, @arg) = @_;
29 $dir .= '/' unless substr($dir, -1) eq '/';
30 for my $d (qw(new/ cur/)) {
32 opendir my $dh, $pfx or next;
33 while (defined(my $bn = readdir($dh))) {
34 maildir_basename_flags($bn) // next;
35 $cb->($pfx.$bn, @arg);
40 my %c2kw = ('D' => 'draft', F => 'flagged', R => 'answered', S => 'seen');
42 sub maildir_each_eml ($$;@) {
43 my ($dir, $cb, @arg) = @_;
44 $dir .= '/' unless substr($dir, -1) eq '/';
45 my $pfx = "$dir/new/";
46 if (opendir(my $dh, $pfx)) {
47 while (defined(my $bn = readdir($dh))) {
48 next if substr($bn, 0, 1) eq '.';
49 my @f = split(/:/, $bn, -1);
50 next if scalar(@f) != 1;
51 my $eml = eml_from_path($pfx.$bn) or next;
52 $cb->([], $eml, @arg);
56 opendir my $dh, $pfx or return;
57 while (defined(my $bn = readdir($dh))) {
58 my $fl = maildir_basename_flags($bn) // next;
59 my $eml = eml_from_path($pfx.$bn) or next;
60 my @kw = sort(map { $c2kw{$_} // () } split(//, $fl));
61 $cb->(\@kw, $eml, @arg);