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 (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_escape/;
14 use PublicInbox::Hval qw/to_filename/;
15 use PublicInbox::Smsg;
17 use PublicInbox::GitAsyncCat;
18 use PublicInbox::GzipFilter qw(gzf_maybe);
20 # called by PSGI server as body response
21 # this gets called twice for every message, once to return the header,
22 # once to retrieve the body
25 my $smsg = $ctx->{smsg} or return;
26 my $ibx = $ctx->{-inbox};
27 my $eml = $ibx->smsg_eml($smsg) or return;
28 $ctx->{smsg} = $ibx->over->next_by_mid($ctx->{mid}, @{$ctx->{id_prev}});
29 msg_hdr($ctx, $eml, $smsg->{mid}) . msg_body($eml);
32 sub close { !!delete($_[0]->{http_out}) }
34 sub mbox_async_step ($) { # public-inbox-httpd-only
36 if (my $smsg = $ctx->{smsg}) {
37 git_async_cat($ctx->{-inbox}->git, $smsg->{blob},
38 \&mbox_blob_cb, $ctx);
39 } elsif (my $out = delete $ctx->{http_out}) {
44 # called by PublicInbox::DS::write
46 my ($http) = @_; # PublicInbox::HTTP
47 my $ctx = $http->{forward} or return; # client aborted
49 $ctx->{smsg} = $ctx->{-inbox}->over->next_by_mid(
50 $ctx->{mid}, @{$ctx->{id_prev}});
51 mbox_async_step($ctx);
55 # this is public-inbox-httpd-specific
56 sub mbox_blob_cb { # git->cat_async callback
57 my ($bref, $oid, $type, $size, $ctx) = @_;
58 my $http = $ctx->{env}->{'psgix.io'} or return; # client abort
59 my $smsg = delete $ctx->{smsg} or die 'BUG: no smsg';
61 # it's possible to have TOCTOU if an admin runs
62 # public-inbox-(edit|purge), just move onto the next message
63 return $http->next_step(\&mbox_async_next);
65 $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
67 my $eml = PublicInbox::Eml->new($bref);
68 $ctx->{http_out}->write(msg_hdr($ctx, $eml, $smsg->{mid}));
69 $ctx->{http_out}->write(msg_body($eml));
70 $http->next_step(\&mbox_async_next);
74 my ($ctx, $subject) = @_;
75 my $fn = $subject // 'no-subject';
77 $fn = $fn eq '' ? 'no-subject' : to_filename($fn);
78 my @hdr = ('Content-Type');
79 if ($ctx->{-inbox}->{obfuscate}) {
80 # obfuscation is stupid, but maybe scrapers are, too...
81 push @hdr, 'application/mbox';
84 push @hdr, 'text/plain';
87 push @hdr, 'Content-Disposition', "inline; filename=$fn";
91 # for rare cases where v1 inboxes aren't indexed w/ ->over at all
94 my $mref = $ctx->{-inbox}->msg_by_mid($ctx->{mid}) or return;
95 my $eml = PublicInbox::Eml->new($mref);
96 [ 200, res_hdr($ctx, $eml->header_str('Subject')),
97 [ msg_hdr($ctx, $eml, $ctx->{mid}) . msg_body($eml) ] ]
100 sub stream_raw { # MboxGz response callback
102 delete($ctx->{smsg}) //
103 $ctx->{-inbox}->over->next_by_mid($ctx->{mid},
107 # /$INBOX/$MESSAGE_ID/raw
110 my $env = $ctx->{env};
111 $ctx->{base_url} = $ctx->{-inbox}->base_url($env);
112 my $over = $ctx->{-inbox}->over or return no_over_raw($ctx);
114 my $smsg = $over->next_by_mid($ctx->{mid}, \$id, \$prev) or return;
115 $ctx->{smsg} = $smsg;
116 my $res_hdr = res_hdr($ctx, $smsg->{subject});
117 $ctx->{id_prev} = [ \$id, \$prev ];
119 if (my $gzf = gzf_maybe($res_hdr, $env)) {
120 $ctx->{gz} = delete $gzf->{gz};
121 require PublicInbox::MboxGz;
122 PublicInbox::MboxGz::response($ctx, \&stream_raw, $res_hdr);
123 } elsif ($env->{'pi-httpd.async'}) {
125 my ($wcb) = @_; # -httpd provided write callback
126 $ctx->{http_out} = $wcb->([200, $res_hdr]);
127 $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
128 bless $ctx, __PACKAGE__;
129 mbox_async_step($ctx); # start stepping
131 } else { # generic PSGI code path
132 bless $ctx, __PACKAGE__; # respond to ->getline
133 [ 200, $res_hdr, $ctx ];
138 my ($ctx, $eml, $mid) = @_;
139 my $header_obj = $eml->header_obj;
141 # drop potentially confusing headers, ssoma already should've dropped
142 # Lines and Content-Length
143 foreach my $d (qw(Lines Bytes Content-Length Status)) {
144 $header_obj->header_set($d);
146 my $ibx = $ctx->{-inbox};
147 my $base = $ctx->{base_url};
148 $mid = $ctx->{mid} unless defined $mid;
149 $mid = mid_escape($mid);
151 'Archived-At', "<$base$mid/>",
152 'List-Archive', "<$base>",
153 'List-Post', "<mailto:$ibx->{-primary_address}>",
155 my $crlf = $header_obj->crlf;
156 my $buf = $header_obj->as_string;
157 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
158 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
159 $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf;
161 for (my $i = 0; $i < @append; $i += 2) {
163 my $v = $append[$i + 1];
164 my @v = $header_obj->header_raw($k);
171 $buf .= "$k: $v$crlf" if defined $v;
177 my $bdy = $_[0]->{bdy} // return "\n";
178 # mboxrd quoting style
179 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
180 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
181 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
182 $$bdy =~ s/^(>*From )/>$1/gm;
188 my $msgs = $ctx->{msgs};
190 if (my $smsg = shift @$msgs) {
194 $ctx->{msgs} = $msgs = $ctx->{over}->get_thread($ctx->{mid},
196 return unless @$msgs;
197 $ctx->{prev} = $msgs->[-1];
202 my ($ctx, $over, $sfx) = @_;
203 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
204 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
205 $ctx->{prev} = $msgs->[-1];
206 $ctx->{over} = $over; # bump refcnt
207 require PublicInbox::MboxGz;
208 PublicInbox::MboxGz::mbox_gz($ctx, \&thread_cb, $msgs->[0]->{subject});
212 my ($ctx, $range) = @_;
215 if ($range eq 'all') { # TODO: YYYY[-MM]
218 return [404, [qw(Content-Type text/plain)], []];
220 mbox_all($ctx, $query);
225 my $ids = $ctx->{ids};
227 while ((my $num = shift @$ids)) {
228 my $smsg = $ctx->{over}->get_art($num) or next;
231 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
237 my $ibx = $ctx->{-inbox};
239 my $mm = $ctx->{mm} = $ibx->mm;
240 my $ids = $mm->ids_after(\$prev) or return
241 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
242 $ctx->{over} = $ibx->over or
243 return PublicInbox::WWW::need($ctx, 'Overview');
245 $ctx->{prev} = $prev;
246 require PublicInbox::MboxGz;
247 PublicInbox::MboxGz::mbox_gz($ctx, \&all_ids_cb, 'all');
252 my $mset = $ctx->{mset};
253 my $srch = $ctx->{srch};
255 while (my $mi = (($mset->items)[$ctx->{iter}++])) {
256 my $smsg = PublicInbox::Smsg::from_mitem($mi,
261 $mset = $ctx->{mset} = $srch->query($ctx->{query},
263 my $size = $mset->size or return;
264 $ctx->{qopts}->{offset} += $size;
270 my ($ctx, $query) = @_;
272 return mbox_all_ids($ctx) if $query eq '';
273 my $qopts = $ctx->{qopts} = { mset => 2 };
274 my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
275 return PublicInbox::WWW::need($ctx, 'Search');;
276 my $mset = $ctx->{mset} = $srch->query($query, $qopts);
277 $qopts->{offset} = $mset->size or
278 return [404, [qw(Content-Type text/plain)],
279 ["No results found\n"]];
281 $ctx->{query} = $query;
282 require PublicInbox::MboxGz;
283 PublicInbox::MboxGz::mbox_gz($ctx, \&results_cb, 'results-'.$query);