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