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