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 mboxrd
4 package PublicInbox::Mbox;
7 use PublicInbox::MID qw/mid2path mid_clean/;
8 use URI::Escape qw/uri_escape_utf8/;
12 my ($ctx, $srch, $sfx) = @_;
14 my ($response) = @_; # Plack callback
15 emit_mbox($response, $ctx, $srch, $sfx);
20 my $simple = Email::Simple->new(pop);
24 # single message should be easily renderable in browsers
25 my $fh = $response->([200, ['Content-Type'=>'text/plain']]);
26 emit_msg($ctx, $fh, $simple);
32 my ($ctx, $fh, $simple) = @_; # Email::Simple object
33 my $header_obj = $simple->header_obj;
35 # drop potentially confusing headers, ssoma already should've dropped
36 # Lines and Content-Length
37 foreach my $d (qw(Lines Bytes Content-Length Status)) {
38 $header_obj->header_set($d);
40 my $feed_opts = $ctx->{feed_opts};
42 require PublicInbox::Feed; # FIXME: gross
43 $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
44 $ctx->{feed_opts} = $feed_opts;
46 my $base = $feed_opts->{url};
47 my $mid = mid_clean($header_obj->header('Message-ID'));
48 $mid = uri_escape_utf8($mid);
49 my @archived_at = $header_obj->header('Archived-At');
50 push @archived_at, "<$base$mid/>";
51 $header_obj->header_set('Archived-At', @archived_at);
52 $header_obj->header_set('List-Archive', "<$base>");
53 $header_obj->header_set('List-Post', "<mailto:$feed_opts->{id_addr}>");
55 my $buf = $header_obj->as_string;
56 unless ($buf =~ /\AFrom /) {
57 $fh->write("From mboxrd\@z Thu Jan 1 00:00:00 1970\n");
59 $fh->write($buf .= $simple->crlf);
62 $simple->body_set('');
64 # mboxrd quoting style
65 # ref: http://www.qmail.org/man/man5/mbox.html
66 $buf =~ s/^(>*From )/>$1/gm;
68 $buf .= "\n" unless $buf =~ /\n\z/s;
74 my ($response, $ctx, $srch, $sfx) = @_;
77 eval { require IO::Compress::Gzip };
78 return need_gzip($response) if $@;
82 # http://www.iana.org/assignments/media-types/application/gzip
83 # http://www.iana.org/assignments/media-types/application/mbox
84 my $fh = $response->([200, ['Content-Type' => "application/$type"]]);
85 $fh = PublicInbox::MboxGz->new($fh) if $sfx;
87 require PublicInbox::GitCatFile;
88 my $mid = $ctx->{mid};
89 my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
90 my %opts = (offset => 0);
93 my $res = $srch->get_thread($mid, \%opts);
94 my $msgs = $res->{msgs};
96 while (defined(my $smsg = shift @$msgs)) {
98 my $p = 'HEAD:'.mid2path($smsg->mid);
99 Email::Simple->new($git->cat_file($p));
101 emit_msg($ctx, $fh, $msg) if $msg;
104 $opts{offset} += $nr;
111 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
112 my $title = 'gzipped mbox not available';
114 <html><head><title>$title</title><body><pre>$title
115 The administrator needs to install the IO::Compress::Gzip Perl module
116 to support gzipped mboxes.
117 <a href="../">Return to index</a></pre></body></html>
124 # fh may not be a proper IO, so we wrap the write and close methods
125 # to prevent IO::Compress::Gzip from complaining
126 package PublicInbox::MboxGz;
131 my ($class, $fh) = @_;
135 gz => IO::Compress::Gzip->new(\$buf),
142 if (defined ${$self->{buf}}) {
143 $self->{fh}->write(${$self->{buf}});
144 ${$self->{buf}} = undef;
149 $_[0]->{gz}->write($_[1]);