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;
11 # returns Maildir flags from a basename ('' for no flags, undef for invalid)
12 sub maildir_basename_flags {
13 my (@f) = split(/:/, $_[0], -1);
14 return if (scalar(@f) > 2 || substr($f[0], 0, 1) eq '.');
15 $f[1] // return ''; # "new"
16 $f[1] =~ /\A2,([A-Za-z]*)\z/ ? $1 : undef; # "cur"
19 # same as above, but for full path name
20 sub maildir_path_flags {
22 my $i = rindex($f, '/');
23 $i >= 0 ? maildir_basename_flags(substr($f, $i + 1)) : undef;
26 sub maildir_each_file ($$;@) {
27 my ($dir, $cb, @arg) = @_;
28 $dir .= '/' unless substr($dir, -1) eq '/';
29 for my $d (qw(new/ cur/)) {
31 opendir my $dh, $pfx or next;
32 while (defined(my $bn = readdir($dh))) {
33 maildir_basename_flags($bn) // next;
34 $cb->($pfx.$bn, @arg);