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