1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Streaming interface for formatting messages as an mboxrd.
5 # Used by the web interface
6 package PublicInbox::Mbox;
9 use PublicInbox::MID qw/mid_clean/;
10 use URI::Escape qw/uri_escape_utf8/;
12 require Email::Simple;
15 my $simple = Email::Simple->new(pop);
19 # single message should be easily renderable in browsers
20 my $fh = $response->([200, ['Content-Type'=>'text/plain']]);
21 emit_msg($ctx, $fh, $simple);
27 my ($ctx, $fh, $simple) = @_; # Email::Simple object
28 my $header_obj = $simple->header_obj;
30 # drop potentially confusing headers, ssoma already should've dropped
31 # Lines and Content-Length
32 foreach my $d (qw(Lines Bytes Content-Length Status)) {
33 $header_obj->header_set($d);
35 my $ibx = $ctx->{-inbox};
36 my $base = $ibx->base_url($ctx->{cgi});
37 my $mid = mid_clean($header_obj->header('Message-ID'));
38 $mid = uri_escape_utf8($mid);
40 'Archived-At', "<$base$mid/>",
41 'List-Archive', "<$base>",
42 'List-Post', "<mailto:$ibx->{-primary_address}>",
45 my $crlf = $simple->crlf;
46 for (my $i = 0; $i < @append; $i += 2) {
48 my $v = $append[$i + 1];
49 my @v = $header_obj->header($k);
56 $append .= "$k: $v$crlf" if defined $v;
58 my $buf = $header_obj->as_string;
59 unless ($buf =~ /\AFrom /) {
60 $fh->write("From mboxrd\@z Thu Jan 1 00:00:00 1970\n");
63 $fh->write($buf .= $append);
66 $simple->body_set('');
68 # mboxrd quoting style
69 # ref: http://www.qmail.org/man/man5/mbox.html
70 $buf =~ s/^(>*From )/>$1/gm;
72 $fh->write($buf .= "\n");
78 my ($ctx, $srch, $sfx) = @_;
79 eval { require IO::Compress::Gzip };
80 return sub { need_gzip(@_) } if $@;
82 my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
83 # http://www.iana.org/assignments/media-types/application/gzip
84 [200, ['Content-Type' => 'application/gzip'],
85 PublicInbox::MboxGz->new($ctx, $cb) ];
89 my ($ctx, $range) = @_;
91 eval { require IO::Compress::Gzip };
92 return sub { need_gzip(@_) } if $@;
94 if ($range eq 'all') { # TODO: YYYY[-MM]
97 return [404, [qw(Content-Type text/plain)], []];
99 my $cb = sub { $ctx->{srch}->query($query, @_) };
101 # http://www.iana.org/assignments/media-types/application/gzip
102 [200, [qw(Content-Type application/gzip)],
103 PublicInbox::MboxGz->new($ctx, $cb) ];
107 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
108 my $title = 'gzipped mbox not available';
110 <html><head><title>$title</title><body><pre>$title
111 The administrator needs to install the IO::Compress::Gzip Perl module
112 to support gzipped mboxes.
113 <a href="../">Return to index</a></pre></body></html>
120 package PublicInbox::MboxGz;
123 use PublicInbox::MID qw(mid2path);
126 my ($class, $ctx, $cb) = @_;
130 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
134 opts => { asc => 1, offset => 0 },
140 my $ret = $self->{buf};
142 ${$self->{buf}} = undef;
146 # called by Plack::Util::foreach or similar
150 my $ctx = $self->{ctx};
151 my $git = $ctx->{git};
153 while (defined(my $smsg = shift @{$self->{msgs}})) {
155 my $p = 'HEAD:'.mid2path($smsg->mid);
156 Email::Simple->new($git->cat_file($p));
160 PublicInbox::Mbox::emit_msg($ctx, $self->{gz}, $msg);
161 my $ret = _flush_buf($self);
164 $res = $self->{cb}->($self->{opts});
165 $self->{msgs} = $res->{msgs};
166 $res = scalar @{$self->{msgs}};
167 $self->{opts}->{offset} += $res;