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 $msg = Email::Simple->new($msg);
32 my $fn = subject_fn($msg);
33 my @hdr = ('Content-Type');
34 if ($ctx->{-inbox}->{obfuscate}) {
35 # obfuscation is stupid, but maybe scrapers are, too...
36 push @hdr, 'application/mbox';
39 push @hdr, 'text/plain';
42 push @hdr, 'Content-Disposition', "inline; filename=$fn";
44 # single message should be easily renderable in browsers,
45 # unless obfuscation is enabled :<
46 [ 200, \@hdr, [ msg_str($ctx, $msg) ] ]
50 my ($ctx, $simple) = @_; # Email::Simple object
51 my $header_obj = $simple->header_obj;
53 # drop potentially confusing headers, ssoma already should've dropped
54 # Lines and Content-Length
55 foreach my $d (qw(Lines Bytes Content-Length Status)) {
56 $header_obj->header_set($d);
58 my $ibx = $ctx->{-inbox};
59 my $base = $ibx->base_url($ctx->{env});
60 my $mid = mid_clean($header_obj->header('Message-ID'));
61 $mid = mid_escape($mid);
63 'Archived-At', "<$base$mid/>",
64 'List-Archive', "<$base>",
65 'List-Post', "<mailto:$ibx->{-primary_address}>",
67 my $crlf = $simple->crlf;
68 my $buf = "From mboxrd\@z Thu Jan 1 00:00:00 1970\n" .
69 $header_obj->as_string;
70 for (my $i = 0; $i < @append; $i += 2) {
72 my $v = $append[$i + 1];
73 my @v = $header_obj->header($k);
80 $buf .= "$k: $v$crlf" if defined $v;
84 # mboxrd quoting style
85 # ref: http://www.qmail.org/man/man5/mbox.html
86 my $body = $simple->body;
87 $body =~ s/^(>*From )/>$1/gm;
93 my ($ctx, $srch, $sfx) = @_;
94 eval { require IO::Compress::Gzip };
95 return sub { need_gzip(@_) } if $@;
97 my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
98 PublicInbox::MboxGz->response($ctx, $cb);
102 my ($ctx, $range) = @_;
105 if ($range eq 'all') { # TODO: YYYY[-MM]
108 return [404, [qw(Content-Type text/plain)], []];
110 mbox_all($ctx, $query);
114 my ($ctx, $query) = @_;
116 eval { require IO::Compress::Gzip };
117 return sub { need_gzip(@_) } if $@;
118 my $cb = sub { $ctx->{srch}->query($query, @_) };
119 PublicInbox::MboxGz->response($ctx, $cb, 'results-'.$query);
123 my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
124 my $title = 'gzipped mbox not available';
126 <html><head><title>$title</title><body><pre>$title
127 The administrator needs to install the IO::Compress::Gzip Perl module
128 to support gzipped mboxes.
129 <a href="../">Return to index</a></pre></body></html>
136 package PublicInbox::MboxGz;
139 use PublicInbox::Hval qw/to_filename/;
142 my ($class, $ctx, $cb) = @_;
146 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
150 opts => { offset => 0 },
155 my ($class, $ctx, $cb, $fn) = @_;
156 my $body = $class->new($ctx, $cb);
157 # http://www.iana.org/assignments/media-types/application/gzip
158 $body->{hdr} = [ 'Content-Type', 'application/gzip' ];
160 my $hdr = $body->getline; # fill in Content-Disposition filename
161 [ 200, $hdr, $body ];
164 sub set_filename ($$) {
166 return to_filename($fn) if defined($fn);
168 PublicInbox::Mbox::subject_fn($msg);
171 # called by Plack::Util::foreach or similar
174 my $ctx = $self->{ctx} or return;
176 my $ibx = $ctx->{-inbox};
177 my $gz = $self->{gz};
179 # work on existing result set
180 while (defined(my $smsg = shift @{$self->{msgs}})) {
181 my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
182 $msg = Email::Simple->new($msg);
183 $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
185 # use subject of first message as subject
186 if (my $hdr = delete $self->{hdr}) {
187 my $fn = set_filename($self->{fn}, $msg);
188 push @$hdr, 'Content-Disposition',
189 "inline; filename=$fn.mbox.gz";
192 my $bref = $self->{buf};
193 if (length($$bref) >= 8192) {
194 my $ret = $$bref; # copy :<
195 ${$self->{buf}} = '';
199 # be fair to other clients on public-inbox-httpd:
204 $res = $self->{cb}->($self->{opts});
205 $self->{msgs} = $res->{msgs};
206 $res = scalar @{$self->{msgs}};
207 $self->{opts}->{offset} += $res;
211 ${delete $self->{buf}};