]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Mbox.pm
3013dc91d9e5609ad6de78a0b754bbb7076c72df
[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                 return msg_hdr($ctx, $hdr);
48         }
49         if ($mref) { # all messages hit this
50                 pop @$more; # $mref
51                 return msg_body($$mref);
52         }
53         my $cur = $next or return;
54         my $ibx = $ctx->{-inbox};
55         $next = $ibx->over->next_by_mid($ctx->{mid}, \$id, \$prev);
56         $mref = $ibx->msg_by_smsg($cur) or return;
57         $hdr = Email::Simple->new($mref)->header_obj;
58         @$more = ($ctx, $id, $prev, $next, $mref); # $next may be undef, here
59         msg_hdr($ctx, $hdr); # all but first message hits this
60 }
61
62 sub close {} # noop
63
64 # /$INBOX/$MESSAGE_ID/raw
65 sub emit_raw {
66         my ($ctx) = @_;
67         my $mid = $ctx->{mid};
68         my $ibx = $ctx->{-inbox};
69         $ctx->{base_url} = $ibx->base_url($ctx->{env});
70         my ($mref, $more, $id, $prev, $next);
71         if (my $over = $ibx->over) {
72                 my $smsg = $over->next_by_mid($mid, \$id, \$prev) or return;
73                 $mref = $ibx->msg_by_smsg($smsg) or return;
74                 $next = $over->next_by_mid($mid, \$id, \$prev);
75         } else {
76                 $mref = $ibx->msg_by_mid($mid) or return;
77         }
78         my $hdr = Email::Simple->new($mref)->header_obj;
79         $more = [ $ctx, $id, $prev, $next, $mref, $hdr ]; # for ->getline
80         my $fn = subject_fn($hdr);
81         my @hdr = ('Content-Type');
82         if ($ibx->{obfuscate}) {
83                 # obfuscation is stupid, but maybe scrapers are, too...
84                 push @hdr, 'application/mbox';
85                 $fn .= '.mbox';
86         } else {
87                 push @hdr, 'text/plain';
88                 $fn .= '.txt';
89         }
90         push @hdr, 'Content-Disposition', "inline; filename=$fn";
91         [ 200, \@hdr, mb_stream($more) ];
92 }
93
94 sub msg_hdr ($$;$) {
95         my ($ctx, $header_obj, $mid) = @_;
96
97         # drop potentially confusing headers, ssoma already should've dropped
98         # Lines and Content-Length
99         foreach my $d (qw(Lines Bytes Content-Length Status)) {
100                 $header_obj->header_set($d);
101         }
102         my $ibx = $ctx->{-inbox};
103         my $base = $ctx->{base_url};
104         $mid = $ctx->{mid} unless defined $mid;
105         $mid = mid_escape($mid);
106         my @append = (
107                 'Archived-At', "<$base$mid/>",
108                 'List-Archive', "<$base>",
109                 'List-Post', "<mailto:$ibx->{-primary_address}>",
110         );
111         my $crlf = $header_obj->crlf;
112         my $buf = "From mboxrd\@z Thu Jan  1 00:00:00 1970\n" .
113                         $header_obj->as_string;
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;