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);
16 # single message should be easily renderable in browsers,
17 # unless obfuscation is enabled :<
18 [ 200, [ 'Content-Type',
19 $ctx->{-inbox}->{obfuscate} ? 'application/mbox' : 'text/plain' ],
20 [ msg_str($ctx, $msg)] ]
24 my ($ctx, $simple) = @_; # Email::Simple object
25 my $header_obj = $simple->header_obj;
27 # drop potentially confusing headers, ssoma already should've dropped
28 # Lines and Content-Length
29 foreach my $d (qw(Lines Bytes Content-Length Status)) {
30 $header_obj->header_set($d);
32 my $ibx = $ctx->{-inbox};
33 my $base = $ibx->base_url($ctx->{env});
34 my $mid = mid_clean($header_obj->header('Message-ID'));
35 $mid = mid_escape($mid);
37 'Archived-At', "<$base$mid/>",
38 'List-Archive', "<$base>",
39 'List-Post', "<mailto:$ibx->{-primary_address}>",
41 my $crlf = $simple->crlf;
42 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
43 $header_obj->as_string;
44 for (my $i = 0; $i < @append; $i += 2) {
46 my $v = $append[$i + 1];
47 my @v = $header_obj->header($k);
54 $buf .= "$k: $v$crlf" if defined $v;
58 # mboxrd quoting style
59 # ref: http://www.qmail.org/man/man5/mbox.html
60 my $body = $simple->body;
61 $body =~ s/^(>*From )/>$1/gm;
67 my ($ctx, $srch, $sfx) = @_;
68 eval { require IO::Compress::Gzip };
69 return sub { need_gzip(@_) } if $@;
71 my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
72 # http://www.iana.org/assignments/media-types/application/gzip
73 [200, ['Content-Type' => 'application/gzip'],
74 PublicInbox::MboxGz->new($ctx, $cb) ];
78 my ($ctx, $range) = @_;
80 eval { require IO::Compress::Gzip };
81 return sub { need_gzip(@_) } if $@;
83 if ($range eq 'all') { # TODO: YYYY[-MM]
86 return [404, [qw(Content-Type text/plain)], []];
88 my $cb = sub { $ctx->{srch}->query($query, @_) };
90 # http://www.iana.org/assignments/media-types/application/gzip
91 [200, [qw(Content-Type application/gzip)],
92 PublicInbox::MboxGz->new($ctx, $cb) ];
96 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
97 my $title = 'gzipped mbox not available';
99 <html><head><title>$title</title><body><pre>$title
100 The administrator needs to install the IO::Compress::Gzip Perl module
101 to support gzipped mboxes.
102 <a href="../">Return to index</a></pre></body></html>
109 package PublicInbox::MboxGz;
114 my ($class, $ctx, $cb) = @_;
118 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
122 opts => { offset => 0 },
126 # called by Plack::Util::foreach or similar
129 my $ctx = $self->{ctx} or return;
131 my $ibx = $ctx->{-inbox};
132 my $gz = $self->{gz};
134 while (defined(my $smsg = shift @{$self->{msgs}})) {
135 my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
136 $msg = Email::Simple->new($msg);
137 $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
138 my $bref = $self->{buf};
139 if (length($$bref) >= 8192) {
140 my $ret = $$bref; # copy :<
141 ${$self->{buf}} = '';
145 # be fair to other clients on public-inbox-httpd:
148 $res = $self->{cb}->($self->{opts});
149 $self->{msgs} = $res->{msgs};
150 $res = scalar @{$self->{msgs}};
151 $self->{opts}->{offset} += $res;
155 ${delete $self->{buf}};