1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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 use PublicInbox::Hval qw/to_filename/;
12 use Email::MIME::Encode;
16 my $fn = $simple->header('Subject');
17 return 'no-subject' unless defined($fn);
19 # no need for full Email::MIME, here
21 eval { $fn = Encode::decode('MIME-Header', $fn) };
22 $fn = 'no-subject' if $@;
25 $fn = to_filename($fn);
26 $fn eq '' ? 'no-subject' : $fn;
31 bless $more, 'PublicInbox::Mbox';
34 # called by PSGI server as body response
36 my ($more) = @_; # self
37 my ($ctx, $head, $tail, $db, $cur) = @$more;
40 return msg_str($ctx, $cur);
42 for (; !defined($cur) && $head != $tail; $head++) {
43 my $smsg = PublicInbox::SearchMsg->get($head, $db, $ctx->{mid});
44 my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
45 $cur = Email::Simple->new($mref);
46 $cur = msg_str($ctx, $cur);
56 my $mid = $ctx->{mid};
57 my $ibx = $ctx->{-inbox};
60 my ($head, $tail, $db);
62 if (my $srch = $ibx->search) {
63 $srch->retry_reopen(sub {
64 ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
65 for (; !defined($first) && $head != $tail; $head++) {
66 my @args = ($head, $db, $mid);
67 my $smsg = PublicInbox::SearchMsg->get(@args);
68 my $mref = $ibx->msg_by_smsg($smsg) or next;
69 $first = Email::Simple->new($mref);
72 $more = [ $ctx, $head, $tail, $db, $first ];
76 my $mref = $ibx->msg_by_mid($mid) or return;
77 $first = Email::Simple->new($mref);
79 return unless defined $first;
80 my $fn = subject_fn($first);
81 my @hdr = ('Content-Type');
82 if ($ibx->{obfuscate}) {
83 # obfuscation is stupid, but maybe scrapers are, too...
84 push @hdr, 'application/mbox';
87 push @hdr, 'text/plain';
90 push @hdr, 'Content-Disposition', "inline; filename=$fn";
91 [ 200, \@hdr, $more ? mb_stream($more) : [ msg_str($ctx, $first) ] ];
95 my ($ctx, $simple, $mid) = @_; # Email::Simple object
96 my $header_obj = $simple->header_obj;
98 # drop potentially confusing headers, ssoma already should've dropped
99 # Lines and Content-Length
100 foreach my $d (qw(Lines Bytes Content-Length Status)) {
101 $header_obj->header_set($d);
103 my $ibx = $ctx->{-inbox};
104 my $base = $ibx->base_url($ctx->{env});
105 $mid = $ctx->{mid} unless defined $mid;
106 $mid = mid_escape($mid);
108 'Archived-At', "<$base$mid/>",
109 'List-Archive', "<$base>",
110 'List-Post', "<mailto:$ibx->{-primary_address}>",
112 my $crlf = $simple->crlf;
113 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
114 $header_obj->as_string;
115 for (my $i = 0; $i < @append; $i += 2) {
117 my $v = $append[$i + 1];
118 my @v = $header_obj->header($k);
125 $buf .= "$k: $v$crlf" if defined $v;
129 # mboxrd quoting style
130 # ref: http://www.qmail.org/man/man5/mbox.html
131 my $body = $simple->body;
132 $body =~ s/^(>*From )/>$1/gm;
138 my ($ctx, $srch, $sfx) = @_;
139 eval { require IO::Compress::Gzip };
140 return sub { need_gzip(@_) } if $@;
143 my $msgs = $srch->get_thread($ctx->{mid}, $prev);
144 $prev = $msgs->[-1]->{num} if scalar(@$msgs);
147 PublicInbox::MboxGz->response($ctx, $cb);
151 my ($ctx, $range) = @_;
154 if ($range eq 'all') { # TODO: YYYY[-MM]
157 return [404, [qw(Content-Type text/plain)], []];
159 mbox_all($ctx, $query);
163 my ($ctx, $query) = @_;
165 eval { require IO::Compress::Gzip };
166 return sub { need_gzip(@_) } if $@;
169 my $cb = sub { $ctx->{-inbox}->mm->ids_after(\$prev) };
170 return PublicInbox::MboxGz->response($ctx, $cb, 'all');
172 my $opts = { offset => 0 };
173 my $srch = $ctx->{srch};
174 my $cb = sub { # called by MboxGz->getline
175 my $msgs = $srch->query($query, $opts);
176 $opts->{offset} += scalar @$msgs;
179 PublicInbox::MboxGz->response($ctx, $cb, 'results-'.$query);
183 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
184 my $title = 'gzipped mbox not available';
186 <html><head><title>$title</title><body><pre>$title
187 The administrator needs to install the IO::Compress::Gzip Perl module
188 to support gzipped mboxes.
189 <a href="../">Return to index</a></pre></body></html>
196 package PublicInbox::MboxGz;
199 use PublicInbox::Hval qw/to_filename/;
202 my ($class, $ctx, $cb) = @_;
206 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
214 my ($class, $ctx, $cb, $fn) = @_;
215 my $body = $class->new($ctx, $cb);
216 # http://www.iana.org/assignments/media-types/application/gzip
217 $body->{hdr} = [ 'Content-Type', 'application/gzip' ];
219 my $hdr = $body->getline; # fill in Content-Disposition filename
220 [ 200, $hdr, $body ];
223 sub set_filename ($$) {
225 return to_filename($fn) if defined($fn);
227 PublicInbox::Mbox::subject_fn($msg);
230 # called by Plack::Util::foreach or similar
233 my $ctx = $self->{ctx} or return;
234 my $ibx = $ctx->{-inbox};
235 my $gz = $self->{gz};
236 my $msgs = $self->{msgs};
238 # work on existing result set
239 while (defined(my $smsg = shift @$msgs)) {
240 # ids_after may return integers
242 $smsg = $ctx->{srch}->{over_ro}->get_art($smsg);
244 my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
245 $msg = Email::Simple->new($msg);
246 $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg,
249 # use subject of first message as subject
250 if (my $hdr = delete $self->{hdr}) {
251 my $fn = set_filename($self->{fn}, $msg);
252 push @$hdr, 'Content-Disposition',
253 "inline; filename=$fn.mbox.gz";
256 my $bref = $self->{buf};
257 if (length($$bref) >= 8192) {
258 my $ret = $$bref; # copy :<
259 ${$self->{buf}} = '';
263 # be fair to other clients on public-inbox-httpd:
268 $msgs = $self->{msgs} = $self->{cb}->();
271 # signal that we're done and can return undef next call:
273 ${delete $self->{buf}};