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