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 # reader for mbox variants we support
5 package PublicInbox::MboxReader;
9 $Data::Dumper::Useqq = 1; # should've been the default, for bad data
12 qr/^From \S+ +\S+ \S+ +\S+ [^\n:]+:[^\n:]+:[^\n:]+ [^\n:]+\n/sm;
14 # cf: https://doc.dovecot.org/configuration_manual/mail_location/mbox/
15 my %status2kw = (F => 'flagged', A => 'answered', R => 'seen', T => 'draft');
16 # O (old/non-recent), and D (deleted) aren't in JMAP,
17 # so probably won't be supported by us.
20 my $s = "@{[$eml->header_raw('X-Status'),$eml->header_raw('Status')]}";
22 $s =~ s/([FART])/$kw{$status2kw{$1}} = 1/sge;
27 my ($mbfh, $from_re, $eml_cb, @arg) = @_;
30 while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) {
31 if ($r == 0) { # close here to check for "curl --fail"
32 close($mbfh) or die "error closing mbox: \$?=$? $!";
35 @raw = split(/$from_strict/mos, $buf, -1);
36 next if scalar(@raw) == 0;
37 $buf = pop(@raw); # last bit may be incomplete
39 @raw = grep /[^ \t\r\n]/s, @raw; # skip empty messages
40 while (defined(my $raw = shift @raw)) {
41 $raw =~ s/^\r?\n\z//ms;
42 $raw =~ s/$from_re/$1/gms;
43 my $eml = PublicInbox::Eml->new(\$raw);
44 $eml_cb->($eml, @arg);
46 return if $r == 0; # EOF
48 die "error reading mboxo/mboxrd handle: $!";
52 my (undef, $mbfh, $eml_cb, @arg) = @_;
53 _mbox_from($mbfh, qr/^>(>*From )/ms, $eml_cb, @arg);
57 my (undef, $mbfh, $eml_cb, @arg) = @_;
58 _mbox_from($mbfh, qr/^>(From )/ms, $eml_cb, @arg);
62 my ($mbfh, $bref, $cl) = @_;
63 my $body = substr($$bref, 0, $cl, '');
64 my $need = $cl - length($body);
66 $mbfh or die "E: needed $need bytes after EOF";
67 defined(my $r = read($mbfh, $body, $need, length($body))) or
68 die "E: read error: $!\n";
69 $r == $need or die "E: read $r of $need bytes\n";
76 if (index($$ref, "\r\n") < 0 && (my $pos = index($$ref, "\n\n")) >= 0) {
78 \substr($$ref, 0, $pos + 2, ''); # sv_chop on $$ref
79 } elsif ($$ref =~ /\r?\n\r?\n/s) {
80 \substr($$ref, 0, $+[0], ''); # sv_chop on $$ref
86 sub _mbox_cl ($$$;@) {
87 my ($mbfh, $uxs_from, $eml_cb, @arg) = @_;
89 while (defined(my $r = read($mbfh, $buf, 65536, length($buf)))) {
90 if ($r == 0) { # detect "curl --fail"
92 die "error closing mboxcl/mboxcl2: \$?=$? $!";
95 while (my $hdr = _extract_hdr(\$buf)) {
96 $$hdr =~ s/\A[\r\n]*From [^\n]*\n//s or
97 die "E: no 'From ' line in:\n", Dumper($hdr);
98 my $eml = PublicInbox::Eml->new($hdr);
99 my @cl = $eml->header_raw('Content-Length');
101 $n == 0 and die "E: Content-Length missing in:\n",
102 Dumper($eml->as_string);
103 $n == 1 or die "E: multiple ($n) Content-Length in:\n",
104 Dumper($eml->as_string);
105 $cl[0] =~ /\A[0-9]+\z/ or die
106 "E: Content-Length `$cl[0]' invalid\n",
107 Dumper($eml->as_string);
108 if (($eml->{bdy} = _cl_body($mbfh, \$buf, $cl[0]))) {
110 ${$eml->{bdy}} =~ s/^>From /From /sgm;
112 $eml_cb->($eml, @arg);
115 $buf =~ /[^ \r\n\t]/ and
116 warn "W: leftover at end of mboxcl/mboxcl2:\n",
121 die "error reading mboxcl/mboxcl2 handle: $!";
125 my (undef, $mbfh, $eml_cb, @arg) = @_;
126 _mbox_cl($mbfh, 1, $eml_cb, @arg);
130 my (undef, $mbfh, $eml_cb, @arg) = @_;
131 _mbox_cl($mbfh, undef, $eml_cb, @arg);
134 sub new { bless \(my $x), __PACKAGE__ }