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;
20 my $fn = $hdr->header_str('Subject');
21 return 'no-subject' if (!defined($fn) || $fn eq '');
24 $fn eq '' ? 'no-subject' : to_filename($fn);
29 bless $more, 'PublicInbox::Mbox';
32 # called by PSGI server as body response
33 # this gets called twice for every message, once to return the header,
34 # once to retrieve the body
36 my ($more) = @_; # self
37 my ($ctx, $id, $prev, $next, $mref, $hdr) = @$more;
38 if ($hdr) { # first message hits this, only
41 return msg_hdr($ctx, $hdr) . msg_body($$mref);
43 my $cur = $next or return;
44 my $ibx = $ctx->{-inbox};
45 $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
46 $mref = $ibx->msg_by_smsg($cur) or return;
47 $hdr = PublicInbox::Eml->new($mref)->header_obj;
48 @$more = ($ctx, $id, $prev, $next); # $next may be undef, here
49 msg_hdr($ctx, $hdr) . msg_body($$mref);
54 # /$INBOX/$MESSAGE_ID/raw
57 my $mid = $ctx->{mid};
58 my $ibx = $ctx->{-inbox};
59 $ctx->{base_url} = $ibx->base_url($ctx->{env});
60 my ($mref, $more, $id, $prev, $next);
61 if (my $over = $ibx->over) {
62 my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
63 $mref = $ibx->msg_by_smsg($smsg) or return;
64 $next = $over->next_by_mid($mid, \$id, \$prev);
66 $mref = $ibx->msg_by_mid($mid) or return;
68 my $hdr = PublicInbox::Eml->new($mref)->header_obj;
69 $more = [ $ctx, $id, $prev, $next, $mref, $hdr ]; # for ->getline
70 my $fn = subject_fn($hdr);
71 my @hdr = ('Content-Type');
72 if ($ibx->{obfuscate}) {
73 # obfuscation is stupid, but maybe scrapers are, too...
74 push @hdr, 'application/mbox';
77 push @hdr, 'text/plain';
80 push @hdr, 'Content-Disposition', "inline; filename=$fn";
81 [ 200, \@hdr, mb_stream($more) ];
85 my ($ctx, $header_obj, $mid) = @_;
87 # drop potentially confusing headers, ssoma already should've dropped
88 # Lines and Content-Length
89 foreach my $d (qw(Lines Bytes Content-Length Status)) {
90 $header_obj->header_set($d);
92 my $ibx = $ctx->{-inbox};
93 my $base = $ctx->{base_url};
94 $mid = $ctx->{mid} unless defined $mid;
95 $mid = mid_escape($mid);
97 'Archived-At', "<$base$mid/>",
98 'List-Archive', "<$base>",
99 'List-Post', "<mailto:$ibx->{-primary_address}>",
101 my $crlf = $header_obj->crlf;
102 my $buf = $header_obj->as_string;
103 # fixup old bug from import (pre-a0c07cba0e5d8b6a)
104 $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
105 $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970" . $crlf . $buf;
107 for (my $i = 0; $i < @append; $i += 2) {
109 my $v = $append[$i + 1];
110 my @v = $header_obj->header_raw($k);
117 $buf .= "$k: $v$crlf" if defined $v;
123 # mboxrd quoting style
124 # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
125 # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
126 # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
127 $_[0] =~ s/^(>*From )/>$1/gm;
133 my $msgs = $ctx->{msgs};
135 if (my $smsg = shift @$msgs) {
139 $ctx->{msgs} = $msgs = $ctx->{over}->get_thread($ctx->{mid},
141 return unless @$msgs;
142 $ctx->{prev} = $msgs->[-1];
147 my ($ctx, $over, $sfx) = @_;
148 require PublicInbox::MboxGz;
149 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
150 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
151 $ctx->{prev} = $msgs->[-1];
152 $ctx->{over} = $over; # bump refcnt
153 PublicInbox::MboxGz->response($ctx, \&thread_cb, $msgs->[0]->{subject});
157 my ($ctx, $range) = @_;
160 if ($range eq 'all') { # TODO: YYYY[-MM]
163 return [404, [qw(Content-Type text/plain)], []];
165 mbox_all($ctx, $query);
170 my $ids = $ctx->{ids};
172 while ((my $num = shift @$ids)) {
173 my $smsg = $ctx->{over}->get_art($num) or next;
176 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
182 my $ibx = $ctx->{-inbox};
184 my $mm = $ctx->{mm} = $ibx->mm;
185 my $ids = $mm->ids_after(\$prev) or return
186 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
187 $ctx->{over} = $ibx->over or
188 return PublicInbox::WWW::need($ctx, 'Overview');
190 $ctx->{prev} = $prev;
191 return PublicInbox::MboxGz->response($ctx, \&all_ids_cb, 'all');
196 my $mset = $ctx->{mset};
197 my $srch = $ctx->{srch};
199 while (my $mi = (($mset->items)[$ctx->{iter}++])) {
200 my $smsg = PublicInbox::Smsg::from_mitem($mi,
205 $mset = $ctx->{mset} = $srch->query($ctx->{query},
207 my $size = $mset->size or return;
208 $ctx->{qopts}->{offset} += $size;
214 my ($ctx, $query) = @_;
216 require PublicInbox::MboxGz;
217 return mbox_all_ids($ctx) if $query eq '';
218 my $qopts = $ctx->{qopts} = { mset => 2 };
219 my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
220 return PublicInbox::WWW::need($ctx, 'Search');;
221 my $mset = $ctx->{mset} = $srch->query($query, $qopts);
222 $qopts->{offset} = $mset->size or
223 return [404, [qw(Content-Type text/plain)],
224 ["No results found\n"]];
226 $ctx->{query} = $query;
227 PublicInbox::MboxGz->response($ctx, \&results_cb, 'results-'.$query);