1 # Copyright (C) 2015, all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 # Streaming interface for formatting messages as an mbox
4 package PublicInbox::Mbox;
7 use PublicInbox::MID qw/mid_clean mid_compressed mid2path/;
8 use Fcntl qw(SEEK_SET);
11 my ($ctx, $srch) = @_;
13 my ($response) = @_; # Plack callback
14 my $w = $response->([200, ['Content-Type' => 'text/plain']]);
15 emit_mbox($w, $ctx, $srch);
20 my ($fh, $simple) = @_; # Email::Simple object
22 # drop potentially confusing headers, ssoma already should've dropped
23 # Lines and Content-Length
24 foreach my $d (qw(Lines Content-Length Status)) {
25 $simple->header_set($d);
28 my $buf = $simple->header_obj->as_string;
29 unless ($buf =~ /\AFrom /) {
30 $fh->write("From a\@a Thu Jan 1 00:00:00 1970\n");
32 $fh->write($buf .= $simple->crlf);
35 $simple->body_set('');
36 $buf =~ s/^(From )/>$1/gm;
37 $buf .= "\n" unless $buf =~ /\n\z/s;
43 my ($fh, $ctx, $srch) = @_;
45 require PublicInbox::GitCatFile;
46 require Email::Simple;
47 my $mid = mid_compressed($ctx->{mid});
48 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
49 my %opts = (offset => 0);
52 my $res = $srch->get_thread($mid, \%opts);
53 my $msgs = $res->{msgs};
55 while (defined(my $smsg = shift @$msgs)) {
57 my $p = 'HEAD:'.mid2path($smsg->mid);
58 Email::Simple->new($git->cat_file($p));
60 emit_msg($fh, $msg) if $msg;