]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
mbox: generate Archived-At, List-Post, List-Archive headers
[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/mid2path mid_clean/;
8 use URI::Escape qw/uri_escape_utf8/;
9 require Email::Simple;
10
11 sub thread_mbox {
12         my ($ctx, $srch, $sfx) = @_;
13         sub {
14                 my ($response) = @_; # Plack callback
15                 emit_mbox($response, $ctx, $srch, $sfx);
16         }
17 }
18
19 sub emit1 {
20         my $simple = Email::Simple->new(pop);
21         my $ctx = pop;
22         sub {
23                 my ($response) = @_;
24                 # single message should be easily renderable in browsers
25                 my $fh = $response->([200, ['Content-Type'=>'text/plain']]);
26                 emit_msg($ctx, $fh, $simple);
27                 $fh->close;
28         }
29 }
30
31 sub emit_msg {
32         my ($ctx, $fh, $simple) = @_; # Email::Simple object
33         my $header_obj = $simple->header_obj;
34
35         # drop potentially confusing headers, ssoma already should've dropped
36         # Lines and Content-Length
37         foreach my $d (qw(Lines Bytes Content-Length Status)) {
38                 $header_obj->header_set($d);
39         }
40         my $feed_opts = $ctx->{feed_opts};
41         unless ($feed_opts) {
42                 require PublicInbox::Feed; # FIXME: gross
43                 $feed_opts = PublicInbox::Feed::get_feedopts($ctx);
44                 $ctx->{feed_opts} = $feed_opts;
45         }
46         my $base = $feed_opts->{url};
47         my $mid = mid_clean($header_obj->header('Message-ID'));
48         $mid = uri_escape_utf8($mid);
49         my @archived_at = $header_obj->header('Archived-At');
50         push @archived_at, "<$base$mid/>";
51         $header_obj->header_set('Archived-At', @archived_at);
52         $header_obj->header_set('List-Archive', "<$base>");
53         $header_obj->header_set('List-Post', "<mailto:$feed_opts->{id_addr}>");
54
55         my $buf = $header_obj->as_string;
56         unless ($buf =~ /\AFrom /) {
57                 $fh->write("From mboxrd\@z Thu Jan  1 00:00:00 1970\n");
58         }
59         $fh->write($buf .= $simple->crlf);
60
61         $buf = $simple->body;
62         $simple->body_set('');
63
64         # mboxrd quoting style
65         # ref: http://www.qmail.org/man/man5/mbox.html
66         $buf =~ s/^(>*From )/>$1/gm;
67
68         $buf .= "\n" unless $buf =~ /\n\z/s;
69
70         $fh->write($buf);
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::GitCatFile;
88         my $mid = $ctx->{mid};
89         my $git = PublicInbox::GitCatFile->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;