]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
reduce scope of mbox From_ line removal
[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 = $header_obj->as_string;
110         # fixup old bug from import (pre-a0c07cba0e5d8b6a)
111         $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
112         $buf = "From mboxrd\@z Thu Jan  1 00:00:00 1970" . $crlf . $buf;
113
114         for (my $i = 0; $i < @append; $i += 2) {
115                 my $k = $append[$i];
116                 my $v = $append[$i + 1];
117                 my @v = $header_obj->header($k);
118                 foreach (@v) {
119                         if ($v eq $_) {
120                                 $v = undef;
121                                 last;
122                         }
123                 }
124                 $buf .= "$k: $v$crlf" if defined $v;
125         }
126         $buf .= $crlf;
127 }
128
129 sub msg_body ($) {
130         # mboxrd quoting style
131         # https://en.wikipedia.org/wiki/Mbox#Modified_mbox
132         # https://www.loc.gov/preservation/digital/formats/fdd/fdd000385.shtml
133         # https://web.archive.org/http://www.qmail.org/man/man5/mbox.html
134         $_[0] =~ s/^(>*From )/>$1/gm;
135         $_[0] .= "\n";
136 }
137
138 sub thread_cb {
139         my ($ctx) = @_;
140         my $msgs = $ctx->{msgs};
141         while (1) {
142                 if (my $smsg = shift @$msgs) {
143                         return $smsg;
144                 }
145                 # refill result set
146                 $ctx->{msgs} = $msgs = $ctx->{over}->get_thread($ctx->{mid},
147                                                                 $ctx->{prev});
148                 return unless @$msgs;
149                 $ctx->{prev} = $msgs->[-1];
150         }
151 }
152
153 sub thread_mbox {
154         my ($ctx, $over, $sfx) = @_;
155         eval { require PublicInbox::MboxGz };
156         return need_gzip($ctx) if $@;
157         my $msgs = $ctx->{msgs} = $over->get_thread($ctx->{mid}, {});
158         return [404, [qw(Content-Type text/plain)], []] if !@$msgs;
159         $ctx->{prev} = $msgs->[-1];
160         $ctx->{over} = $over; # bump refcnt
161         PublicInbox::MboxGz->response($ctx, \&thread_cb, $msgs->[0]->subject);
162 }
163
164 sub emit_range {
165         my ($ctx, $range) = @_;
166
167         my $query;
168         if ($range eq 'all') { # TODO: YYYY[-MM]
169                 $query = '';
170         } else {
171                 return [404, [qw(Content-Type text/plain)], []];
172         }
173         mbox_all($ctx, $query);
174 }
175
176 sub all_ids_cb {
177         my ($ctx) = @_;
178         my $ids = $ctx->{ids};
179         do {
180                 while ((my $num = shift @$ids)) {
181                         my $smsg = $ctx->{over}->get_art($num) or next;
182                         return $smsg;
183                 }
184                 $ctx->{ids} = $ids = $ctx->{mm}->ids_after(\($ctx->{prev}));
185         } while (@$ids);
186 }
187
188 sub mbox_all_ids {
189         my ($ctx) = @_;
190         my $ibx = $ctx->{-inbox};
191         my $prev = 0;
192         my $mm = $ctx->{mm} = $ibx->mm;
193         my $ids = $mm->ids_after(\$prev) or return
194                 [404, [qw(Content-Type text/plain)], ["No results found\n"]];
195         $ctx->{over} = $ibx->over or
196                 return PublicInbox::WWW::need($ctx, 'Overview');
197         $ctx->{ids} = $ids;
198         $ctx->{prev} = $prev;
199         return PublicInbox::MboxGz->response($ctx, \&all_ids_cb, 'all');
200 }
201
202 sub results_cb {
203         my ($ctx) = @_;
204         my $mset = $ctx->{mset};
205         my $srch = $ctx->{srch};
206         while (1) {
207                 while (my $mi = (($mset->items)[$ctx->{iter}++])) {
208                         my $smsg = PublicInbox::Smsg::from_mitem($mi,
209                                                                 $srch) or next;
210                         return $smsg;
211                 }
212                 # refill result set
213                 $mset = $ctx->{mset} = $srch->query($ctx->{query},
214                                                         $ctx->{qopts});
215                 my $size = $mset->size or return;
216                 $ctx->{qopts}->{offset} += $size;
217                 $ctx->{iter} = 0;
218         }
219 }
220
221 sub mbox_all {
222         my ($ctx, $query) = @_;
223
224         eval { require PublicInbox::MboxGz };
225         return need_gzip($ctx) if $@;
226         return mbox_all_ids($ctx) if $query eq '';
227         my $qopts = $ctx->{qopts} = { mset => 2 };
228         my $srch = $ctx->{srch} = $ctx->{-inbox}->search or
229                 return PublicInbox::WWW::need($ctx, 'Search');;
230         my $mset = $ctx->{mset} = $srch->query($query, $qopts);
231         $qopts->{offset} = $mset->size or
232                         return [404, [qw(Content-Type text/plain)],
233                                 ["No results found\n"]];
234         $ctx->{iter} = 0;
235         $ctx->{query} = $query;
236         PublicInbox::MboxGz->response($ctx, \&results_cb, 'results-'.$query);
237 }
238
239 sub need_gzip {
240         html_oneshot($_[0], 501, \<<EOF);
241 <pre>gzipped mbox not available
242
243 The administrator needs to install the Compress::Raw::Zlib Perl module
244 to support gzipped mboxes.
245
246 <a href="../">Return to index</a></pre>
247 EOF
248 }
249
250 1;