]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: implement hybrid flat+thread conversation view
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # Main web interface for mailing list archives
5 #
6 # We focus on the lowest common denominators here:
7 # - targeted at text-only console browsers (w3m, links, etc..)
8 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
9 # - No JavaScript, graphics or icons allowed.
10 # - Must not rely on static content
11 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
12 package PublicInbox::WWW;
13 use 5.008;
14 use strict;
15 use warnings;
16 use Plack::Request;
17 use PublicInbox::Config;
18 use PublicInbox::Hval;
19 use URI::Escape qw(uri_escape_utf8 uri_unescape);
20 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
21 use constant PI_URL => '//public-inbox.org/';
22 require PublicInbox::Git;
23 use PublicInbox::GitHTTPBackend;
24 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
25 our $MID_RE = qr!([^/]+)!;
26 our $END_RE = qr!(t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
27 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
28
29 sub new {
30         my ($class, $pi_config) = @_;
31         $pi_config ||= PublicInbox::Config->new;
32         bless { pi_config => $pi_config }, $class;
33 }
34
35 # backwards compatibility, do not use
36 sub run {
37         my ($req, $method) = @_;
38         PublicInbox::WWW->new->call($req->env);
39 }
40
41 sub call {
42         my ($self, $env) = @_;
43         my $cgi = Plack::Request->new($env);
44         my $ctx = { cgi => $cgi, env => $env, www => $self,
45                 pi_config => $self->{pi_config} };
46
47         # we don't care about multi-value
48         my %qp = map {
49                 my ($k, $v) = split('=', $_, 2);
50                 $v = '' unless defined $v;
51                 $v =~ tr/+/ /;
52                 ($k, $v)
53         } split(/[&;]/, uri_unescape($env->{QUERY_STRING}));
54         $ctx->{qp} = \%qp;
55
56         my $path_info = $env->{PATH_INFO};
57         my $method = $env->{REQUEST_METHOD};
58
59         if ($method eq 'POST' &&
60                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
61                 my $path = $2;
62                 return (invalid_inbox($self, $ctx, $1) ||
63                         serve_git($env, $ctx->{git}, $path));
64         }
65         elsif ($method !~ /\AGET|HEAD\z/) {
66                 return r(405, 'Method Not Allowed');
67         }
68
69         # top-level indices and feeds
70         if ($path_info eq '/') {
71                 r404();
72         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
73                 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
74         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
75                 invalid_inbox($self, $ctx, $1) || get_index($ctx);
76         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
77                 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
78
79         } elsif ($path_info =~ m!$INBOX_RE/
80                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
81                 my $path = $2;
82                 invalid_inbox($self, $ctx, $1) ||
83                         serve_git($env, $ctx->{git}, $path);
84         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
85                 serve_mbox_range($self, $ctx, $1, $2);
86         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
87                 msg_page($self, $ctx, $1, $2, $3);
88
89         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
90                 my ($idx, $fn) = ($3, $4);
91                 invalid_inbox_mid($self, $ctx, $1, $2) ||
92                         get_attach($ctx, $idx, $fn);
93         # in case people leave off the trailing slash:
94         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(?:T|T/|t)\z!o) {
95                 my ($inbox, $mid) = ($1, $2);
96                 r301($ctx, $inbox, $mid, 't/#u');
97
98         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
99                 my ($inbox, $mid) = ($1, $2);
100                 r301($ctx, $inbox, $mid, '#R');
101
102         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
103                 r301($ctx, $1, $2);
104
105         # convenience redirects order matters
106         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
107                 r301($ctx, $1, $2);
108
109         } else {
110                 legacy_redirects($self, $ctx, $path_info);
111         }
112 }
113
114 # for CoW-friendliness, MOOOOO!
115 sub preload {
116         require PublicInbox::Feed;
117         require PublicInbox::View;
118         require PublicInbox::Thread;
119         require Email::MIME;
120         require Digest::SHA;
121         require POSIX;
122
123         foreach (qw(PublicInbox::Search PublicInbox::SearchView
124                         PublicInbox::Mbox IO::Compress::Gzip
125                         PublicInbox::NewsWWW)) {
126                 eval "require $_;";
127         }
128 }
129
130 # private functions below
131
132 sub r404 {
133         my ($ctx) = @_;
134         if ($ctx && $ctx->{mid}) {
135                 require PublicInbox::ExtMsg;
136                 searcher($ctx);
137                 return PublicInbox::ExtMsg::ext_msg($ctx);
138         }
139         r(404, 'Not Found');
140 }
141
142 # simple response for errors
143 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
144
145 # returns undef if valid, array ref response if invalid
146 sub invalid_inbox {
147         my ($self, $ctx, $inbox) = @_;
148         my $obj = $ctx->{pi_config}->lookup_name($inbox);
149         if (defined $obj) {
150                 $ctx->{git_dir} = $obj->{mainrepo};
151                 $ctx->{git} = $obj->git;
152                 # for PublicInbox::HTTP::weaken_task:
153                 $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
154                 $ctx->{-inbox} = $obj;
155                 $ctx->{inbox} = $inbox;
156                 return;
157         }
158
159         # sometimes linkifiers (not ours!) screw up automatic link
160         # generation and link things intended for nntp:// to https?://,
161         # so try to infer links and redirect them to the appropriate
162         # list URL.
163         $self->news_www->call($ctx->{cgi}->{env});
164 }
165
166 # returns undef if valid, array ref response if invalid
167 sub invalid_inbox_mid {
168         my ($self, $ctx, $inbox, $mid) = @_;
169         my $ret = invalid_inbox($self, $ctx, $inbox);
170         return $ret if $ret;
171
172         $ctx->{mid} = $mid = uri_unescape($mid);
173         if ($mid =~ /\A[a-f0-9]{40}\z/) {
174                 # this is horiffically wasteful for legacy URLs:
175                 if ($mid = mid2blob($ctx)) {
176                         require Email::Simple;
177                         use PublicInbox::MID qw/mid_clean/;
178                         my $s = Email::Simple->new($mid);
179                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
180                 }
181         }
182         undef;
183 }
184
185 # /$INBOX/new.atom                     -> Atom feed, includes replies
186 sub get_atom {
187         my ($ctx) = @_;
188         require PublicInbox::Feed;
189         PublicInbox::Feed::generate($ctx);
190 }
191
192 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
193 sub get_index {
194         my ($ctx) = @_;
195         require PublicInbox::Feed;
196         my $srch = searcher($ctx);
197         footer($ctx);
198         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
199                 require PublicInbox::SearchView;
200                 PublicInbox::SearchView::sres_top_html($ctx);
201         } else {
202                 PublicInbox::Feed::generate_html_index($ctx);
203         }
204 }
205
206 # just returns a string ref for the blob in the current ctx
207 sub mid2blob {
208         my ($ctx) = @_;
209         $ctx->{-inbox}->msg_by_mid($ctx->{mid});
210 }
211
212 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
213 sub get_mid_txt {
214         my ($ctx) = @_;
215         my $x = mid2blob($ctx) or return r404($ctx);
216         require PublicInbox::Mbox;
217         PublicInbox::Mbox::emit1($ctx, $x);
218 }
219
220 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
221 sub get_mid_html {
222         my ($ctx) = @_;
223         my $x = mid2blob($ctx) or return r404($ctx);
224
225         require PublicInbox::View;
226         my $foot = footer($ctx);
227         require Email::MIME;
228         my $mime = Email::MIME->new($x);
229         searcher($ctx);
230         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
231           PublicInbox::View::msg_html($ctx, $mime, $foot) ];
232 }
233
234 # /$INBOX/$MESSAGE_ID/t/
235 sub get_thread {
236         my ($ctx, $flat) = @_;
237         my $srch = searcher($ctx) or return need_search($ctx);
238         require PublicInbox::View;
239         my $foot = footer($ctx);
240         $ctx->{flat} = $flat;
241         PublicInbox::View::thread_html($ctx, $foot, $srch);
242 }
243
244 sub ctx_get {
245         my ($ctx, $key) = @_;
246         my $val = $ctx->{$key};
247         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
248         $val;
249 }
250
251 sub footer {
252         my ($ctx) = @_;
253         return '' unless $ctx;
254         my $obj = $ctx->{-inbox} or return '';
255
256         # auto-generate a footer
257         chomp(my $desc = $obj->description);
258         $desc = PublicInbox::Hval::ascii_html($desc);
259
260         my $urls;
261         my @urls = @{$obj->cloneurl};
262         my %seen = map { $_ => 1 } @urls;
263         my $cgi = $ctx->{cgi};
264         my $http = $cgi->base->as_string . $obj->{name};
265         $seen{$http} or unshift @urls, $http;
266         my $ssoma_url = PublicInbox::Hval::prurl($ctx->{env}, SSOMA_URL);
267         if (scalar(@urls) == 1) {
268                 $urls = "URL for <a\nhref=\"" . $ssoma_url .
269                         qq(">ssoma</a> or <b>git clone --mirror $urls[0]</b>);
270         } else {
271                 $urls = "URLs for <a\nhref=\"" . $ssoma_url .
272                         qq(">ssoma</a> or <b>git clone --mirror</b>\n) .
273                         join("\n", map { "\tgit clone --mirror $_" } @urls);
274         }
275
276         my $addr = $obj->{-primary_address};
277         $ctx->{footer} = join("\n",
278                 '- ' . $desc,
279                 "A <a\nhref=\"" .
280                         PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
281                         '">public-inbox</a>, ' .
282                         'anybody may post in plain-text (not HTML):',
283                 $addr,
284                 $urls
285         );
286 }
287
288 # search support is optional, returns undef if Xapian is not installed
289 # or not configured for the given GIT_DIR
290 sub searcher {
291         my ($ctx) = @_;
292         eval {
293                 require PublicInbox::Search;
294                 $ctx->{srch} = $ctx->{-inbox}->search;
295         };
296 }
297
298 sub need_search {
299         my ($ctx) = @_;
300         my $msg = <<EOF;
301 <html><head><title>Search not available for this
302 public-inbox</title><body><pre>Search is not available for this public-inbox
303 <a href="../">Return to index</a></pre></body></html>
304 EOF
305         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
306 }
307
308 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
309 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
310 # note: I'm not a big fan of other compression formats since they're
311 # significantly more expensive on CPU than gzip and less-widely available,
312 # especially on older systems.  Stick to zlib since that's what git uses.
313 sub get_thread_mbox {
314         my ($ctx, $sfx) = @_;
315         my $srch = searcher($ctx) or return need_search($ctx);
316         require PublicInbox::Mbox;
317         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
318 }
319
320
321 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
322 sub get_thread_atom {
323         my ($ctx) = @_;
324         searcher($ctx) or return need_search($ctx);
325         $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
326         require PublicInbox::Feed;
327         PublicInbox::Feed::generate_thread_atom($ctx);
328 }
329
330 sub legacy_redirects {
331         my ($self, $ctx, $path_info) = @_;
332
333         # single-message pages
334         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
335                 r301($ctx, $1, $2);
336         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
337                 r301($ctx, $1, $2, 'raw');
338
339         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
340                 r301($ctx, $1, $2);
341
342         # thread display
343         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
344                 r301($ctx, $1, $2, 't/#u');
345
346         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
347                 r301($ctx, $1, $2, "t.mbox$3");
348
349         # even older legacy redirects
350         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
351                 r301($ctx, $1, $2);
352
353         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
354                 r301($ctx, $1, $2, 't/#u');
355
356         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
357                 r301($ctx, $1, $2);
358
359         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
360                 r301($ctx, $1, $2, 'raw');
361
362         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
363                 r301($ctx, $1, $2, "t$3");
364
365         # legacy convenience redirects, order still matters
366         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
367                 r301($ctx, $1, $2);
368         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
369                 r301($ctx, $1, $2, 't/#u');
370         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
371                 r301($ctx, $1, $2);
372
373         # some Message-IDs have slashes in them and the HTTP server
374         # may try to be clever and unescape them :<
375         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
376                 msg_page($self, $ctx, $1, $2, $3);
377
378         # in case people leave off the trailing slash:
379         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
380                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
381         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
382                 r301($ctx, $1, $2);
383         } else {
384                 $self->news_www->call($ctx->{cgi}->{env});
385         }
386 }
387
388 sub r301 {
389         my ($ctx, $inbox, $mid, $suffix) = @_;
390         my $cgi = $ctx->{cgi};
391         my $obj = $ctx->{-inbox};
392         unless ($obj) {
393                 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
394                 return $r404 if $r404;
395                 $obj = $ctx->{-inbox};
396         }
397         my $url = $obj->base_url($cgi);
398         my $qs = $ctx->{env}->{QUERY_STRING};
399         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
400         $url .= $suffix if (defined $suffix);
401         $url .= "?$qs" if $qs ne '';
402
403         [ 301,
404           [ Location => $url, 'Content-Type' => 'text/plain' ],
405           [ "Redirecting to $url\n" ] ]
406 }
407
408 sub msg_page {
409         my ($self, $ctx, $inbox, $mid, $e) = @_;
410         my $ret;
411         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
412         '' eq $e and return get_mid_html($ctx);
413         't/' eq $e and return get_thread($ctx);
414         't.atom' eq $e and return get_thread_atom($ctx);
415         't.mbox' eq $e and return get_thread_mbox($ctx);
416         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
417         'T/' eq $e and return get_thread($ctx, 1);
418         'raw' eq $e and return get_mid_txt($ctx);
419
420         # legacy, but no redirect for compatibility:
421         'f/' eq $e and return get_mid_html($ctx);
422         r404($ctx);
423 }
424
425 sub serve_git {
426         my ($env, $git, $path) = @_;
427         PublicInbox::GitHTTPBackend::serve($env, $git, $path);
428 }
429
430 sub serve_mbox_range {
431         my ($self, $ctx, $inbox, $range) = @_;
432         invalid_inbox($self, $ctx, $inbox) || eval {
433                 require PublicInbox::Mbox;
434                 searcher($ctx);
435                 PublicInbox::Mbox::emit_range($ctx, $range);
436         }
437 }
438
439 sub news_www {
440         my ($self) = @_;
441         my $nw = $self->{news_www};
442         return $nw if $nw;
443         require PublicInbox::NewsWWW;
444         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
445 }
446
447 sub get_attach {
448         my ($ctx, $idx, $fn) = @_;
449         require PublicInbox::WwwAttach;
450         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
451 }
452
453 1;