1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Streaming interface for mboxrd HTTP responses
5 # See PublicInbox::GzipFilter for details.
6 package PublicInbox::Mbox;
8 use parent 'PublicInbox::GzipFilter';
9 use PublicInbox::MID qw/mid_escape/;
10 use PublicInbox::Hval qw/to_filename/;
11 use PublicInbox::Smsg;
14 # called by PSGI server as body response
15 # this gets called twice for every message, once to return the header,
16 # once to retrieve the body
19 my $smsg = $ctx->{smsg} or return;
20 my $ibx = $ctx->{-inbox};
21 my $eml = $ibx->smsg_eml($smsg) or return;
22 my $n = $ctx->{smsg} = $ibx->over->next_by_mid(@{$ctx->{next_arg}});
23 $ctx->zmore(msg_hdr($ctx, $eml, $smsg->{mid}));
25 $ctx->translate(msg_body($eml));
26 } else { # last message
27 $ctx->zmore(msg_body($eml));
32 # called by PublicInbox::DS::write after http->next_step
34 my ($http) = @_; # PublicInbox::HTTP
35 my $ctx = $http->{forward} or return; # client aborted
37 my $smsg = $ctx->{smsg} or return $ctx->close;
38 $ctx->smsg_blob($smsg);
43 sub async_eml { # for async_blob_cb
45 my $smsg = delete $ctx->{smsg};
47 $ctx->{smsg} = $ctx->{-inbox}->over->next_by_mid(@{$ctx->{next_arg}});
49 $ctx->zmore(msg_hdr($ctx, $eml, $smsg->{mid}));
50 $ctx->{http_out}->write($ctx->translate(msg_body($eml)));
54 my ($ctx, $subject) = @_;
55 my $fn = $subject // '';
57 $fn = to_filename($fn) // 'no-subject';
58 my @hdr = ('Content-Type');
59 if ($ctx->{-inbox}->{obfuscate}) {
60 # obfuscation is stupid, but maybe scrapers are, too...
61 push @hdr, 'application/mbox';
64 push @hdr, 'text/plain';
67 push @hdr, 'Content-Disposition', "inline; filename=$fn";
71 # for rare cases where v1 inboxes aren't indexed w/ ->over at all
74 my $mref = $ctx->{-inbox}->msg_by_mid($ctx->{mid}) or return;
75 my $eml = PublicInbox::Eml->new($mref);
76 [ 200, res_hdr($ctx, $eml->header_str('Subject')),
77 [ msg_hdr($ctx, $eml, $ctx->{mid}) . msg_body($eml) ] ]
80 # /$INBOX/$MESSAGE_ID/raw
83 $ctx->{base_url} = $ctx->{-inbox}->base_url($ctx->{env});
84 my $over = $ctx->{-inbox}->over or return no_over_raw($ctx);
86 my $mip = $ctx->{next_arg} = [ $ctx->{mid}, \$id, \$prev ];
87 my $smsg = $ctx->{smsg} = $over->next_by_mid(@$mip) or return;
88 my $res_hdr = res_hdr($ctx, $smsg->{subject});
89 bless $ctx, __PACKAGE__;
90 $ctx->psgi_response(200, $res_hdr);
94 my ($ctx, $eml, $mid) = @_;
95 my $header_obj = $eml->header_obj;
97 # drop potentially confusing headers, ssoma already should've dropped
98 # Lines and Content-Length
99 foreach my $d (qw(Lines Bytes Content-Length Status)) {
100 $header_obj->header_set($d);
102 my $ibx = $ctx->{-inbox};
103 my $base = $ctx->{base_url};
104 $mid = $ctx->{mid} unless defined $mid;
105 $mid = mid_escape($mid);
107 'Archived-At', "<$base$mid/>",
108 'List-Archive', "<$base>",
109 'List-Post', "<mailto:$ibx->{-primary_address}>",
111 my $crlf = $header_obj->crlf;
112 my $buf = $header_obj->as_string;
113 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
114 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
115 $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf;
117 for (my $i = 0; $i < @append; $i += 2) {
119 my $v = $append[$i + 1];
120 my @v = $header_obj->header_raw($k);
127 $buf .= "$k: $v$crlf" if defined $v;
133 my $bdy = $_[0]->{bdy} // return "\n";
134 # mboxrd quoting style
135 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
136 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
137 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
138 $$bdy =~ s/^(>*From )/>$1/gm;
144 my $msgs = $ctx->{msgs};
146 if (my $smsg = shift @$msgs) {
150 $ctx->{msgs} = $msgs = $ctx->{over}->get_thread($ctx->{mid},
152 return unless @$msgs;
153 $ctx->{prev} = $msgs->[-1];
158 my ($ctx, $over, $sfx) = @_;
159 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
160 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
161 $ctx->{prev} = $msgs->[-1];
162 $ctx->{over} = $over; # bump refcnt
163 require PublicInbox::MboxGz;
164 PublicInbox::MboxGz::mbox_gz($ctx, \&thread_cb, $msgs->[0]->{subject});
168 my ($ctx, $range) = @_;
171 if ($range eq 'all') { # TODO: YYYY[-MM]
174 return [404, [qw(Content-Type text/plain)], []];
176 mbox_all($ctx, { q => $q });
181 my $ids = $ctx->{ids};
183 while ((my $num = shift @$ids)) {
184 my $smsg = $ctx->{over}->get_art($num) or next;
187 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
193 my $ibx = $ctx->{-inbox};
195 my $mm = $ctx->{mm} = $ibx->mm;
196 my $ids = $mm->ids_after(\$prev) or return
197 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
198 $ctx->{over} = $ibx->over or
199 return PublicInbox::WWW::need($ctx, 'Overview');
201 $ctx->{prev} = $prev;
202 require PublicInbox::MboxGz;
203 PublicInbox::MboxGz::mbox_gz($ctx, \&all_ids_cb, 'all');
208 my $over = $ctx->{-inbox}->over or return;
210 while (defined(my $num = shift(@{$ctx->{ids}}))) {
211 my $smsg = $over->get_art($num) or next;
215 my $srch = $ctx->{-inbox}->search(undef, $ctx) or return;
216 my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
217 my $size = $mset->size or return;
218 $ctx->{qopts}->{offset} += $size;
219 $ctx->{ids} = $srch->mset_to_artnums($mset);
223 sub results_thread_cb {
226 my $over = $ctx->{-inbox}->over or return;
228 while (defined(my $num = shift(@{$ctx->{xids}}))) {
229 my $smsg = $over->get_art($num) or next;
233 # refills ctx->{xids}
234 next if $over->expand_thread($ctx);
237 my $srch = $ctx->{-inbox}->search(undef, $ctx) or return;
238 my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
239 my $size = $mset->size or return;
240 $ctx->{qopts}->{offset} += $size;
241 $ctx->{ids} = $srch->mset_to_artnums($mset);
248 my $q_string = $q->{'q'};
249 return mbox_all_ids($ctx) if $q_string !~ /\S/;
250 my $srch = $ctx->{-inbox}->search or
251 return PublicInbox::WWW::need($ctx, 'Search');
252 my $over = $ctx->{-inbox}->over or
253 return PublicInbox::WWW::need($ctx, 'Overview');
255 my $qopts = $ctx->{qopts} = { mset => 2 }; # order by docid
256 $qopts->{thread} = 1 if $q->{t};
257 my $mset = $srch->mset($q_string, $qopts);
258 $qopts->{offset} = $mset->size or
259 return [404, [qw(Content-Type text/plain)],
260 ["No results found\n"]];
261 $ctx->{query} = $q_string;
262 $ctx->{ids} = $srch->mset_to_artnums($mset);
263 require PublicInbox::MboxGz;
265 if ($q->{t} && $srch->has_threadid) {
266 $fn = 'results-thread-'.$q_string;
267 PublicInbox::MboxGz::mbox_gz($ctx, \&results_thread_cb, $fn);
269 $fn = 'results-'.$q_string;
270 PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, $fn);