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/;
11 require Email::Simple;
15 $msg = Email::Simple->new($msg);
16 # single message should be easily renderable in browsers
17 [200, ['Content-Type', 'text/plain'], [ msg_str($ctx, $msg)] ]
21 my ($ctx, $simple) = @_; # Email::Simple object
22 my $header_obj = $simple->header_obj;
24 # drop potentially confusing headers, ssoma already should've dropped
25 # Lines and Content-Length
26 foreach my $d (qw(Lines Bytes Content-Length Status)) {
27 $header_obj->header_set($d);
29 my $ibx = $ctx->{-inbox};
30 my $base = $ibx->base_url($ctx->{env});
31 my $mid = mid_clean($header_obj->header('Message-ID'));
32 $mid = uri_escape_utf8($mid);
34 'Archived-At', "<$base$mid/>",
35 'List-Archive', "<$base>",
36 'List-Post', "<mailto:$ibx->{-primary_address}>",
38 my $crlf = $simple->crlf;
39 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
40 $header_obj->as_string;
41 for (my $i = 0; $i < @append; $i += 2) {
43 my $v = $append[$i + 1];
44 my @v = $header_obj->header($k);
51 $buf .= "$k: $v$crlf" if defined $v;
55 # mboxrd quoting style
56 # ref: http://www.qmail.org/man/man5/mbox.html
57 my $body = $simple->body;
58 $body =~ s/^(>*From )/>$1/gm;
64 my ($ctx, $srch, $sfx) = @_;
65 eval { require IO::Compress::Gzip };
66 return sub { need_gzip(@_) } if $@;
68 my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
69 # http://www.iana.org/assignments/media-types/application/gzip
70 [200, ['Content-Type' => 'application/gzip'],
71 PublicInbox::MboxGz->new($ctx, $cb) ];
75 my ($ctx, $range) = @_;
77 eval { require IO::Compress::Gzip };
78 return sub { need_gzip(@_) } if $@;
80 if ($range eq 'all') { # TODO: YYYY[-MM]
83 return [404, [qw(Content-Type text/plain)], []];
85 my $cb = sub { $ctx->{srch}->query($query, @_) };
87 # http://www.iana.org/assignments/media-types/application/gzip
88 [200, [qw(Content-Type application/gzip)],
89 PublicInbox::MboxGz->new($ctx, $cb) ];
93 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
94 my $title = 'gzipped mbox not available';
96 <html><head><title>$title</title><body><pre>$title
97 The administrator needs to install the IO::Compress::Gzip Perl module
98 to support gzipped mboxes.
99 <a href="../">Return to index</a></pre></body></html>
106 package PublicInbox::MboxGz;
111 my ($class, $ctx, $cb) = @_;
115 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
119 opts => { asc => 1, offset => 0 },
123 # called by Plack::Util::foreach or similar
126 my $ctx = $self->{ctx} or return;
128 my $ibx = $ctx->{-inbox};
129 my $gz = $self->{gz};
131 while (defined(my $smsg = shift @{$self->{msgs}})) {
132 my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
133 $msg = Email::Simple->new($msg);
134 $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
135 my $bref = $self->{buf};
136 if (length($$bref) >= 8192) {
137 my $ret = $$bref; # copy :<
138 ${$self->{buf}} = '';
142 $res = $self->{cb}->($self->{opts});
143 $self->{msgs} = $res->{msgs};
144 $res = scalar @{$self->{msgs}};
145 $self->{opts}->{offset} += $res;
149 ${delete $self->{buf}};