]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MdirReader.pm
net_reader: new package split from -watch
[public-inbox.git] / lib / PublicInbox / MdirReader.pm
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>
3
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;
8 use strict;
9 use v5.10.1;
10
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"
17 }
18
19 # same as above, but for full path name
20 sub maildir_path_flags {
21         my ($f) = @_;
22         my $i = rindex($f, '/');
23         $i >= 0 ? maildir_basename_flags(substr($f, $i + 1)) : undef;
24 }
25
26 sub maildir_each_file ($$;@) {
27         my ($dir, $cb, @arg) = @_;
28         $dir .= '/' unless substr($dir, -1) eq '/';
29         for my $d (qw(new/ cur/)) {
30                 my $pfx = $dir.$d;
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);
35                 }
36         }
37 }
38
39 1;