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;
16 use PublicInbox::WwwStream qw(html_oneshot);
21 my $fn = $hdr->header_str('Subject');
22 return 'no-subject' if (!defined($fn) || $fn eq '');
25 $fn eq '' ? 'no-subject' : to_filename($fn);
30 bless $more, 'PublicInbox::Mbox';
33 # called by PSGI server as body response
34 # this gets called twice for every message, once to return the header,
35 # once to retrieve the body
37 my ($more) = @_; # self
38 my ($ctx, $id, $prev, $next, $mref, $hdr) = @$more;
39 if ($hdr) { # first message hits this, only
42 return msg_hdr($ctx, $hdr) . msg_body($$mref);
44 my $cur = $next or return;
45 my $ibx = $ctx->{-inbox};
46 $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
47 $mref = $ibx->msg_by_smsg($cur) or return;
48 $hdr = PublicInbox::Eml->new($mref)->header_obj;
49 @$more = ($ctx, $id, $prev, $next); # $next may be undef, here
50 msg_hdr($ctx, $hdr) . msg_body($$mref);
55 # /$INBOX/$MESSAGE_ID/raw
58 my $mid = $ctx->{mid};
59 my $ibx = $ctx->{-inbox};
60 $ctx->{base_url} = $ibx->base_url($ctx->{env});
61 my ($mref, $more, $id, $prev, $next);
62 if (my $over = $ibx->over) {
63 my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
64 $mref = $ibx->msg_by_smsg($smsg) or return;
65 $next = $over->next_by_mid($mid, \$id, \$prev);
67 $mref = $ibx->msg_by_mid($mid) or return;
69 my $hdr = PublicInbox::Eml->new($mref)->header_obj;
70 $more = [ $ctx, $id, $prev, $next, $mref, $hdr ]; # for ->getline
71 my $fn = subject_fn($hdr);
72 my @hdr = ('Content-Type');
73 if ($ibx->{obfuscate}) {
74 # obfuscation is stupid, but maybe scrapers are, too...
75 push @hdr, 'application/mbox';
78 push @hdr, 'text/plain';
81 push @hdr, 'Content-Disposition', "inline; filename=$fn";
82 [ 200, \@hdr, mb_stream($more) ];
86 my ($ctx, $header_obj, $mid) = @_;
88 # drop potentially confusing headers, ssoma already should've dropped
89 # Lines and Content-Length
90 foreach my $d (qw(Lines Bytes Content-Length Status)) {
91 $header_obj->header_set($d);
93 my $ibx = $ctx->{-inbox};
94 my $base = $ctx->{base_url};
95 $mid = $ctx->{mid} unless defined $mid;
96 $mid = mid_escape($mid);
98 'Archived-At', "<$base$mid/>",
99 'List-Archive', "<$base>",
100 'List-Post', "<mailto:$ibx->{-primary_address}>",
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 $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf;
108 for (my $i = 0; $i < @append; $i += 2) {
110 my $v = $append[$i + 1];
111 my @v = $header_obj->header_raw($k);
118 $buf .= "$k: $v$crlf" if defined $v;
124 # mboxrd quoting style
125 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
126 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
127 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
128 $_[0] =~ s/^(>*From )/>$1/gm;
134 my $msgs = $ctx->{msgs};
136 if (my $smsg = shift @$msgs) {
140 $ctx->{msgs} = $msgs = $ctx->{over}->get_thread($ctx->{mid},
142 return unless @$msgs;
143 $ctx->{prev} = $msgs->[-1];
148 my ($ctx, $over, $sfx) = @_;
149 require PublicInbox::MboxGz;
150 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
151 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
152 $ctx->{prev} = $msgs->[-1];
153 $ctx->{over} = $over; # bump refcnt
154 PublicInbox::MboxGz->response($ctx, \&thread_cb, $msgs->[0]->{subject});
158 my ($ctx, $range) = @_;
161 if ($range eq 'all') { # TODO: YYYY[-MM]
164 return [404, [qw(Content-Type text/plain)], []];
166 mbox_all($ctx, $query);
171 my $ids = $ctx->{ids};
173 while ((my $num = shift @$ids)) {
174 my $smsg = $ctx->{over}->get_art($num) or next;
177 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
183 my $ibx = $ctx->{-inbox};
185 my $mm = $ctx->{mm} = $ibx->mm;
186 my $ids = $mm->ids_after(\$prev) or return
187 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
188 $ctx->{over} = $ibx->over or
189 return PublicInbox::WWW::need($ctx, 'Overview');
191 $ctx->{prev} = $prev;
192 return PublicInbox::MboxGz->response($ctx, \&all_ids_cb, 'all');
197 my $mset = $ctx->{mset};
198 my $srch = $ctx->{srch};
200 while (my $mi = (($mset->items)[$ctx->{iter}++])) {
201 my $smsg = PublicInbox::Smsg::from_mitem($mi,
206 $mset = $ctx->{mset} = $srch->query($ctx->{query},
208 my $size = $mset->size or return;
209 $ctx->{qopts}->{offset} += $size;
215 my ($ctx, $query) = @_;
217 require PublicInbox::MboxGz;
218 return mbox_all_ids($ctx) if $query eq '';
219 my $qopts = $ctx->{qopts} = { mset => 2 };
220 my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
221 return PublicInbox::WWW::need($ctx, 'Search');;
222 my $mset = $ctx->{mset} = $srch->query($query, $qopts);
223 $qopts->{offset} = $mset->size or
224 return [404, [qw(Content-Type text/plain)],
225 ["No results found\n"]];
227 $ctx->{query} = $query;
228 PublicInbox::MboxGz->response($ctx, \&results_cb, 'results-'.$query);