]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/MdirReader.pm
lei convert: mail format conversion sub-command
[public-inbox.git] / lib / PublicInbox / MdirReader.pm
index e0ff676deb19eb656a37e0e30fc187a29732405d..5fa534f58cbb139913155b3529d8b16e2e26cd83 100644 (file)
@@ -7,6 +7,7 @@
 package PublicInbox::MdirReader;
 use strict;
 use v5.10.1;
+use PublicInbox::InboxWritable qw(eml_from_path);
 
 # returns Maildir flags from a basename ('' for no flags, undef for invalid)
 sub maildir_basename_flags {
@@ -36,4 +37,29 @@ sub maildir_each_file ($$;@) {
        }
 }
 
+my %c2kw = ('D' => 'draft', F => 'flagged', R => 'answered', S => 'seen');
+
+sub maildir_each_eml ($$;@) {
+       my ($dir, $cb, @arg) = @_;
+       $dir .= '/' unless substr($dir, -1) eq '/';
+       my $pfx = "$dir/new/";
+       if (opendir(my $dh, $pfx)) {
+               while (defined(my $bn = readdir($dh))) {
+                       next if substr($bn, 0, 1) eq '.';
+                       my @f = split(/:/, $bn, -1);
+                       next if scalar(@f) != 1;
+                       my $eml = eml_from_path($pfx.$bn) or next;
+                       $cb->([], $eml, @arg);
+               }
+       }
+       $pfx = "$dir/cur/";
+       opendir my $dh, $pfx or return;
+       while (defined(my $bn = readdir($dh))) {
+               my $fl = maildir_basename_flags($bn) // next;
+               my $eml = eml_from_path($pfx.$bn) or next;
+               my @kw = sort(map { $c2kw{$_} // () } split(//, $fl));
+               $cb->(\@kw, $eml, @arg);
+       }
+}
+
 1;