1 # Copyright (C) 2015-2021 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->{ibx};
21 my $eml = delete($ctx->{eml}) // $ibx->smsg_eml($smsg) // return;
22 my $n = $ctx->{smsg} = $ibx->over->next_by_mid(@{$ctx->{next_arg}});
23 $ctx->zmore(msg_hdr($ctx, $eml));
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->{ibx}->over->next_by_mid(@{$ctx->{next_arg}});
48 local $ctx->{eml} = $eml; # for mbox_hdr
49 $ctx->zmore(msg_hdr($ctx, $eml));
50 $ctx->write(msg_body($eml));
55 my $eml = $ctx->{eml} //= $ctx->{ibx}->smsg_eml($ctx->{smsg});
56 my $fn = $eml->header_str('Subject') // '';
58 $fn = to_filename($fn) // 'no-subject';
59 my @hdr = ('Content-Type');
60 if ($ctx->{ibx}->{obfuscate}) {
61 # obfuscation is stupid, but maybe scrapers are, too...
62 push @hdr, 'application/mbox';
65 push @hdr, 'text/plain';
68 my $cs = $ctx->{eml}->ct->{attributes}->{charset} // 'UTF-8';
69 $cs = 'UTF-8' if $cs =~ /[^a-zA-Z0-9\-\_]/; # avoid header injection
70 $hdr[-1] .= "; charset=$cs";
71 push @hdr, 'Content-Disposition', "inline; filename=$fn";
75 # for rare cases where v1 inboxes aren't indexed w/ ->over at all
78 my $mref = $ctx->{ibx}->msg_by_mid($ctx->{mid}) or return;
79 my $eml = $ctx->{eml} = PublicInbox::Eml->new($mref);
80 [ @{mbox_hdr($ctx)}, [ msg_hdr($ctx, $eml) . msg_body($eml) ] ]
83 # /$INBOX/$MESSAGE_ID/raw
86 $ctx->{base_url} = $ctx->{ibx}->base_url($ctx->{env});
87 my $over = $ctx->{ibx}->over or return no_over_raw($ctx);
89 my $mip = $ctx->{next_arg} = [ $ctx->{mid}, \$id, \$prev ];
90 my $smsg = $ctx->{smsg} = $over->next_by_mid(@$mip) or return;
91 bless $ctx, __PACKAGE__;
92 $ctx->psgi_response(\&mbox_hdr);
97 my $header_obj = $eml->header_obj;
99 # drop potentially confusing headers, ssoma already should've dropped
100 # Lines and Content-Length
101 foreach my $d (qw(Lines Bytes Content-Length Status)) {
102 $header_obj->header_set($d);
104 my $crlf = $header_obj->crlf;
105 my $buf = $header_obj->as_string;
106 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
107 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
108 "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf . $crlf;
112 my $bdy = $_[0]->{bdy} // return "\n";
113 # mboxrd quoting style
114 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
115 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
116 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
117 $$bdy =~ s/^(>*From )/>$1/gm;
123 my $msgs = $ctx->{msgs};
125 if (my $smsg = shift @$msgs) {
129 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
130 $ctx->{msgs} = $msgs = $over->get_thread($ctx->{mid},
132 return unless @$msgs;
133 $ctx->{prev} = $msgs->[-1];
138 my ($ctx, $over, $sfx) = @_;
139 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
140 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
141 $ctx->{prev} = $msgs->[-1];
142 require PublicInbox::MboxGz;
143 PublicInbox::MboxGz::mbox_gz($ctx, \&thread_cb, $msgs->[0]->{subject});
147 my ($ctx, $range) = @_;
150 if ($range eq 'all') { # TODO: YYYY[-MM]
153 return [404, [qw(Content-Type text/plain)], []];
155 mbox_all($ctx, { q => $q });
160 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
161 my $ids = $ctx->{ids};
163 while ((my $num = shift @$ids)) {
164 my $smsg = $over->get_art($num) or next;
167 $ctx->{ids} = $ids = $over->ids_after(\($ctx->{prev}));
174 my $over = $ctx->{ibx}->over or
175 return PublicInbox::WWW::need($ctx, 'Overview');
176 my $ids = $over->ids_after(\$prev) or return
177 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
179 $ctx->{prev} = $prev;
180 $ctx->{-low_prio} = 1;
181 require PublicInbox::MboxGz;
182 PublicInbox::MboxGz::mbox_gz($ctx, \&all_ids_cb, 'all');
187 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
189 while (defined(my $num = shift(@{$ctx->{ids}}))) {
190 my $smsg = $over->get_art($num) or next;
193 # refill result set, deprioritize since there's many results
194 my $srch = $ctx->{ibx}->isrch or return $ctx->gone('search');
195 my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
196 my $size = $mset->size or return;
197 $ctx->{qopts}->{offset} += $size;
198 $ctx->{ids} = $srch->mset_to_artnums($mset, $ctx->{qopts});
199 $ctx->{-low_prio} = 1;
203 sub results_thread_cb {
206 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
208 while (defined(my $num = shift(@{$ctx->{xids}}))) {
209 my $smsg = $over->get_art($num) or next;
213 # refills ctx->{xids}
214 next if $over->expand_thread($ctx);
216 # refill result set, deprioritize since there's many results
217 my $srch = $ctx->{ibx}->isrch or return $ctx->gone('search');
218 my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
219 my $size = $mset->size or return;
220 $ctx->{qopts}->{offset} += $size;
221 $ctx->{ids} = $srch->mset_to_artnums($mset, $ctx->{qopts});
222 $ctx->{-low_prio} = 1;
229 my $q_string = $q->{'q'};
230 return mbox_all_ids($ctx) if $q_string !~ /\S/;
231 my $srch = $ctx->{ibx}->isrch or
232 return PublicInbox::WWW::need($ctx, 'Search');
233 my $over = $ctx->{ibx}->over or
234 return PublicInbox::WWW::need($ctx, 'Overview');
236 my $qopts = $ctx->{qopts} = { relevance => -2 }; # ORDER BY docid DESC
237 $qopts->{threads} = 1 if $q->{t};
238 $srch->query_approxidate($ctx->{ibx}->git, $q_string);
239 my $mset = $srch->mset($q_string, $qopts);
240 $qopts->{offset} = $mset->size or
241 return [404, [qw(Content-Type text/plain)],
242 ["No results found\n"]];
243 $ctx->{query} = $q_string;
244 $ctx->{ids} = $srch->mset_to_artnums($mset, $qopts);
245 require PublicInbox::MboxGz;
247 if ($q->{t} && $srch->has_threadid) {
248 $fn = 'results-thread-'.$q_string;
249 PublicInbox::MboxGz::mbox_gz($ctx, \&results_thread_cb, $fn);
251 $fn = 'results-'.$q_string;
252 PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, $fn);