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