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 mid_escape/;
10 require Email::Simple;
14 $msg = Email::Simple->new($msg);
15 # single message should be easily renderable in browsers
16 [200, ['Content-Type', 'text/plain'], [ msg_str($ctx, $msg)] ]
20 my ($ctx, $simple) = @_; # Email::Simple object
21 my $header_obj = $simple->header_obj;
23 # drop potentially confusing headers, ssoma already should've dropped
24 # Lines and Content-Length
25 foreach my $d (qw(Lines Bytes Content-Length Status)) {
26 $header_obj->header_set($d);
28 my $ibx = $ctx->{-inbox};
29 my $base = $ibx->base_url($ctx->{env});
30 my $mid = mid_clean($header_obj->header('Message-ID'));
31 $mid = mid_escape($mid);
33 'Archived-At', "<$base$mid/>",
34 'List-Archive', "<$base>",
35 'List-Post', "<mailto:$ibx->{-primary_address}>",
37 my $crlf = $simple->crlf;
38 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
39 $header_obj->as_string;
40 for (my $i = 0; $i < @append; $i += 2) {
42 my $v = $append[$i + 1];
43 my @v = $header_obj->header($k);
50 $buf .= "$k: $v$crlf" if defined $v;
54 # mboxrd quoting style
55 # ref: http://www.qmail.org/man/man5/mbox.html
56 my $body = $simple->body;
57 $body =~ s/^(>*From )/>$1/gm;
63 my ($ctx, $srch, $sfx) = @_;
64 eval { require IO::Compress::Gzip };
65 return sub { need_gzip(@_) } if $@;
67 my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
68 # http://www.iana.org/assignments/media-types/application/gzip
69 [200, ['Content-Type' => 'application/gzip'],
70 PublicInbox::MboxGz->new($ctx, $cb) ];
74 my ($ctx, $range) = @_;
76 eval { require IO::Compress::Gzip };
77 return sub { need_gzip(@_) } if $@;
79 if ($range eq 'all') { # TODO: YYYY[-MM]
82 return [404, [qw(Content-Type text/plain)], []];
84 my $cb = sub { $ctx->{srch}->query($query, @_) };
86 # http://www.iana.org/assignments/media-types/application/gzip
87 [200, [qw(Content-Type application/gzip)],
88 PublicInbox::MboxGz->new($ctx, $cb) ];
92 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
93 my $title = 'gzipped mbox not available';
95 <html><head><title>$title</title><body><pre>$title
96 The administrator needs to install the IO::Compress::Gzip Perl module
97 to support gzipped mboxes.
98 <a href="../">Return to index</a></pre></body></html>
105 package PublicInbox::MboxGz;
110 my ($class, $ctx, $cb) = @_;
114 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
118 opts => { offset => 0 },
122 # called by Plack::Util::foreach or similar
125 my $ctx = $self->{ctx} or return;
127 my $ibx = $ctx->{-inbox};
128 my $gz = $self->{gz};
130 while (defined(my $smsg = shift @{$self->{msgs}})) {
131 my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
132 $msg = Email::Simple->new($msg);
133 $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
134 my $bref = $self->{buf};
135 if (length($$bref) >= 8192) {
136 my $ret = $$bref; # copy :<
137 ${$self->{buf}} = '';
141 # be fair to other clients on public-inbox-httpd:
144 $res = $self->{cb}->($self->{opts});
145 $self->{msgs} = $res->{msgs};
146 $res = scalar @{$self->{msgs}};
147 $self->{opts}->{offset} += $res;
151 ${delete $self->{buf}};