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