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)
4 # Main web interface for mailing list archives
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;
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/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
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;
33 if ($method eq 'POST' &&
34 $path_info =~ m!$LISTNAME_RE/(git-upload-pack)\z!) {
36 return (invalid_list($ctx, $1) ||
37 serve_git($cgi, $ctx->{git}, $path));
39 elsif ($method !~ /\AGET|HEAD\z/) {
40 return r(405, 'Method Not Allowed');
43 # top-level indices and feeds
44 if ($path_info eq '/') {
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);
53 } elsif ($path_info =~ m!$LISTNAME_RE/
54 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
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);
60 # in case people leave off the trailing slash:
61 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
62 my ($listname, $mid, $suffix) = ($1, $2, $3);
63 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
64 r301($ctx, $listname, $mid, $suffix);
66 # convenience redirects order matters
67 } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
71 legacy_redirects($ctx, $path_info);
75 # for CoW-friendliness, MOOOOO!
77 require PublicInbox::Feed;
78 require PublicInbox::View;
79 require PublicInbox::Thread;
85 require PublicInbox::Search;
86 require PublicInbox::SearchView;
87 require PublicInbox::Mbox;
88 require IO::Compress::Gzip;
92 # private functions below
96 if ($ctx && $ctx->{mid}) {
97 require PublicInbox::ExtMsg;
99 return PublicInbox::ExtMsg::ext_msg($ctx);
104 # simple response for errors
105 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
107 # returns undef if valid, array ref response if invalid
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;
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);
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'));
138 # /$LISTNAME/new.atom -> Atom feed, includes replies
141 require PublicInbox::Feed;
142 PublicInbox::Feed::generate($ctx);
145 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
148 require PublicInbox::Feed;
149 my $srch = searcher($ctx);
151 if (defined $ctx->{cgi}->param('q')) {
152 require PublicInbox::SearchView;
153 PublicInbox::SearchView::sres_top_html($ctx);
155 PublicInbox::Feed::generate_html_index($ctx);
159 # just returns a string ref for the blob in the current ctx
162 require PublicInbox::MID;
163 my $path = PublicInbox::MID::mid2path($ctx->{mid});
164 $ctx->{git}->cat_file("HEAD:$path");
167 # /$LISTNAME/$MESSAGE_ID/raw -> raw mbox
170 my $x = mid2blob($ctx) or return r404($ctx);
171 require PublicInbox::Mbox;
172 PublicInbox::Mbox::emit1($ctx, $x);
175 # /$LISTNAME/$MESSAGE_ID/ -> HTML content (short quotes)
178 my $x = mid2blob($ctx) or return r404($ctx);
180 require PublicInbox::View;
181 my $foot = footer($ctx);
183 my $mime = Email::MIME->new($x);
185 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
186 [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
189 # /$LISTNAME/$MESSAGE_ID/f/ -> HTML content (fullquotes)
192 my $x = mid2blob($ctx) or return r404($ctx);
194 require PublicInbox::View;
195 my $foot = footer($ctx);
197 my $mime = Email::MIME->new($x);
199 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
200 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
203 # /$LISTNAME/$MESSAGE_ID/t/
205 my ($ctx, $flat) = @_;
206 my $srch = searcher($ctx) or return need_search($ctx);
207 require PublicInbox::View;
208 my $foot = footer($ctx);
209 $ctx->{flat} = $flat;
210 PublicInbox::View::thread_html($ctx, $foot, $srch);
216 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
220 my ($ctx, $key) = @_;
221 my $val = $ctx->{$key};
222 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
228 return '' unless $ctx;
229 my $git_dir = ctx_get($ctx, 'git_dir');
231 # favor user-supplied footer
232 my $footer = try_cat("$git_dir/public-inbox/footer.html");
233 if (defined $footer) {
235 $ctx->{footer} = $footer;
239 # auto-generate a footer
240 my $listname = ctx_get($ctx, 'listname');
241 my $desc = try_cat("$git_dir/description");
242 $desc = '$GIT_DIR/description missing' unless defined $desc;
245 my $urls = try_cat("$git_dir/cloneurl");
246 my @urls = split(/\r?\n/, $urls || '');
247 my %seen = map { $_ => 1 } @urls;
248 my $cgi = $ctx->{cgi};
249 my $http = (ref($cgi) eq 'CGI') ? $cgi->url(-base) . "/$listname" :
250 $cgi->base->as_string . $listname;
251 $seen{$http} or unshift @urls, $http;
252 if (scalar(@urls) == 1) {
253 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
254 '">ssoma</a>: ' . $urls[0];
256 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
257 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
260 my $addr = $pi_config->get($listname, 'address');
261 if (ref($addr) eq 'ARRAY') {
262 $addr = $addr->[0]; # first address is primary
265 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
267 $ctx->{footer} = join("\n",
269 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
270 'anybody may post in plain-text (not HTML):',
276 # search support is optional, returns undef if Xapian is not installed
277 # or not configured for the given GIT_DIR
281 require PublicInbox::Search;
282 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
289 <html><head><title>Search not available for this
290 public-inbox</title><body><pre>Search is not available for this public-inbox
291 <a href="../">Return to index</a></pre></body></html>
293 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
296 # /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
297 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
298 # note: I'm not a big fan of other compression formats since they're
299 # significantly more expensive on CPU than gzip and less-widely available,
300 # especially on older systems. Stick to zlib since that's what git uses.
301 sub get_thread_mbox {
302 my ($ctx, $sfx) = @_;
303 my $srch = searcher($ctx) or return need_search($ctx);
304 require PublicInbox::Mbox;
305 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
309 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
310 sub get_thread_atom {
312 searcher($ctx) or return need_search($ctx);
313 $ctx->{self_url} = self_url($ctx->{cgi});
314 require PublicInbox::Feed;
315 PublicInbox::Feed::generate_thread_atom($ctx);
318 sub legacy_redirects {
319 my ($ctx, $path_info) = @_;
321 # single-message pages
322 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
324 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
325 r301($ctx, $1, $2, 'raw');
327 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
328 r301($ctx, $1, $2, 'f/');
331 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
332 r301($ctx, $1, $2, 't/#u');
334 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
335 r301($ctx, $1, $2, "t.mbox$3");
337 # even older legacy redirects
338 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
341 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
342 r301($ctx, $1, $2, 't/#u');
344 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
345 r301($ctx, $1, $2, 'f/');
347 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
348 r301($ctx, $1, $2, 'raw');
350 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
351 r301($ctx, $1, $2, "t$3");
353 # legacy convenience redirects, order still matters
354 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
356 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
357 r301($ctx, $1, $2, 't/#u');
358 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
359 r301($ctx, $1, $2, 'f/');
361 # some Message-IDs have slashes in them and the HTTP server
362 # may try to be clever and unescape them :<
363 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
364 msg_page($ctx, $1, $2, $3);
366 # in case people leave off the trailing slash:
367 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
368 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
375 my ($ctx, $listname, $mid, $suffix) = @_;
376 my $cgi = $ctx->{cgi};
379 if (ref($cgi) eq 'CGI') {
380 $url = $cgi->url(-base) . '/';
381 $qs = $cgi->query_string;
382 } else { # Plack::Request
383 $url = $cgi->base->as_string;
384 $qs = $cgi->env->{QUERY_STRING};
387 $url .= $listname . '/';
388 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
389 $url .= $suffix if (defined $suffix);
390 $url .= "?$qs" if $qs ne '';
393 [ Location => $url, 'Content-Type' => 'text/plain' ],
394 [ "Redirecting to $url\n" ] ]
398 my ($ctx, $list, $mid, $e) = @_;
399 unless (invalid_list_mid($ctx, $list, $mid)) {
400 '' eq $e and return get_mid_html($ctx);
401 't/' eq $e and return get_thread($ctx);
402 't.atom' eq $e and return get_thread_atom($ctx);
403 't.mbox' eq $e and return get_thread_mbox($ctx);
404 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
405 'T/' eq $e and return get_thread($ctx, 1);
406 'raw' eq $e and return get_mid_txt($ctx);
407 'f/' eq $e and return get_full_html($ctx);
413 my ($cgi, $git, $path) = @_;
414 PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);