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