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