]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
searchmsg: add git object ID to doc_data
[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/mid_clean/;
10 use URI::Escape qw/uri_escape_utf8/;
11 require Email::Simple;
12
13 sub emit1 {
14         my ($ctx, $msg) = @_;
15         $msg = Email::Simple->new($msg);
16         # single message should be easily renderable in browsers
17         [200, ['Content-Type', 'text/plain'], [ msg_str($ctx, $msg)] ]
18 }
19
20 sub msg_str {
21         my ($ctx, $simple) = @_; # Email::Simple object
22         my $header_obj = $simple->header_obj;
23
24         # drop potentially confusing headers, ssoma already should've dropped
25         # Lines and Content-Length
26         foreach my $d (qw(Lines Bytes Content-Length Status)) {
27                 $header_obj->header_set($d);
28         }
29         my $ibx = $ctx->{-inbox};
30         my $base = $ibx->base_url($ctx->{env});
31         my $mid = mid_clean($header_obj->header('Message-ID'));
32         $mid = uri_escape_utf8($mid);
33         my @append = (
34                 'Archived-At', "<$base$mid/>",
35                 'List-Archive', "<$base>",
36                 'List-Post', "<mailto:$ibx->{-primary_address}>",
37         );
38         my $crlf = $simple->crlf;
39         my $buf = "From mboxrd\@z Thu Jan  1 00:00:00 1970\n" .
40                         $header_obj->as_string;
41         for (my $i = 0; $i < @append; $i += 2) {
42                 my $k = $append[$i];
43                 my $v = $append[$i + 1];
44                 my @v = $header_obj->header($k);
45                 foreach (@v) {
46                         if ($v eq $_) {
47                                 $v = undef;
48                                 last;
49                         }
50                 }
51                 $buf .= "$k: $v$crlf" if defined $v;
52         }
53         $buf .= $crlf;
54
55         # mboxrd quoting style
56         # ref: http://www.qmail.org/man/man5/mbox.html
57         my $body = $simple->body;
58         $body =~ s/^(>*From )/>$1/gm;
59         $buf .= $body;
60         $buf .= "\n";
61 }
62
63 sub thread_mbox {
64         my ($ctx, $srch, $sfx) = @_;
65         eval { require IO::Compress::Gzip };
66         return sub { need_gzip(@_) } if $@;
67
68         my $cb = sub { $srch->get_thread($ctx->{mid}, @_) };
69         # http://www.iana.org/assignments/media-types/application/gzip
70         [200, ['Content-Type' => 'application/gzip'],
71                 PublicInbox::MboxGz->new($ctx, $cb) ];
72 }
73
74 sub emit_range {
75         my ($ctx, $range) = @_;
76
77         eval { require IO::Compress::Gzip };
78         return sub { need_gzip(@_) } if $@;
79         my $query;
80         if ($range eq 'all') { # TODO: YYYY[-MM]
81                 $query = '';
82         } else {
83                 return [404, [qw(Content-Type text/plain)], []];
84         }
85         my $cb = sub { $ctx->{srch}->query($query, @_) };
86
87         # http://www.iana.org/assignments/media-types/application/gzip
88         [200, [qw(Content-Type application/gzip)],
89                 PublicInbox::MboxGz->new($ctx, $cb) ];
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 package PublicInbox::MboxGz;
107 use strict;
108 use warnings;
109
110 sub new {
111         my ($class, $ctx, $cb) = @_;
112         my $buf = '';
113         bless {
114                 buf => \$buf,
115                 gz => IO::Compress::Gzip->new(\$buf, Time => 0),
116                 cb => $cb,
117                 ctx => $ctx,
118                 msgs => [],
119                 opts => { asc => 1, offset => 0 },
120         }, $class;
121 }
122
123 # called by Plack::Util::foreach or similar
124 sub getline {
125         my ($self) = @_;
126         my $ctx = $self->{ctx} or return;
127         my $res;
128         my $ibx = $ctx->{-inbox};
129         my $gz = $self->{gz};
130         do {
131                 while (defined(my $smsg = shift @{$self->{msgs}})) {
132                         my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
133                         $msg = Email::Simple->new($msg);
134                         $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
135                         my $bref = $self->{buf};
136                         if (length($$bref) >= 8192) {
137                                 my $ret = $$bref; # copy :<
138                                 ${$self->{buf}} = '';
139                                 return $ret;
140                         }
141                 }
142                 $res = $self->{cb}->($self->{opts});
143                 $self->{msgs} = $res->{msgs};
144                 $res = scalar @{$self->{msgs}};
145                 $self->{opts}->{offset} += $res;
146         } while ($res);
147         $gz->close;
148         delete $self->{ctx};
149         ${delete $self->{buf}};
150 }
151
152 sub close {} # noop
153
154 1;