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