]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
mbox: consistent header order when decompressed
[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/mid2path mid_clean/;
10 use URI::Escape qw/uri_escape_utf8/;
11 require Email::Simple;
12
13 sub thread_mbox {
14         my ($ctx, $srch, $sfx) = @_;
15         sub {
16                 my ($response) = @_; # Plack callback
17                 emit_mbox($response, $ctx, $srch, $sfx);
18         }
19 }
20
21 sub emit1 {
22         my $simple = Email::Simple->new(pop);
23         my $ctx = pop;
24         sub {
25                 my ($response) = @_;
26                 # single message should be easily renderable in browsers
27                 my $fh = $response->([200, ['Content-Type'=>'text/plain']]);
28                 emit_msg($ctx, $fh, $simple);
29                 $fh->close;
30         }
31 }
32
33 sub emit_msg {
34         my ($ctx, $fh, $simple) = @_; # Email::Simple object
35         my $header_obj = $simple->header_obj;
36
37         # drop potentially confusing headers, ssoma already should've dropped
38         # Lines and Content-Length
39         foreach my $d (qw(Lines Bytes Content-Length Status)) {
40                 $header_obj->header_set($d);
41         }
42         my $feed_opts = $ctx->{feed_opts};
43         unless ($feed_opts) {
44                 require PublicInbox::Feed; # FIXME: gross
45                 $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
46                 $ctx->{feed_opts} = $feed_opts;
47         }
48         my $base = $feed_opts->{url};
49         my $mid = mid_clean($header_obj->header('Message-ID'));
50         $mid = uri_escape_utf8($mid);
51         my @append = (
52                 'Archived-At', "<$base$mid/>",
53                 'List-Archive', "<$base>",
54                 'List-Post', "<mailto:$feed_opts->{id_addr}>",
55         );
56         my $append = '';
57         my $crlf = $simple->crlf;
58         for (my $i = 0; $i < @append; $i += 2) {
59                 my $k = $append[$i];
60                 my $v = $append[$i + 1];
61                 my @v = $header_obj->header($k);
62                 foreach (@v) {
63                         if ($v eq $_) {
64                                 $v = undef;
65                                 last;
66                         }
67                 }
68                 $append .= "$k: $v$crlf" if defined $v;
69         }
70         my $buf = $header_obj->as_string;
71         unless ($buf =~ /\AFrom /) {
72                 $fh->write("From mboxrd\@z Thu Jan  1 00:00:00 1970\n");
73         }
74         $append .= $crlf;
75         $fh->write($buf .= $append);
76
77         $buf = $simple->body;
78         $simple->body_set('');
79
80         # mboxrd quoting style
81         # ref: http://www.qmail.org/man/man5/mbox.html
82         $buf =~ s/^(>*From )/>$1/gm;
83
84         $fh->write($buf .= "\n");
85 }
86
87 sub emit_mbox {
88         my ($response, $ctx, $srch, $sfx) = @_;
89         my $type = 'mbox';
90         if ($sfx) {
91                 eval { require IO::Compress::Gzip };
92                 return need_gzip($response) if $@;
93                 $type = 'gzip';
94         }
95
96         # http://www.iana.org/assignments/media-types/application/gzip
97         # http://www.iana.org/assignments/media-types/application/mbox
98         my $fh = $response->([200, ['Content-Type' => "application/$type"]]);
99         $fh = PublicInbox::MboxGz->new($fh) if $sfx;
100
101         require PublicInbox::Git;
102         my $mid = $ctx->{mid};
103         my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
104         my %opts = (offset => 0, asc => 1);
105         my $nr;
106         do {
107                 my $res = $srch->get_thread($mid, \%opts);
108                 my $msgs = $res->{msgs};
109                 $nr = scalar @$msgs;
110                 while (defined(my $smsg = shift @$msgs)) {
111                         my $msg = eval {
112                                 my $p = 'HEAD:'.mid2path($smsg->mid);
113                                 Email::Simple->new($git->cat_file($p));
114                         };
115                         emit_msg($ctx, $fh, $msg) if $msg;
116                 }
117
118                 $opts{offset} += $nr;
119         } while ($nr > 0);
120
121         $fh->close;
122 }
123
124 sub need_gzip {
125         my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
126         my $title = 'gzipped mbox not available';
127         $fh->write(<<EOF);
128 <html><head><title>$title</title><body><pre>$title
129 The administrator needs to install the IO::Compress::Gzip Perl module
130 to support gzipped mboxes.
131 <a href="../">Return to index</a></pre></body></html>
132 EOF
133         $fh->close;
134 }
135
136 1;
137
138 # fh may not be a proper IO, so we wrap the write and close methods
139 # to prevent IO::Compress::Gzip from complaining
140 package PublicInbox::MboxGz;
141 use strict;
142 use warnings;
143
144 sub new {
145         my ($class, $fh) = @_;
146         my $buf;
147         bless {
148                 buf => \$buf,
149                 gz => IO::Compress::Gzip->new(\$buf),
150                 fh => $fh,
151         }, $class;
152 }
153
154 sub _flush_buf {
155         my ($self) = @_;
156         if (defined ${$self->{buf}}) {
157                 $self->{fh}->write(${$self->{buf}});
158                 ${$self->{buf}} = undef;
159         }
160 }
161
162 sub write {
163         $_[0]->{gz}->write($_[1]);
164         _flush_buf($_[0]);
165 }
166
167 sub close {
168         my ($self) = @_;
169         $self->{gz}->close;
170         _flush_buf($self);
171         $self->{fh}->close;
172 }
173
174 1;