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