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