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