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->zflush(msg_body($eml));
31 # called by PublicInbox::DS::write after http->next_step
33 my ($http) = @_; # PublicInbox::HTTP
34 my $ctx = $http->{forward} or return; # client aborted
36 my $smsg = $ctx->{smsg} or return $ctx->close;
37 $ctx->smsg_blob($smsg);
42 sub async_eml { # for async_blob_cb
44 my $smsg = delete $ctx->{smsg};
46 $ctx->{smsg} = $ctx->{ibx}->over->next_by_mid(@{$ctx->{next_arg}});
47 local $ctx->{eml} = $eml; # for mbox_hdr
48 $ctx->zmore(msg_hdr($ctx, $eml));
49 $ctx->write(msg_body($eml));
54 my $eml = $ctx->{eml} //= $ctx->{ibx}->smsg_eml($ctx->{smsg});
55 my $fn = $eml->header_str('Subject') // '';
57 $fn = to_filename($fn) // 'no-subject';
58 my @hdr = ('Content-Type');
59 if ($ctx->{ibx}->{obfuscate}) {
60 # obfuscation is stupid, but maybe scrapers are, too...
61 push @hdr, 'application/mbox';
64 push @hdr, 'text/plain';
67 my $cs = $ctx->{eml}->ct->{attributes}->{charset} // 'UTF-8';
68 $cs = 'UTF-8' if $cs =~ /[^a-zA-Z0-9\-\_]/; # avoid header injection
69 $hdr[-1] .= "; charset=$cs";
70 push @hdr, 'Content-Disposition', "inline; filename=$fn";
74 # for rare cases where v1 inboxes aren't indexed w/ ->over at all
77 my $mref = $ctx->{ibx}->msg_by_mid($ctx->{mid}) or return;
78 my $eml = $ctx->{eml} = PublicInbox::Eml->new($mref);
79 [ @{mbox_hdr($ctx)}, [ msg_hdr($ctx, $eml) . msg_body($eml) ] ]
82 # /$INBOX/$MESSAGE_ID/raw
85 $ctx->{base_url} = $ctx->{ibx}->base_url($ctx->{env});
86 my $over = $ctx->{ibx}->over or return no_over_raw($ctx);
88 my $mip = $ctx->{next_arg} = [ $ctx->{mid}, \$id, \$prev ];
89 my $smsg = $ctx->{smsg} = $over->next_by_mid(@$mip) or return;
90 bless $ctx, __PACKAGE__;
91 $ctx->psgi_response(\&mbox_hdr);
96 my $header_obj = $eml->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 $crlf = $header_obj->crlf;
104 my $buf = $header_obj->as_string;
105 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
106 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
107 "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf . $crlf;
111 my $bdy = $_[0]->{bdy} // return "\n";
112 # mboxrd quoting style
113 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
114 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
115 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
116 $$bdy =~ s/^(>*From )/>$1/gm;
122 my $msgs = $ctx->{msgs};
124 if (my $smsg = shift @$msgs) {
128 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
129 $ctx->{msgs} = $msgs = $over->get_thread($ctx->{mid},
131 return unless @$msgs;
132 $ctx->{prev} = $msgs->[-1];
137 my ($ctx, $over, $sfx) = @_;
138 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
139 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
140 $ctx->{prev} = $msgs->[-1];
141 require PublicInbox::MboxGz;
142 PublicInbox::MboxGz::mbox_gz($ctx, \&thread_cb, $msgs->[0]->{subject});
146 my ($ctx, $range) = @_;
149 if ($range eq 'all') { # TODO: YYYY[-MM]
152 return [404, [qw(Content-Type text/plain)], []];
154 mbox_all($ctx, { q => $q });
159 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
160 my $ids = $ctx->{ids};
162 while ((my $num = shift @$ids)) {
163 my $smsg = $over->get_art($num) or next;
166 $ctx->{ids} = $ids = $over->ids_after(\($ctx->{prev}));
173 my $over = $ctx->{ibx}->over or
174 return PublicInbox::WWW::need($ctx, 'Overview');
175 my $ids = $over->ids_after(\$prev) or return
176 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
178 $ctx->{prev} = $prev;
179 $ctx->{-low_prio} = 1;
180 require PublicInbox::MboxGz;
181 PublicInbox::MboxGz::mbox_gz($ctx, \&all_ids_cb, 'all');
186 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
188 while (defined(my $num = shift(@{$ctx->{ids}}))) {
189 my $smsg = $over->get_art($num) or next;
192 # refill result set, deprioritize since there's many results
193 my $srch = $ctx->{ibx}->isrch or return $ctx->gone('search');
194 my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
195 my $size = $mset->size or return;
196 $ctx->{qopts}->{offset} += $size;
197 $ctx->{ids} = $srch->mset_to_artnums($mset, $ctx->{qopts});
198 $ctx->{-low_prio} = 1;
202 sub results_thread_cb {
205 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
207 while (defined(my $num = shift(@{$ctx->{xids}}))) {
208 my $smsg = $over->get_art($num) or next;
212 # refills ctx->{xids}
213 next if $over->expand_thread($ctx);
215 # refill result set, deprioritize since there's many results
216 my $srch = $ctx->{ibx}->isrch or return $ctx->gone('search');
217 my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
218 my $size = $mset->size or return;
219 $ctx->{qopts}->{offset} += $size;
220 $ctx->{ids} = $srch->mset_to_artnums($mset, $ctx->{qopts});
221 $ctx->{-low_prio} = 1;
228 my $q_string = $q->{'q'};
229 return mbox_all_ids($ctx) if $q_string !~ /\S/;
230 my $srch = $ctx->{ibx}->isrch or
231 return PublicInbox::WWW::need($ctx, 'Search');
232 my $over = $ctx->{ibx}->over or
233 return PublicInbox::WWW::need($ctx, 'Overview');
235 my $qopts = $ctx->{qopts} = { relevance => -2 }; # ORDER BY docid DESC
236 $qopts->{threads} = 1 if $q->{t};
237 $srch->query_approxidate($ctx->{ibx}->git, $q_string);
238 my $mset = $srch->mset($q_string, $qopts);
239 $qopts->{offset} = $mset->size or
240 return [404, [qw(Content-Type text/plain)],
241 ["No results found\n"]];
242 $ctx->{query} = $q_string;
243 $ctx->{ids} = $srch->mset_to_artnums($mset, $qopts);
244 require PublicInbox::MboxGz;
246 if ($q->{t} && $srch->has_threadid) {
247 $fn = 'results-thread-'.$q_string;
248 PublicInbox::MboxGz::mbox_gz($ctx, \&results_thread_cb, $fn);
250 $fn = 'results-'.$q_string;
251 PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, $fn);