-sub emit_mbox {
- my ($response, $ctx, $srch) = @_;
- eval { require IO::Compress::Gzip };
- return need_gzip($response) if $@;
-
- # http://www.iana.org/assignments/media-types/application/gzip
- # http://www.iana.org/assignments/media-types/application/mbox
- my $fh = $response->([200, ['Content-Type' => 'application/gzip']]);
- $fh = PublicInbox::MboxGz->new($fh);
-
- require PublicInbox::GitCatFile;
- require Email::Simple;
- my $mid = mid_compressed($ctx->{mid});
- my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
- my %opts = (offset => 0);
- my $nr;
- do {
- my $res = $srch->get_thread($mid, \%opts);
- my $msgs = $res->{msgs};
- $nr = scalar @$msgs;
- while (defined(my $smsg = shift @$msgs)) {
- my $msg = eval {
- my $p = 'HEAD:'.mid2path($smsg->mid);
- Email::Simple->new($git->cat_file($p));
- };
- emit_msg($fh, $msg) if $msg;
+sub msg_hdr ($$;$) {
+ my ($ctx, $eml, $mid) = @_;
+ my $header_obj = $eml->header_obj;
+
+ # drop potentially confusing headers, ssoma already should've dropped
+ # Lines and Content-Length
+ foreach my $d (qw(Lines Bytes Content-Length Status)) {
+ $header_obj->header_set($d);
+ }
+ my $ibx = $ctx->{-inbox};
+ my $base = $ctx->{base_url};
+ $mid = $ctx->{mid} unless defined $mid;
+ $mid = mid_escape($mid);
+ my @append = (
+ 'Archived-At', "<$base$mid/>",
+ 'List-Archive', "<$base>",
+ 'List-Post', "<mailto:$ibx->{-primary_address}>",
+ );
+ my $crlf = $header_obj->crlf;
+ my $buf = $header_obj->as_string;
+ # fixup old bug from import (pre-a0c07cba0e5d8b6a)
+ $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+ $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf;
+
+ for (my $i = 0; $i < @append; $i += 2) {
+ my $k = $append[$i];
+ my $v = $append[$i + 1];
+ my @v = $header_obj->header_raw($k);
+ foreach (@v) {
+ if ($v eq $_) {
+ $v = undef;
+ last;
+ }