]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
view: permalink (per-message) view shows multiple messages
[public-inbox.git] / lib / PublicInbox / Mbox.pm
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>
3
4 # Streaming interface for formatting messages as an mboxrd.
5 # Used by the web interface
6 package PublicInbox::Mbox;
7 use strict;
8 use warnings;
9 use PublicInbox::MID qw/mid_clean mid_escape/;
10 use PublicInbox::Hval qw/to_filename/;
11 use Email::Simple;
12 use Email::MIME::Encode;
13
14 sub subject_fn ($) {
15         my ($simple) = @_;
16         my $fn = $simple->header('Subject');
17         return 'no-subject' unless defined($fn);
18
19         # no need for full Email::MIME, here
20         if ($fn =~ /=\?/) {
21                 eval { $fn = Encode::decode('MIME-Header', $fn) };
22                 $fn = 'no-subject' if $@;
23         }
24         $fn =~ s/^re:\s+//i;
25         $fn = to_filename($fn);
26         $fn eq '' ? 'no-subject' : $fn;
27 }
28
29 sub mb_stream {
30         my ($more) = @_;
31         bless $more, 'PublicInbox::Mbox';
32 }
33
34 # called by PSGI server as body response
35 sub getline {
36         my ($more) = @_; # self
37         my ($ctx, $head, $tail, $db, $cur) = @$more;
38         if ($cur) {
39                 pop @$more;
40                 return msg_str($ctx, $cur);
41         }
42         for (; !defined($cur) && $head != $tail; $head++) {
43                 my $smsg = PublicInbox::SearchMsg->get($head, $db, $ctx->{mid});
44                 next if $smsg->type ne 'mail';
45                 my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
46                 $cur = Email::Simple->new($mref);
47                 $cur = msg_str($ctx, $cur);
48         }
49         $more->[1] = $head;
50         $cur;
51 }
52
53 sub close {} # noop
54
55 sub emit_raw {
56         my ($ctx) = @_;
57         my $mid = $ctx->{mid};
58         my $ibx = $ctx->{-inbox};
59         my $first;
60         my $more;
61         my ($head, $tail, $db);
62         my %seen;
63         if (my $srch = $ibx->search) {
64                 $srch->retry_reopen(sub {
65                         ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
66                         for (; !defined($first) && $head != $tail; $head++) {
67                                 my @args = ($head, $db, $mid);
68                                 my $smsg = PublicInbox::SearchMsg->get(@args);
69                                 next if $smsg->type ne 'mail';
70                                 my $mref = $ibx->msg_by_smsg($smsg) or next;
71                                 $first = Email::Simple->new($mref);
72                         }
73                         if ($head != $tail) {
74                                 $more = [ $ctx, $head, $tail, $db, $first ];
75                         }
76                 });
77         } else {
78                 my $mref = $ibx->msg_by_mid($mid) or return;
79                 $first = Email::Simple->new($mref);
80         }
81         return unless defined $first;
82         my $fn = subject_fn($first);
83         my @hdr = ('Content-Type');
84         if ($ibx->{obfuscate}) {
85                 # obfuscation is stupid, but maybe scrapers are, too...
86                 push @hdr, 'application/mbox';
87                 $fn .= '.mbox';
88         } else {
89                 push @hdr, 'text/plain';
90                 $fn .= '.txt';
91         }
92         push @hdr, 'Content-Disposition', "inline; filename=$fn";
93         [ 200, \@hdr, $more ? mb_stream($more) : [ msg_str($ctx, $first) ] ];
94 }
95
96 sub msg_str {
97         my ($ctx, $simple) = @_; # Email::Simple object
98         my $header_obj = $simple->header_obj;
99
100         # drop potentially confusing headers, ssoma already should've dropped
101         # Lines and Content-Length
102         foreach my $d (qw(Lines Bytes Content-Length Status)) {
103                 $header_obj->header_set($d);
104         }
105         my $ibx = $ctx->{-inbox};
106         my $base = $ibx->base_url($ctx->{env});
107         my $mid = mid_clean($header_obj->header('Message-ID'));
108         $mid = mid_escape($mid);
109         my @append = (
110                 'Archived-At', "<$base$mid/>",
111                 'List-Archive', "<$base>",
112                 'List-Post', "<mailto:$ibx->{-primary_address}>",
113         );
114         my $crlf = $simple->crlf;
115         my $buf = "From mboxrd\@z Thu Jan  1 00:00:00 1970\n" .
116                         $header_obj->as_string;
117         for (my $i = 0; $i < @append; $i += 2) {
118                 my $k = $append[$i];
119                 my $v = $append[$i + 1];
120                 my @v = $header_obj->header($k);
121                 foreach (@v) {
122                         if ($v eq $_) {
123                                 $v = undef;
124                                 last;
125                         }
126                 }
127                 $buf .= "$k: $v$crlf" if defined $v;
128         }
129         $buf .= $crlf;
130
131         # mboxrd quoting style
132         # ref: http://www.qmail.org/man/man5/mbox.html
133         my $body = $simple->body;
134         $body =~ s/^(>*From )/>$1/gm;
135         $buf .= $body;
136         $buf .= "\n";
137 }
138
139 sub thread_mbox {
140         my ($ctx, $srch, $sfx) = @_;
141         eval { require IO::Compress::Gzip };
142         return sub { need_gzip(@_) } if $@;
143
144         my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
145         PublicInbox::MboxGz->response($ctx, $cb);
146 }
147
148 sub emit_range {
149         my ($ctx, $range) = @_;
150
151         my $query;
152         if ($range eq 'all') { # TODO: YYYY[-MM]
153                 $query = '';
154         } else {
155                 return [404, [qw(Content-Type text/plain)], []];
156         }
157         mbox_all($ctx, $query);
158 }
159
160 sub mbox_all {
161         my ($ctx, $query) = @_;
162
163         eval { require IO::Compress::Gzip };
164         return sub { need_gzip(@_) } if $@;
165         my $cb = sub { $ctx->{srch}->query($query, @_) };
166         PublicInbox::MboxGz->response($ctx, $cb, 'results-'.$query);
167 }
168
169 sub need_gzip {
170         my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
171         my $title = 'gzipped mbox not available';
172         $fh->write(<<EOF);
173 <html><head><title>$title</title><body><pre>$title
174 The administrator needs to install the IO::Compress::Gzip Perl module
175 to support gzipped mboxes.
176 <a href="../">Return to index</a></pre></body></html>
177 EOF
178         $fh->close;
179 }
180
181 1;
182
183 package PublicInbox::MboxGz;
184 use strict;
185 use warnings;
186 use PublicInbox::Hval qw/to_filename/;
187
188 sub new {
189         my ($class, $ctx, $cb) = @_;
190         my $buf = '';
191         bless {
192                 buf => \$buf,
193                 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
194                 cb => $cb,
195                 ctx => $ctx,
196                 msgs => [],
197                 opts => { offset => 0 },
198         }, $class;
199 }
200
201 sub response {
202         my ($class, $ctx, $cb, $fn) = @_;
203         my $body = $class->new($ctx, $cb);
204         # http://www.iana.org/assignments/media-types/application/gzip
205         $body->{hdr} = [ 'Content-Type', 'application/gzip' ];
206         $body->{fn} = $fn;
207         my $hdr = $body->getline; # fill in Content-Disposition filename
208         [ 200, $hdr, $body ];
209 }
210
211 sub set_filename ($$) {
212         my ($fn, $msg) = @_;
213         return to_filename($fn) if defined($fn);
214
215         PublicInbox::Mbox::subject_fn($msg);
216 }
217
218 # called by Plack::Util::foreach or similar
219 sub getline {
220         my ($self) = @_;
221         my $ctx = $self->{ctx} or return;
222         my $res;
223         my $ibx = $ctx->{-inbox};
224         my $gz = $self->{gz};
225         do {
226                 # work on existing result set
227                 while (defined(my $smsg = shift @{$self->{msgs}})) {
228                         my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
229                         $msg = Email::Simple->new($msg);
230                         $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
231
232                         # use subject of first message as subject
233                         if (my $hdr = delete $self->{hdr}) {
234                                 my $fn = set_filename($self->{fn}, $msg);
235                                 push @$hdr, 'Content-Disposition',
236                                                 "inline; filename=$fn.mbox.gz";
237                                 return $hdr;
238                         }
239                         my $bref = $self->{buf};
240                         if (length($$bref) >= 8192) {
241                                 my $ret = $$bref; # copy :<
242                                 ${$self->{buf}} = '';
243                                 return $ret;
244                         }
245
246                         # be fair to other clients on public-inbox-httpd:
247                         return '';
248                 }
249
250                 # refill result set
251                 $res = $self->{cb}->($self->{opts});
252                 $self->{msgs} = $res->{msgs};
253                 $res = scalar @{$self->{msgs}};
254                 $self->{opts}->{offset} += $res;
255         } while ($res);
256         $gz->close;
257         delete $self->{ctx};
258         ${delete $self->{buf}};
259 }
260
261 sub close {} # noop
262
263 1;