package PublicInbox::MboxReader;
use strict;
use v5.10.1;
-use PublicInbox::DS (); # localize $in_loop for error detection :<
use Data::Dumper;
$Data::Dumper::Useqq = 1; # should've been the default, for bad data
my $from_strict =
qr/^From \S+ +\S+ \S+ +\S+ [^\n:]+:[^\n:]+:[^\n:]+ [^\n:]+\n/sm;
+# cf: https://doc.dovecot.org/configuration_manual/mail_location/mbox/
+my %status2kw = (F => 'flagged', A => 'answered', R => 'seen', T => 'draft');
+# O (old/non-recent), and D (deleted) aren't in JMAP,
+# so probably won't be supported by us.
+sub mbox_keywords {
+ my $eml = $_[-1];
+ my $s = "@{[$eml->header_raw('X-Status'),$eml->header_raw('Status')]}";
+ my %kw;
+ $s =~ s/([FART])/$kw{$status2kw{$1}} = 1/sge;
+ [ sort(keys %kw) ];
+}
+
sub _mbox_from {
my ($mbfh, $from_re, $eml_cb, @arg) = @_;
- local $PublicInbox::DS::in_loop; # disable dwaitpid
my $buf = '';
my @raw;
while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) {
}
@raw = grep /[^ \t\r\n]/s, @raw; # skip empty messages
while (defined(my $raw = shift @raw)) {
- $raw =~ s/\r?\n\z//s;
+ $raw =~ s/^\r?\n\z//ms;
$raw =~ s/$from_re/$1/gms;
my $eml = PublicInbox::Eml->new(\$raw);
- $eml_cb->($eml, @arg);
+ $eml_cb->($eml, @arg) if $eml->raw_size;
}
return if $r == 0; # EOF
}
sub _mbox_cl ($$$;@) {
my ($mbfh, $uxs_from, $eml_cb, @arg) = @_;
- local $PublicInbox::DS::in_loop; # disable dwaitpid
my $buf = '';
while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) {
if ($r == 0) { # detect "curl --fail"
$$hdr =~ s/\A[\r\n]*From [^\n]*\n//s or
die "E: no 'From ' line in:\n", Dumper($hdr);
my $eml = PublicInbox::Eml->new($hdr);
+ next unless $eml->raw_size;
my @cl = $eml->header_raw('Content-Length');
my $n = scalar(@cl);
$n == 0 and die "E: Content-Length missing in:\n",
sub new { bless \(my $x), __PACKAGE__ }
+sub reads {
+ my $ifmt = $_[-1];
+ $ifmt =~ /\Ambox(?:rd|cl|cl2|o)\z/ ? __PACKAGE__->can($ifmt) : undef
+}
+
+# all of these support -c for stdout and -d for decompression,
+# mutt is commonly distributed with hooks for gz, bz2 and xz, at least
+# { foo => '' } means "--foo" is passed to the command-line,
+# otherwise { foo => '--bar' } passes "--bar"
+my %zsfx2cmd = (
+ gz => [ qw(GZIP pigz gzip), { rsyncable => '' } ],
+ bz2 => [ 'bzip2', {} ],
+ xz => [ 'xz', {} ],
+ # don't add new entries here unless MUA support is widely available
+);
+
+sub zsfx ($) {
+ my ($pathname) = @_;
+ my $allow = join('|', keys %zsfx2cmd);
+ $pathname =~ /\.($allow)\z/ ? $1 : undef;
+}
+
+sub zsfx2cmd ($$$) {
+ my ($zsfx, $decompress, $lei) = @_;
+ my $x = $zsfx2cmd{$zsfx} // die "BUG: no support for suffix=.$zsfx";
+ my @info = @$x;
+ my $cmd_opt = pop @info;
+ my @cmd = (undef, $decompress ? qw(-dc) : qw(-c));
+ require PublicInbox::Spawn;
+ for my $exe (@info) {
+ # I think respecting client's ENV{GZIP} is OK, not sure
+ # about ENV overrides for other, less-common compressors
+ if ($exe eq uc($exe)) {
+ $exe = $lei->{env}->{$exe} or next;
+ }
+ $cmd[0] = PublicInbox::Spawn::which($exe) and last;
+ }
+ $cmd[0] // die join(' or ', @info)." missing for .$zsfx";
+ # push @cmd, @{$cmd_opt->{-default}} if $cmd_opt->{-default};
+ for my $bool (qw(rsyncable)) {
+ my $switch = $cmd_opt->{rsyncable} // next;
+ push @cmd, '--'.($switch || $bool);
+ }
+ for my $key (qw(rsyncable)) { # support compression level?
+ my $switch = $cmd_opt->{$key} // next;
+ my $val = $lei->{opt}->{$key} // next;
+ push @cmd, $switch, $val;
+ }
+ \@cmd;
+}
+
+sub zsfxcat ($$$) {
+ my ($in, $zsfx, $lei) = @_;
+ my $cmd = zsfx2cmd($zsfx, 1, $lei);
+ PublicInbox::Spawn::popen_rd($cmd, undef, { 0 => $in, 2 => $lei->{2} });
+}
+
1;