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 = $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));
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}});
49 $ctx->zmore(msg_hdr($ctx, $eml));
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->{ibx}->{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->{ibx}->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) . msg_body($eml) ] ]
80 # /$INBOX/$MESSAGE_ID/raw
83 $ctx->{base_url} = $ctx->{ibx}->base_url($ctx->{env});
84 my $over = $ctx->{ibx}->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);
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 $crlf = $header_obj->crlf;
103 my $buf = $header_obj->as_string;
104 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
105 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
106 "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf . $crlf;
110 my $bdy = $_[0]->{bdy} // return "\n";
111 # mboxrd quoting style
112 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
113 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
114 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
115 $$bdy =~ s/^(>*From )/>$1/gm;
121 my $msgs = $ctx->{msgs};
123 if (my $smsg = shift @$msgs) {
127 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
128 $ctx->{msgs} = $msgs = $over->get_thread($ctx->{mid},
130 return unless @$msgs;
131 $ctx->{prev} = $msgs->[-1];
136 my ($ctx, $over, $sfx) = @_;
137 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
138 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
139 $ctx->{prev} = $msgs->[-1];
140 require PublicInbox::MboxGz;
141 PublicInbox::MboxGz::mbox_gz($ctx, \&thread_cb, $msgs->[0]->{subject});
145 my ($ctx, $range) = @_;
148 if ($range eq 'all') { # TODO: YYYY[-MM]
151 return [404, [qw(Content-Type text/plain)], []];
153 mbox_all($ctx, { q => $q });
158 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
159 my $ids = $ctx->{ids};
161 while ((my $num = shift @$ids)) {
162 my $smsg = $over->get_art($num) or next;
165 $ctx->{ids} = $ids = $over->ids_after(\($ctx->{prev}));
172 my $over = $ctx->{ibx}->over or
173 return PublicInbox::WWW::need($ctx, 'Overview');
174 my $ids = $over->ids_after(\$prev) or return
175 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
177 $ctx->{prev} = $prev;
178 $ctx->{-low_prio} = 1;
179 require PublicInbox::MboxGz;
180 PublicInbox::MboxGz::mbox_gz($ctx, \&all_ids_cb, 'all');
185 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
187 while (defined(my $num = shift(@{$ctx->{ids}}))) {
188 my $smsg = $over->get_art($num) or next;
191 # refill result set, deprioritize since there's many results
192 my $srch = $ctx->{ibx}->isrch or return $ctx->gone('search');
193 my $mset = $srch->mset($ctx->{query}, $ctx->{qopts});
194 my $size = $mset->size or return;
195 $ctx->{qopts}->{offset} += $size;
196 $ctx->{ids} = $srch->mset_to_artnums($mset, $ctx->{qopts});
197 $ctx->{-low_prio} = 1;
201 sub results_thread_cb {
204 my $over = $ctx->{ibx}->over or return $ctx->gone('over');
206 while (defined(my $num = shift(@{$ctx->{xids}}))) {
207 my $smsg = $over->get_art($num) or next;
211 # refills ctx->{xids}
212 next if $over->expand_thread($ctx);
214 # refill result set, deprioritize since there's many results
215 my $srch = $ctx->{ibx}->isrch or return $ctx->gone('search');
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, $ctx->{qopts});
220 $ctx->{-low_prio} = 1;
227 my $q_string = $q->{'q'};
228 return mbox_all_ids($ctx) if $q_string !~ /\S/;
229 my $srch = $ctx->{ibx}->isrch or
230 return PublicInbox::WWW::need($ctx, 'Search');
231 my $over = $ctx->{ibx}->over or
232 return PublicInbox::WWW::need($ctx, 'Overview');
234 my $qopts = $ctx->{qopts} = { relevance => -2 }; # ORDER BY docid DESC
235 $qopts->{threads} = 1 if $q->{t};
236 $srch->query_approxidate($ctx->{ibx}->git, $q_string);
237 my $mset = $srch->mset($q_string, $qopts);
238 $qopts->{offset} = $mset->size or
239 return [404, [qw(Content-Type text/plain)],
240 ["No results found\n"]];
241 $ctx->{query} = $q_string;
242 $ctx->{ids} = $srch->mset_to_artnums($mset, $qopts);
243 require PublicInbox::MboxGz;
245 if ($q->{t} && $srch->has_threadid) {
246 $fn = 'results-thread-'.$q_string;
247 PublicInbox::MboxGz::mbox_gz($ctx, \&results_thread_cb, $fn);
249 $fn = 'results-'.$q_string;
250 PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, $fn);