]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
mbox: show application/mbox for obfuscated inboxes
[public-inbox.git] / lib / PublicInbox / Mbox.pm
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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 require Email::Simple;
11
12 sub emit1 {
13         my ($ctx, $msg) = @_;
14         $msg = Email::Simple->new($msg);
15
16         # single message should be easily renderable in browsers,
17         # unless obfuscation is enabled :<
18         [ 200, [ 'Content-Type',
19           $ctx->{-inbox}->{obfuscate} ? 'application/mbox' : 'text/plain' ],
20          [ msg_str($ctx, $msg)] ]
21 }
22
23 sub msg_str {
24         my ($ctx, $simple) = @_; # Email::Simple object
25         my $header_obj = $simple->header_obj;
26
27         # drop potentially confusing headers, ssoma already should've dropped
28         # Lines and Content-Length
29         foreach my $d (qw(Lines Bytes Content-Length Status)) {
30                 $header_obj->header_set($d);
31         }
32         my $ibx = $ctx->{-inbox};
33         my $base = $ibx->base_url($ctx->{env});
34         my $mid = mid_clean($header_obj->header('Message-ID'));
35         $mid = mid_escape($mid);
36         my @append = (
37                 'Archived-At', "<$base$mid/>",
38                 'List-Archive', "<$base>",
39                 'List-Post', "<mailto:$ibx->{-primary_address}>",
40         );
41         my $crlf = $simple->crlf;
42         my $buf = "From mboxrd\@z Thu Jan  1 00:00:00 1970\n" .
43                         $header_obj->as_string;
44         for (my $i = 0; $i < @append; $i += 2) {
45                 my $k = $append[$i];
46                 my $v = $append[$i + 1];
47                 my @v = $header_obj->header($k);
48                 foreach (@v) {
49                         if ($v eq $_) {
50                                 $v = undef;
51                                 last;
52                         }
53                 }
54                 $buf .= "$k: $v$crlf" if defined $v;
55         }
56         $buf .= $crlf;
57
58         # mboxrd quoting style
59         # ref: http://www.qmail.org/man/man5/mbox.html
60         my $body = $simple->body;
61         $body =~ s/^(>*From )/>$1/gm;
62         $buf .= $body;
63         $buf .= "\n";
64 }
65
66 sub thread_mbox {
67         my ($ctx, $srch, $sfx) = @_;
68         eval { require IO::Compress::Gzip };
69         return sub { need_gzip(@_) } if $@;
70
71         my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
72         # http://www.iana.org/assignments/media-types/application/gzip
73         [200, ['Content-Type' => 'application/gzip'],
74                 PublicInbox::MboxGz->new($ctx, $cb) ];
75 }
76
77 sub emit_range {
78         my ($ctx, $range) = @_;
79
80         eval { require IO::Compress::Gzip };
81         return sub { need_gzip(@_) } if $@;
82         my $query;
83         if ($range eq 'all') { # TODO: YYYY[-MM]
84                 $query = '';
85         } else {
86                 return [404, [qw(Content-Type text/plain)], []];
87         }
88         my $cb = sub { $ctx->{srch}->query($query, @_) };
89
90         # http://www.iana.org/assignments/media-types/application/gzip
91         [200, [qw(Content-Type application/gzip)],
92                 PublicInbox::MboxGz->new($ctx, $cb) ];
93 }
94
95 sub need_gzip {
96         my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
97         my $title = 'gzipped mbox not available';
98         $fh->write(<<EOF);
99 <html><head><title>$title</title><body><pre>$title
100 The administrator needs to install the IO::Compress::Gzip Perl module
101 to support gzipped mboxes.
102 <a href="../">Return to index</a></pre></body></html>
103 EOF
104         $fh->close;
105 }
106
107 1;
108
109 package PublicInbox::MboxGz;
110 use strict;
111 use warnings;
112
113 sub new {
114         my ($class, $ctx, $cb) = @_;
115         my $buf = '';
116         bless {
117                 buf => \$buf,
118                 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
119                 cb => $cb,
120                 ctx => $ctx,
121                 msgs => [],
122                 opts => { offset => 0 },
123         }, $class;
124 }
125
126 # called by Plack::Util::foreach or similar
127 sub getline {
128         my ($self) = @_;
129         my $ctx = $self->{ctx} or return;
130         my $res;
131         my $ibx = $ctx->{-inbox};
132         my $gz = $self->{gz};
133         do {
134                 while (defined(my $smsg = shift @{$self->{msgs}})) {
135                         my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
136                         $msg = Email::Simple->new($msg);
137                         $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
138                         my $bref = $self->{buf};
139                         if (length($$bref) >= 8192) {
140                                 my $ret = $$bref; # copy :<
141                                 ${$self->{buf}} = '';
142                                 return $ret;
143                         }
144
145                         # be fair to other clients on public-inbox-httpd:
146                         return '';
147                 }
148                 $res = $self->{cb}->($self->{opts});
149                 $self->{msgs} = $res->{msgs};
150                 $res = scalar @{$self->{msgs}};
151                 $self->{opts}->{offset} += $res;
152         } while ($res);
153         $gz->close;
154         delete $self->{ctx};
155         ${delete $self->{buf}};
156 }
157
158 sub close {} # noop
159
160 1;