]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
switch to gzipped mboxes
[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 # Streaming interface for formatting messages as an mbox
4 package PublicInbox::Mbox;
5 use strict;
6 use warnings;
7 use PublicInbox::MID qw/mid_clean mid_compressed mid2path/;
8 use Fcntl qw(SEEK_SET);
9
10 sub thread_mbox {
11         my ($ctx, $srch) = @_;
12         sub {
13                 my ($response) = @_; # Plack callback
14                 emit_mbox($response, $ctx, $srch);
15         }
16 }
17
18 sub emit_msg {
19         my ($fh, $simple) = @_; # Email::Simple object
20
21         # drop potentially confusing headers, ssoma already should've dropped
22         # Lines and Content-Length
23         foreach my $d (qw(Lines Content-Length Status)) {
24                 $simple->header_set($d);
25         }
26
27         my $buf = $simple->header_obj->as_string;
28         unless ($buf =~ /\AFrom /) {
29                 $fh->write("From a\@a Thu Jan  1 00:00:00 1970\n");
30         }
31         $fh->write($buf .= $simple->crlf);
32
33         $buf = $simple->body;
34         $simple->body_set('');
35         $buf =~ s/^(From )/>$1/gm;
36         $buf .= "\n" unless $buf =~ /\n\z/s;
37
38         $fh->write($buf);
39 }
40
41 sub emit_mbox {
42         my ($response, $ctx, $srch) = @_;
43         eval { require IO::Compress::Gzip };
44         return need_gzip($response) if $@;
45
46         # http://www.iana.org/assignments/media-types/application/gzip
47         # http://www.iana.org/assignments/media-types/application/mbox
48         my $fh = $response->([200, ['Content-Type' => 'application/gzip']]);
49         $fh = PublicInbox::MboxGz->new($fh);
50
51         require PublicInbox::GitCatFile;
52         require Email::Simple;
53         my $mid = mid_compressed($ctx->{mid});
54         my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
55         my %opts = (offset => 0);
56         my $nr;
57         do {
58                 my $res = $srch->get_thread($mid, \%opts);
59                 my $msgs = $res->{msgs};
60                 $nr = scalar @$msgs;
61                 while (defined(my $smsg = shift @$msgs)) {
62                         my $msg = eval {
63                                 my $p = 'HEAD:'.mid2path($smsg->mid);
64                                 Email::Simple->new($git->cat_file($p));
65                         };
66                         emit_msg($fh, $msg) if $msg;
67                 }
68
69                 $opts{offset} += $nr;
70         } while ($nr > 0);
71
72         $fh->close;
73 }
74
75 sub need_gzip {
76         my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
77         my $title = 'gzipped mbox not available';
78         $fh->write(<<EOF);
79 <html><head><title>$title</title><body><pre>$title
80 The administrator needs to install the IO::Compress::Gzip Perl module
81 to support gzipped mboxes.
82 <a href="../">Return to index</a></pre></body></html>
83 EOF
84 }
85
86 1;
87
88 # fh may not be a proper IO, so we wrap the write and close methods
89 # to prevent IO::Compress::Gzip from complaining
90 package PublicInbox::MboxGz;
91 use strict;
92 use warnings;
93 use fields qw(gz fh buf);
94
95 sub new {
96         my ($class, $fh) = @_;
97         my $self = fields::new($class);
98         my $buf;
99         $self->{buf} = \$buf;
100         $self->{gz} = IO::Compress::Gzip->new(\$buf);
101         $self->{fh} = $fh;
102         $self;
103 }
104
105 sub _flush_buf {
106         my ($self) = @_;
107         if (defined ${$self->{buf}}) {
108                 $self->{fh}->write(${$self->{buf}});
109                 ${$self->{buf}} = undef;
110         }
111 }
112
113 sub write {
114         $_[0]->{gz}->write($_[1]);
115         _flush_buf($_[0]);
116 }
117
118 sub close {
119         my ($self) = @_;
120         $self->{gz}->close;
121         _flush_buf($self);
122         # do not actually close $fh
123 }
124
125 1;