]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
16de1a728ab6345f58c0637f537bfca6485a44ce
[public-inbox.git] / lib / PublicInbox / Mbox.pm
1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Streaming (via getline) interface for formatting messages as an mboxrd.
5 # Used by the PSGI web interface.
6 #
7 # public-inbox-httpd favors "getline" response bodies to take a
8 # "pull"-based approach to feeding slow clients (as opposed to a
9 # more common "push" model)
10 package PublicInbox::Mbox;
11 use strict;
12 use warnings;
13 use PublicInbox::MID qw/mid_escape/;
14 use PublicInbox::Hval qw/to_filename/;
15 use PublicInbox::Smsg;
16 use PublicInbox::WwwStream qw(html_oneshot);
17 use Email::Simple;
18 use Email::MIME::Encode;
19
20 sub subject_fn ($) {
21         my ($hdr) = @_;
22         my $fn = $hdr->header('Subject');
23         return 'no-subject' if (!defined($fn) || $fn eq '');
24
25         # no need for full Email::MIME, here
26         if ($fn =~ /=\?/) {
27                 eval { $fn = Encode::decode('MIME-Header', $fn) };
28                 return 'no-subject' if $@;
29         }
30         $fn =~ s/^re:\s+//i;
31         $fn eq '' ? 'no-subject' : to_filename($fn);
32 }
33
34 sub mb_stream {
35         my ($more) = @_;
36         bless $more, 'PublicInbox::Mbox';
37 }
38
39 # called by PSGI server as body response
40 # this gets called twice for every message, once to return the header,
41 # once to retrieve the body
42 sub getline {
43         my ($more) = @_; # self
44         my ($ctx, $id, $prev, $next, $mref, $hdr) = @$more;
45         if ($hdr) { # first message hits this, only
46                 pop @$more; # $hdr
47                 pop @$more; # $mref
48                 return msg_hdr($ctx, $hdr) . msg_body($$mref);
49         }
50         my $cur = $next or return;
51         my $ibx = $ctx->{-inbox};
52         $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
53         $mref = $ibx->msg_by_smsg($cur) or return;
54         $hdr = Email::Simple->new($mref)->header_obj;
55         @$more = ($ctx, $id, $prev, $next); # $next may be undef, here
56         msg_hdr($ctx, $hdr) . msg_body($$mref);
57 }
58
59 sub close {} # noop
60
61 # /$INBOX/$MESSAGE_ID/raw
62 sub emit_raw {
63         my ($ctx) = @_;
64         my $mid = $ctx->{mid};
65         my $ibx = $ctx->{-inbox};
66         $ctx->{base_url} = $ibx->base_url($ctx->{env});
67         my ($mref, $more, $id, $prev, $next);
68         if (my $over = $ibx->over) {
69                 my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
70                 $mref = $ibx->msg_by_smsg($smsg) or return;
71                 $next = $over->next_by_mid($mid, \$id, \$prev);
72         } else {
73                 $mref = $ibx->msg_by_mid($mid) or return;
74         }
75         my $hdr = Email::Simple->new($mref)->header_obj;
76         $more = [ $ctx, $id, $prev, $next, $mref, $hdr ]; # for ->getline
77         my $fn = subject_fn($hdr);
78         my @hdr = ('Content-Type');
79         if ($ibx->{obfuscate}) {
80                 # obfuscation is stupid, but maybe scrapers are, too...
81                 push @hdr, 'application/mbox';
82                 $fn .= '.mbox';
83         } else {
84                 push @hdr, 'text/plain';
85                 $fn .= '.txt';
86         }
87         push @hdr, 'Content-Disposition', "inline; filename=$fn";
88         [ 200, \@hdr, mb_stream($more) ];
89 }
90
91 sub msg_hdr ($$;$) {
92         my ($ctx, $header_obj, $mid) = @_;
93
94         # drop potentially confusing headers, ssoma already should've dropped
95         # Lines and Content-Length
96         foreach my $d (qw(Lines Bytes Content-Length Status)) {
97                 $header_obj->header_set($d);
98         }
99         my $ibx = $ctx->{-inbox};
100         my $base = $ctx->{base_url};
101         $mid = $ctx->{mid} unless defined $mid;
102         $mid = mid_escape($mid);
103         my @append = (
104                 'Archived-At', "<$base$mid/>",
105                 'List-Archive', "<$base>",
106                 'List-Post', "<mailto:$ibx->{-primary_address}>",
107         );
108         my $crlf = $header_obj->crlf;
109         my $buf = 'From mboxrd@z Thu Jan  1 00:00:00 1970' . $crlf .
110                         $header_obj->as_string;
111         for (my $i = 0; $i < @append; $i += 2) {
112                 my $k = $append[$i];
113                 my $v = $append[$i + 1];
114                 my @v = $header_obj->header($k);
115                 foreach (@v) {
116                         if ($v eq $_) {
117                                 $v = undef;
118                                 last;
119                         }
120                 }
121                 $buf .= "$k: $v$crlf" if defined $v;
122         }
123         $buf .= $crlf;
124 }
125
126 sub msg_body ($) {
127         # mboxrd quoting style
128         # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
129         # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
130         # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
131         $_[0] =~ s/^(>*From )/>$1/gm;
132         $_[0] .= "\n";
133 }
134
135 sub thread_cb {
136         my ($ctx) = @_;
137         my $msgs = $ctx->{msgs};
138         while (1) {
139                 if (my $smsg = shift @$msgs) {
140                         return $smsg;
141                 }
142                 # refill result set
143                 $ctx->{msgs} = $msgs = $ctx->{over}->get_thread($ctx->{mid},
144                                                                 $ctx->{prev});
145                 return unless @$msgs;
146                 $ctx->{prev} = $msgs->[-1];
147         }
148 }
149
150 sub thread_mbox {
151         my ($ctx, $over, $sfx) = @_;
152         eval { require PublicInbox::MboxGz };
153         return need_gzip($ctx) if $@;
154         my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
155         return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
156         $ctx->{prev} = $msgs->[-1];
157         $ctx->{over} = $over; # bump refcnt
158         PublicInbox::MboxGz->response($ctx, \&thread_cb, $msgs->[0]->subject);
159 }
160
161 sub emit_range {
162         my ($ctx, $range) = @_;
163
164         my $query;
165         if ($range eq 'all') { # TODO: YYYY[-MM]
166                 $query = '';
167         } else {
168                 return [404, [qw(Content-Type text/plain)], []];
169         }
170         mbox_all($ctx, $query);
171 }
172
173 sub all_ids_cb {
174         my ($ctx) = @_;
175         my $ids = $ctx->{ids};
176         do {
177                 while ((my $num = shift @$ids)) {
178                         my $smsg = $ctx->{over}->get_art($num) or next;
179                         return $smsg;
180                 }
181                 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
182         } while (@$ids);
183 }
184
185 sub mbox_all_ids {
186         my ($ctx) = @_;
187         my $ibx = $ctx->{-inbox};
188         my $prev = 0;
189         my $mm = $ctx->{mm} = $ibx->mm;
190         my $ids = $mm->ids_after(\$prev) or return
191                 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
192         $ctx->{over} = $ibx->over or
193                 return PublicInbox::WWW::need($ctx, 'Overview');
194         $ctx->{ids} = $ids;
195         $ctx->{prev} = $prev;
196         return PublicInbox::MboxGz->response($ctx, \&all_ids_cb, 'all');
197 }
198
199 sub results_cb {
200         my ($ctx) = @_;
201         my $mset = $ctx->{mset};
202         my $srch = $ctx->{srch};
203         while (1) {
204                 while (my $mi = (($mset->items)[$ctx->{iter}++])) {
205                         my $smsg = PublicInbox::Smsg::from_mitem($mi,
206                                                                 $srch) or next;
207                         return $smsg;
208                 }
209                 # refill result set
210                 $mset = $ctx->{mset} = $srch->query($ctx->{query},
211                                                         $ctx->{qopts});
212                 my $size = $mset->size or return;
213                 $ctx->{qopts}->{offset} += $size;
214                 $ctx->{iter} = 0;
215         }
216 }
217
218 sub mbox_all {
219         my ($ctx, $query) = @_;
220
221         eval { require PublicInbox::MboxGz };
222         return need_gzip($ctx) if $@;
223         return mbox_all_ids($ctx) if $query eq '';
224         my $qopts = $ctx->{qopts} = { mset => 2 };
225         my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
226                 return PublicInbox::WWW::need($ctx, 'Search');;
227         my $mset = $ctx->{mset} = $srch->query($query, $qopts);
228         $qopts->{offset} = $mset->size or
229                         return [404, [qw(Content-Type text/plain)],
230                                 ["No results found\n"]];
231         $ctx->{iter} = 0;
232         $ctx->{query} = $query;
233         PublicInbox::MboxGz->response($ctx, \&results_cb, 'results-'.$query);
234 }
235
236 sub need_gzip {
237         html_oneshot($_[0], 501, \<<EOF);
238 <pre>gzipped mbox not available
239
240 The administrator needs to install the Compress::Raw::Zlib Perl module
241 to support gzipped mboxes.
242
243 <a href="../">Return to index</a></pre>
244 EOF
245 }
246
247 1;