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 (via getline) interface for formatting messages as an mboxrd.
5 # Used by the PSGI web interface.
7 # public-inbox-httpd favors "getline" response bodies to take a
8 # "pull"-based approach to feeding slow clients (as opposed to a
9 # more common "push" model)
10 package PublicInbox::Mbox;
13 use PublicInbox::MID qw/mid_clean mid_escape/;
14 use PublicInbox::Hval qw/to_filename/;
16 use Email::MIME::Encode;
20 my $fn = $simple->header('Subject');
21 return 'no-subject' unless defined($fn);
23 # no need for full Email::MIME, here
25 eval { $fn = Encode::decode('MIME-Header', $fn) };
26 $fn = 'no-subject' if $@;
29 $fn = to_filename($fn);
30 $fn eq '' ? 'no-subject' : $fn;
35 bless $more, 'PublicInbox::Mbox';
38 # called by PSGI server as body response
40 my ($more) = @_; # self
41 my ($ctx, $id, $prev, $next, $cur) = @$more;
44 return msg_str($ctx, $cur);
46 $cur = $next or return;
47 my $ibx = $ctx->{-inbox};
48 $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
49 @$more = ($ctx, $id, $prev, $next); # $next may be undef, here
50 my $mref = $ibx->msg_by_smsg($cur) or return;
51 msg_str($ctx, Email::Simple->new($mref));
58 my $mid = $ctx->{mid};
59 my $ibx = $ctx->{-inbox};
62 if (my $over = $ibx->over) {
64 my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
65 my $mref = $ibx->msg_by_smsg($smsg) or return;
66 $first = Email::Simple->new($mref);
67 my $next = $over->next_by_mid($mid, \$id, \$prev);
68 # $more is for ->getline
69 $more = [ $ctx, $id, $prev, $next, $first ] if $next;
71 my $mref = $ibx->msg_by_mid($mid) or return;
72 $first = Email::Simple->new($mref);
74 return unless defined $first;
75 my $fn = subject_fn($first);
76 my @hdr = ('Content-Type');
77 if ($ibx->{obfuscate}) {
78 # obfuscation is stupid, but maybe scrapers are, too...
79 push @hdr, 'application/mbox';
82 push @hdr, 'text/plain';
85 push @hdr, 'Content-Disposition', "inline; filename=$fn";
86 [ 200, \@hdr, $more ? mb_stream($more) : [ msg_str($ctx, $first) ] ];
90 my ($ctx, $simple, $mid) = @_; # Email::Simple object
91 my $header_obj = $simple->header_obj;
93 # drop potentially confusing headers, ssoma already should've dropped
94 # Lines and Content-Length
95 foreach my $d (qw(Lines Bytes Content-Length Status)) {
96 $header_obj->header_set($d);
98 my $ibx = $ctx->{-inbox};
99 my $base = $ibx->base_url($ctx->{env});
100 $mid = $ctx->{mid} unless defined $mid;
101 $mid = mid_escape($mid);
103 'Archived-At', "<$base$mid/>",
104 'List-Archive', "<$base>",
105 'List-Post', "<mailto:$ibx->{-primary_address}>",
107 my $crlf = $simple->crlf;
108 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
109 $header_obj->as_string;
110 for (my $i = 0; $i < @append; $i += 2) {
112 my $v = $append[$i + 1];
113 my @v = $header_obj->header($k);
120 $buf .= "$k: $v$crlf" if defined $v;
124 # mboxrd quoting style
125 # ref: http://www.qmail.org/man/man5/mbox.html
126 my $body = $simple->body;
127 $body =~ s/^(>*From )/>$1/gm;
133 my ($ctx, $over, $sfx) = @_;
134 eval { require IO::Compress::Gzip };
135 return sub { need_gzip(@_) } if $@;
136 my $mid = $ctx->{mid};
137 my $msgs = $over->get_thread($mid, {});
138 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
139 my $prev = $msgs->[-1];
143 if (my $smsg = $msgs->[$i++]) {
147 $msgs = $over->get_thread($mid, $prev);
148 return unless @$msgs;
153 PublicInbox::MboxGz->response($ctx, $cb, $msgs->[0]->subject);
157 my ($ctx, $range) = @_;
160 if ($range eq 'all') { # TODO: YYYY[-MM]
163 return [404, [qw(Content-Type text/plain)], []];
165 mbox_all($ctx, $query);
171 my $ibx = $ctx->{-inbox};
172 my $ids = $ibx->mm->ids_after(\$prev) or return
173 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
175 my $over = $ibx->over or
176 return PublicInbox::WWW::need($ctx, 'Overview');
179 while ((my $num = $ids->[$i++])) {
180 my $smsg = $over->get_art($num) or next;
183 $ids = $ibx->mm->ids_after(\$prev);
188 return PublicInbox::MboxGz->response($ctx, $cb, 'all');
192 my ($ctx, $query) = @_;
194 eval { require IO::Compress::Gzip };
195 return sub { need_gzip(@_) } if $@;
196 return mbox_all_ids($ctx) if $query eq '';
197 my $opts = { mset => 2 };
198 my $srch = $ctx->{-inbox}->search or
199 return PublicInbox::WWW::need($ctx, 'Search');;
200 my $mset = $srch->query($query, $opts);
201 $opts->{offset} = $mset->size or
202 return [404, [qw(Content-Type text/plain)],
203 ["No results found\n"]];
205 my $cb = sub { # called by MboxGz->getline
207 while (my $mi = (($mset->items)[$i++])) {
208 my $doc = $mi->get_document;
209 my $smsg = $srch->retry_reopen(sub {
210 PublicInbox::SearchMsg->load_doc($doc);
215 $mset = $srch->query($query, $opts);
216 my $size = $mset->size or return;
217 $opts->{offset} += $size;
221 PublicInbox::MboxGz->response($ctx, $cb, 'results-'.$query);
225 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
226 my $title = 'gzipped mbox not available';
228 <html><head><title>$title</title><body><pre>$title
229 The administrator needs to install the IO::Compress::Gzip Perl module
230 to support gzipped mboxes.
231 <a href="../">Return to index</a></pre></body></html>
238 package PublicInbox::MboxGz;
241 use PublicInbox::Hval qw/to_filename/;
244 my ($class, $ctx, $cb) = @_;
248 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
255 my ($class, $ctx, $cb, $fn) = @_;
256 my $body = $class->new($ctx, $cb);
257 # http://www.iana.org/assignments/media-types/application/gzip
258 my @h = qw(Content-Type application/gzip);
260 $fn = to_filename($fn);
261 push @h, 'Content-Disposition', "inline; filename=$fn.mbox.gz";
266 # called by Plack::Util::foreach or similar
269 my $ctx = $self->{ctx} or return;
270 while (my $smsg = $self->{cb}->()) {
271 my $msg = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
272 $msg = Email::Simple->new($msg);
273 $self->{gz}->write(PublicInbox::Mbox::msg_str($ctx, $msg,
275 my $bref = $self->{buf};
276 if (length($$bref) >= 8192) {
277 my $ret = $$bref; # copy :<
278 ${$self->{buf}} = '';
282 # be fair to other clients on public-inbox-httpd:
285 delete($self->{gz})->close;
286 # signal that we're done and can return undef next call:
288 ${delete $self->{buf}};