]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
mbox: clarify our use of the the mboxrd variant
[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 mboxrd
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 mboxrd\@z 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
46         # mboxrd quoting style
47         # ref: http://www.qmail.org/man/man5/mbox.html
48         $buf =~ s/^(>*From )/>$1/gm;
49
50         $buf .= "\n" unless $buf =~ /\n\z/s;
51
52         $fh->write($buf);
53 }
54
55 sub emit_mbox {
56         my ($response, $ctx, $srch, $sfx) = @_;
57         my $type = 'mbox';
58         if ($sfx) {
59                 eval { require IO::Compress::Gzip };
60                 return need_gzip($response) if $@;
61                 $type = 'gzip';
62         }
63
64         # http://www.iana.org/assignments/media-types/application/gzip
65         # http://www.iana.org/assignments/media-types/application/mbox
66         my $fh = $response->([200, ['Content-Type' => "application/$type"]]);
67         $fh = PublicInbox::MboxGz->new($fh) if $sfx;
68
69         require PublicInbox::GitCatFile;
70         my $mid = mid_compressed($ctx->{mid});
71         my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
72         my %opts = (offset => 0);
73         my $nr;
74         do {
75                 my $res = $srch->get_thread($mid, \%opts);
76                 my $msgs = $res->{msgs};
77                 $nr = scalar @$msgs;
78                 while (defined(my $smsg = shift @$msgs)) {
79                         my $msg = eval {
80                                 my $p = 'HEAD:'.mid2path($smsg->mid);
81                                 Email::Simple->new($git->cat_file($p));
82                         };
83                         emit_msg($fh, $msg) if $msg;
84                 }
85
86                 $opts{offset} += $nr;
87         } while ($nr > 0);
88
89         $fh->close;
90 }
91
92 sub need_gzip {
93         my $fh = $_[0]->([501, ['Content-Type' => 'text/html']]);
94         my $title = 'gzipped mbox not available';
95         $fh->write(<<EOF);
96 <html><head><title>$title</title><body><pre>$title
97 The administrator needs to install the IO::Compress::Gzip Perl module
98 to support gzipped mboxes.
99 <a href="../">Return to index</a></pre></body></html>
100 EOF
101         $fh->close;
102 }
103
104 1;
105
106 # fh may not be a proper IO, so we wrap the write and close methods
107 # to prevent IO::Compress::Gzip from complaining
108 package PublicInbox::MboxGz;
109 use strict;
110 use warnings;
111 use fields qw(gz fh buf);
112
113 sub new {
114         my ($class, $fh) = @_;
115         my $self = fields::new($class);
116         my $buf;
117         $self->{buf} = \$buf;
118         $self->{gz} = IO::Compress::Gzip->new(\$buf);
119         $self->{fh} = $fh;
120         $self;
121 }
122
123 sub _flush_buf {
124         my ($self) = @_;
125         if (defined ${$self->{buf}}) {
126                 $self->{fh}->write(${$self->{buf}});
127                 ${$self->{buf}} = undef;
128         }
129 }
130
131 sub write {
132         $_[0]->{gz}->write($_[1]);
133         _flush_buf($_[0]);
134 }
135
136 sub close {
137         my ($self) = @_;
138         $self->{gz}->close;
139         _flush_buf($self);
140         $self->{fh}->close;
141 }
142
143 1;