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