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);
18 use Email::MIME::Encode;
22 my $fn = $hdr->header('Subject');
23 return 'no-subject' if (!defined($fn) || $fn eq '');
25 # no need for full Email::MIME, here
27 eval { $fn = Encode::decode('MIME-Header', $fn) };
28 return 'no-subject' if $@;
31 $fn eq '' ? 'no-subject' : to_filename($fn);
36 bless $more, 'PublicInbox::Mbox';
39 # called by PSGI server as body response
40 # this gets called twice for every message, once to return the header,
41 # once to retrieve the body
43 my ($more) = @_; # self
44 my ($ctx, $id, $prev, $next, $mref, $hdr) = @$more;
45 if ($hdr) { # first message hits this, only
48 return msg_hdr($ctx, $hdr) . msg_body($$mref);
50 my $cur = $next or return;
51 my $ibx = $ctx->{-inbox};
52 $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
53 $mref = $ibx->msg_by_smsg($cur) or return;
54 $hdr = Email::Simple->new($mref)->header_obj;
55 @$more = ($ctx, $id, $prev, $next); # $next may be undef, here
56 msg_hdr($ctx, $hdr) . msg_body($$mref);
61 # /$INBOX/$MESSAGE_ID/raw
64 my $mid = $ctx->{mid};
65 my $ibx = $ctx->{-inbox};
66 $ctx->{base_url} = $ibx->base_url($ctx->{env});
67 my ($mref, $more, $id, $prev, $next);
68 if (my $over = $ibx->over) {
69 my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
70 $mref = $ibx->msg_by_smsg($smsg) or return;
71 $next = $over->next_by_mid($mid, \$id, \$prev);
73 $mref = $ibx->msg_by_mid($mid) or return;
75 my $hdr = Email::Simple->new($mref)->header_obj;
76 $more = [ $ctx, $id, $prev, $next, $mref, $hdr ]; # for ->getline
77 my $fn = subject_fn($hdr);
78 my @hdr = ('Content-Type');
79 if ($ibx->{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";
88 [ 200, \@hdr, mb_stream($more) ];
92 my ($ctx, $header_obj, $mid) = @_;
94 # drop potentially confusing headers, ssoma already should've dropped
95 # Lines and Content-Length
96 foreach my $d (qw(Lines Bytes Content-Length Status)) {
97 $header_obj->header_set($d);
99 my $ibx = $ctx->{-inbox};
100 my $base = $ctx->{base_url};
101 $mid = $ctx->{mid} unless defined $mid;
102 $mid = mid_escape($mid);
104 'Archived-At', "<$base$mid/>",
105 'List-Archive', "<$base>",
106 'List-Post', "<mailto:$ibx->{-primary_address}>",
108 my $crlf = $header_obj->crlf;
109 my $buf = $header_obj->as_string;
110 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
111 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
112 $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf;
114 for (my $i = 0; $i < @append; $i += 2) {
116 my $v = $append[$i + 1];
117 my @v = $header_obj->header($k);
124 $buf .= "$k: $v$crlf" if defined $v;
130 # mboxrd quoting style
131 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
132 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
133 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
134 $_[0] =~ s/^(>*From )/>$1/gm;
140 my $msgs = $ctx->{msgs};
142 if (my $smsg = shift @$msgs) {
146 $ctx->{msgs} = $msgs = $ctx->{over}->get_thread($ctx->{mid},
148 return unless @$msgs;
149 $ctx->{prev} = $msgs->[-1];
154 my ($ctx, $over, $sfx) = @_;
155 require PublicInbox::MboxGz;
156 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
157 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
158 $ctx->{prev} = $msgs->[-1];
159 $ctx->{over} = $over; # bump refcnt
160 PublicInbox::MboxGz->response($ctx, \&thread_cb, $msgs->[0]->subject);
164 my ($ctx, $range) = @_;
167 if ($range eq 'all') { # TODO: YYYY[-MM]
170 return [404, [qw(Content-Type text/plain)], []];
172 mbox_all($ctx, $query);
177 my $ids = $ctx->{ids};
179 while ((my $num = shift @$ids)) {
180 my $smsg = $ctx->{over}->get_art($num) or next;
183 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
189 my $ibx = $ctx->{-inbox};
191 my $mm = $ctx->{mm} = $ibx->mm;
192 my $ids = $mm->ids_after(\$prev) or return
193 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
194 $ctx->{over} = $ibx->over or
195 return PublicInbox::WWW::need($ctx, 'Overview');
197 $ctx->{prev} = $prev;
198 return PublicInbox::MboxGz->response($ctx, \&all_ids_cb, 'all');
203 my $mset = $ctx->{mset};
204 my $srch = $ctx->{srch};
206 while (my $mi = (($mset->items)[$ctx->{iter}++])) {
207 my $smsg = PublicInbox::Smsg::from_mitem($mi,
212 $mset = $ctx->{mset} = $srch->query($ctx->{query},
214 my $size = $mset->size or return;
215 $ctx->{qopts}->{offset} += $size;
221 my ($ctx, $query) = @_;
223 require PublicInbox::MboxGz;
224 return mbox_all_ids($ctx) if $query eq '';
225 my $qopts = $ctx->{qopts} = { mset => 2 };
226 my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
227 return PublicInbox::WWW::need($ctx, 'Search');;
228 my $mset = $ctx->{mset} = $srch->query($query, $qopts);
229 $qopts->{offset} = $mset->size or
230 return [404, [qw(Content-Type text/plain)],
231 ["No results found\n"]];
233 $ctx->{query} = $query;
234 PublicInbox::MboxGz->response($ctx, \&results_cb, 'results-'.$query);