1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Streaming interface for formatting messages as an mboxrd.
5 # Used by the web interface
6 package PublicInbox::Mbox;
9 use PublicInbox::MID qw/mid_clean mid_escape/;
10 use PublicInbox::Hval qw/to_filename/;
12 use Email::MIME::Encode;
16 my $fn = $simple->header('Subject');
17 return 'no-subject' unless defined($fn);
19 # no need for full Email::MIME, here
21 eval { $fn = Encode::decode('MIME-Header', $fn) };
22 $fn = 'no-subject' if $@;
25 $fn = to_filename($fn);
26 $fn eq '' ? 'no-subject' : $fn;
31 bless $more, 'PublicInbox::Mbox';
34 # called by PSGI server as body response
36 my ($more) = @_; # self
37 my ($ctx, $id, $prev, $next, $cur) = @$more;
40 return msg_str($ctx, $cur);
42 $cur = $next or return;
43 my $ibx = $ctx->{-inbox};
44 $next = $ibx->search->next_by_mid($ctx->{mid}, \$id, \$prev);
45 @$more = ($ctx, $id, $prev, $next); # $next may be undef, here
46 my $mref = $ibx->msg_by_smsg($cur) or return;
47 msg_str($ctx, Email::Simple->new($mref));
54 my $mid = $ctx->{mid};
55 my $ibx = $ctx->{-inbox};
58 if (my $srch = $ibx->search) {
60 my $smsg = $srch->next_by_mid($mid, \$id, \$prev) or return;
61 my $mref = $ibx->msg_by_smsg($smsg) or return;
62 $first = Email::Simple->new($mref);
63 my $next = $srch->next_by_mid($mid, \$id, \$prev);
64 # $more is for ->getline
65 $more = [ $ctx, $id, $prev, $next, $first ] if $next;
67 my $mref = $ibx->msg_by_mid($mid) or return;
68 $first = Email::Simple->new($mref);
70 return unless defined $first;
71 my $fn = subject_fn($first);
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, $more ? mb_stream($more) : [ msg_str($ctx, $first) ] ];
86 my ($ctx, $simple, $mid) = @_; # Email::Simple object
87 my $header_obj = $simple->header_obj;
89 # drop potentially confusing headers, ssoma already should've dropped
90 # Lines and Content-Length
91 foreach my $d (qw(Lines Bytes Content-Length Status)) {
92 $header_obj->header_set($d);
94 my $ibx = $ctx->{-inbox};
95 my $base = $ibx->base_url($ctx->{env});
96 $mid = $ctx->{mid} unless defined $mid;
97 $mid = mid_escape($mid);
99 'Archived-At', "<$base$mid/>",
100 'List-Archive', "<$base>",
101 'List-Post', "<mailto:$ibx->{-primary_address}>",
103 my $crlf = $simple->crlf;
104 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
105 $header_obj->as_string;
106 for (my $i = 0; $i < @append; $i += 2) {
108 my $v = $append[$i + 1];
109 my @v = $header_obj->header($k);
116 $buf .= "$k: $v$crlf" if defined $v;
120 # mboxrd quoting style
121 # ref: http://www.qmail.org/man/man5/mbox.html
122 my $body = $simple->body;
123 $body =~ s/^(>*From )/>$1/gm;
129 my ($ctx, $srch, $sfx) = @_;
130 eval { require IO::Compress::Gzip };
131 return sub { need_gzip(@_) } if $@;
132 my $mid = $ctx->{mid};
133 my $msgs = $srch->get_thread($mid, {});
134 return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
135 my $prev = $msgs->[-1];
139 if (my $smsg = $msgs->[$i++]) {
143 $msgs = $srch->get_thread($mid, $prev);
144 return unless @$msgs;
149 PublicInbox::MboxGz->response($ctx, $cb, $msgs->[0]->subject);
153 my ($ctx, $range) = @_;
156 if ($range eq 'all') { # TODO: YYYY[-MM]
159 return [404, [qw(Content-Type text/plain)], []];
161 mbox_all($ctx, $query);
167 my $ids = $ctx->{-inbox}->mm->ids_after(\$prev) or return
168 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
170 my $over = $ctx->{srch}->{over_ro};
173 while ((my $num = $ids->[$i++])) {
174 my $smsg = $over->get_art($num) or next;
177 $ids = $ctx->{-inbox}->mm->ids_after(\$prev);
182 return PublicInbox::MboxGz->response($ctx, $cb, 'all');
186 my ($ctx, $query) = @_;
188 eval { require IO::Compress::Gzip };
189 return sub { need_gzip(@_) } if $@;
190 return mbox_all_ids($ctx) if $query eq '';
191 my $opts = { mset => 2 };
192 my $srch = $ctx->{srch};
193 my $mset = $srch->query($query, $opts);
194 $opts->{offset} = $mset->size or
195 return [404, [qw(Content-Type text/plain)],
196 ["No results found\n"]];
198 my $cb = sub { # called by MboxGz->getline
200 while (my $mi = (($mset->items)[$i++])) {
201 my $doc = $mi->get_document;
202 my $smsg = $srch->retry_reopen(sub {
203 PublicInbox::SearchMsg->load_doc($doc);
208 $mset = $srch->query($query, $opts);
209 my $size = $mset->size or return;
210 $opts->{offset} += $size;
214 PublicInbox::MboxGz->response($ctx, $cb, 'results-'.$query);
218 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
219 my $title = 'gzipped mbox not available';
221 <html><head><title>$title</title><body><pre>$title
222 The administrator needs to install the IO::Compress::Gzip Perl module
223 to support gzipped mboxes.
224 <a href="../">Return to index</a></pre></body></html>
231 package PublicInbox::MboxGz;
234 use PublicInbox::Hval qw/to_filename/;
237 my ($class, $ctx, $cb) = @_;
241 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
248 my ($class, $ctx, $cb, $fn) = @_;
249 my $body = $class->new($ctx, $cb);
250 # http://www.iana.org/assignments/media-types/application/gzip
251 my @h = qw(Content-Type application/gzip);
253 $fn = to_filename($fn);
254 push @h, 'Content-Disposition', "inline; filename=$fn.mbox.gz";
259 # called by Plack::Util::foreach or similar
262 my $ctx = $self->{ctx} or return;
263 while (my $smsg = $self->{cb}->()) {
264 my $msg = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
265 $msg = Email::Simple->new($msg);
266 $self->{gz}->write(PublicInbox::Mbox::msg_str($ctx, $msg,
268 my $bref = $self->{buf};
269 if (length($$bref) >= 8192) {
270 my $ret = $$bref; # copy :<
271 ${$self->{buf}} = '';
275 # be fair to other clients on public-inbox-httpd:
278 delete($self->{gz})->close;
279 # signal that we're done and can return undef next call:
281 ${delete $self->{buf}};