1 # Copyright (C) 2015-2019 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::SearchMsg;
17 use Email::MIME::Encode;
21 my $fn = $hdr->header('Subject');
22 return 'no-subject' if (!defined($fn) || $fn eq '');
24 # no need for full Email::MIME, here
26 eval { $fn = Encode::decode('MIME-Header', $fn) };
27 $fn = 'no-subject' if $@;
30 $fn = to_filename($fn);
31 $fn eq '' ? 'no-subject' : $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
47 return msg_hdr($ctx, $hdr);
49 if ($mref) { # all messages hit this
51 return msg_body($$mref);
53 my $cur = $next or return;
54 my $ibx = $ctx->{-inbox};
55 $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
56 $mref = $ibx->msg_by_smsg($cur) or return;
57 $hdr = Email::Simple->new($mref)->header_obj;
58 @$more = ($ctx, $id, $prev, $next, $mref); # $next may be undef, here
59 msg_hdr($ctx, $hdr); # all but first message hits this
64 # /$INBOX/$MESSAGE_ID/raw
67 my $mid = $ctx->{mid};
68 my $ibx = $ctx->{-inbox};
69 $ctx->{base_url} = $ibx->base_url($ctx->{env});
70 my ($mref, $more, $id, $prev, $next);
71 if (my $over = $ibx->over) {
72 my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
73 $mref = $ibx->msg_by_smsg($smsg) or return;
74 $next = $over->next_by_mid($mid, \$id, \$prev);
76 $mref = $ibx->msg_by_mid($mid) or return;
78 my $hdr = Email::Simple->new($mref)->header_obj;
79 $more = [ $ctx, $id, $prev, $next, $mref, $hdr ]; # for ->getline
80 my $fn = subject_fn($hdr);
81 my @hdr = ('Content-Type');
82 if ($ibx->{obfuscate}) {
83 # obfuscation is stupid, but maybe scrapers are, too...
84 push @hdr, 'application/mbox';
87 push @hdr, 'text/plain';
90 push @hdr, 'Content-Disposition', "inline; filename=$fn";
91 [ 200, \@hdr, mb_stream($more) ];
95 my ($ctx, $header_obj, $mid) = @_;
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 $ibx = $ctx->{-inbox};
103 my $base = $ctx->{base_url};
104 $mid = $ctx->{mid} unless defined $mid;
105 $mid = mid_escape($mid);
107 'Archived-At', "<$base$mid/>",
108 'List-Archive', "<$base>",
109 'List-Post', "<mailto:$ibx->{-primary_address}>",
111 my $crlf = $header_obj->crlf;
112 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
113 $header_obj->as_string;
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 eval { require PublicInbox::MboxGz };
156 return need_gzip() if $@;
157 my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
158 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
159 $ctx->{prev} = $msgs->[-1];
160 $ctx->{over} = $over; # bump refcnt
161 PublicInbox::MboxGz->response($ctx, \&thread_cb, $msgs->[0]->subject);
165 my ($ctx, $range) = @_;
168 if ($range eq 'all') { # TODO: YYYY[-MM]
171 return [404, [qw(Content-Type text/plain)], []];
173 mbox_all($ctx, $query);
178 my $ids = $ctx->{ids};
180 while ((my $num = shift @$ids)) {
181 my $smsg = $ctx->{over}->get_art($num) or next;
184 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
190 my $ibx = $ctx->{-inbox};
192 my $mm = $ctx->{mm} = $ibx->mm;
193 my $ids = $mm->ids_after(\$prev) or return
194 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
195 $ctx->{over} = $ibx->over or
196 return PublicInbox::WWW::need($ctx, 'Overview');
198 $ctx->{prev} = $prev;
199 return PublicInbox::MboxGz->response($ctx, \&all_ids_cb, 'all');
204 my $mset = $ctx->{mset};
205 my $srch = $ctx->{srch};
207 while (my $mi = (($mset->items)[$ctx->{iter}++])) {
208 my $smsg = PublicInbox::SearchMsg::from_mitem($mi,
213 $mset = $ctx->{mset} = $srch->query($ctx->{query},
215 my $size = $mset->size or return;
216 $ctx->{qopts}->{offset} += $size;
222 my ($ctx, $query) = @_;
224 eval { require PublicInbox::MboxGz };
225 return need_gzip() if $@;
226 return mbox_all_ids($ctx) if $query eq '';
227 my $qopts = $ctx->{qopts} = { mset => 2 };
228 my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
229 return PublicInbox::WWW::need($ctx, 'Search');;
230 my $mset = $ctx->{mset} = $srch->query($query, $qopts);
231 $qopts->{offset} = $mset->size or
232 return [404, [qw(Content-Type text/plain)],
233 ["No results found\n"]];
235 $ctx->{query} = $query;
236 PublicInbox::MboxGz->response($ctx, \&results_cb, 'results-'.$query);
240 my $title = 'gzipped mbox not available';
242 <html><head><title>$title</title><body><pre>$title
243 The administrator needs to install the Compress::Raw::Zlib Perl module
244 to support gzipped mboxes.
245 <a href="../">Return to index</a></pre></body></html>
248 [501,[qw(Content-Type text/html Content-Length), bytes::length($body)],