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;
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 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
21 our $MID_RE = qr!([^/]+)!;
22 our $END_RE = qr!(f/|T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
26 my ($cgi, $method) = @_;
27 $pi_config ||= PublicInbox::Config->new;
28 my $ctx = { cgi => $cgi, pi_config => $pi_config };
29 if ($method !~ /\AGET|HEAD\z/) {
30 return r(405, 'Method Not Allowed');
32 my $path_info = $cgi->path_info;
34 # top-level indices and feeds
35 if ($path_info eq '/') {
37 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
38 invalid_list($ctx, $1) || r301($ctx, $1);
39 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
40 invalid_list($ctx, $1) || get_index($ctx);
41 } elsif ($path_info =~ m!$LISTNAME_RE/(?:atom\.xml|new\.atom)\z!o) {
42 invalid_list($ctx, $1) || get_atom($ctx);
44 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
45 msg_page($ctx, $1, $2, $3);
47 # in case people leave off the trailing slash:
48 } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
49 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
51 # convenience redirects order matters
52 } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
56 legacy_redirects($ctx, $path_info);
60 # for CoW-friendliness, MOOOOO!
62 require PublicInbox::Feed;
63 require PublicInbox::View;
64 require PublicInbox::Thread;
65 require PublicInbox::GitCatFile;
71 require PublicInbox::Search;
72 require PublicInbox::SearchView;
73 require PublicInbox::Mbox;
74 require IO::Compress::Gzip;
78 # private functions below
82 if ($ctx && $ctx->{mid}) {
83 require PublicInbox::ExtMsg;
85 return PublicInbox::ExtMsg::ext_msg($ctx);
90 # simple response for errors
91 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
93 # returns undef if valid, array ref response if invalid
95 my ($ctx, $listname) = @_;
96 my $git_dir = $pi_config->get($listname, "mainrepo");
97 if (defined $git_dir) {
98 $ctx->{git_dir} = $git_dir;
99 $ctx->{listname} = $listname;
105 # returns undef if valid, array ref response if invalid
106 sub invalid_list_mid {
107 my ($ctx, $listname, $mid) = @_;
108 my $ret = invalid_list($ctx, $listname, $mid);
111 $ctx->{mid} = $mid = uri_unescape($mid);
112 if ($mid =~ /\A[a-f0-9]{40}\z/) {
113 if ($mid = mid2blob($ctx)) {
114 require Email::Simple;
115 use PublicInbox::MID qw/mid_clean/;
116 $mid = Email::Simple->new($mid);
117 $ctx->{mid} = mid_clean($mid->header('Message-ID'));
123 # /$LISTNAME/new.atom -> Atom feed, includes replies
126 require PublicInbox::Feed;
127 PublicInbox::Feed::generate($ctx);
130 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
133 require PublicInbox::Feed;
134 my $srch = searcher($ctx);
136 if (defined $ctx->{cgi}->param('q')) {
137 require PublicInbox::SearchView;
138 PublicInbox::SearchView::sres_top_html($ctx);
140 PublicInbox::Feed::generate_html_index($ctx);
144 # just returns a string ref for the blob in the current ctx
147 require PublicInbox::MID;
148 my $path = PublicInbox::MID::mid2path($ctx->{mid});
149 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
150 qw(cat-file blob), "HEAD:$path");
151 my $pid = open my $fh, '-|';
152 defined $pid or die "fork failed: $!\n";
154 open STDERR, '>', '/dev/null'; # ignore errors
155 exec @cmd or die "exec failed: $!\n";
157 my $blob = eval { local $/; <$fh> };
159 $? == 0 ? \$blob : undef;
163 # /$LISTNAME/$MESSAGE_ID/raw -> raw mbox
166 my $x = mid2blob($ctx) or return r404($ctx);
167 require PublicInbox::Mbox;
168 PublicInbox::Mbox::emit1($ctx, $x);
171 # /$LISTNAME/$MESSAGE_ID/ -> HTML content (short quotes)
174 my $x = mid2blob($ctx) or return r404($ctx);
176 require PublicInbox::View;
177 my $foot = footer($ctx);
179 my $mime = Email::MIME->new($x);
181 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
182 [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
185 # /$LISTNAME/$MESSAGE_ID/f/ -> HTML content (fullquotes)
188 my $x = mid2blob($ctx) or return r404($ctx);
190 require PublicInbox::View;
191 my $foot = footer($ctx);
193 my $mime = Email::MIME->new($x);
195 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
196 [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
199 # /$LISTNAME/$MESSAGE_ID/t/
201 my ($ctx, $flat) = @_;
202 my $srch = searcher($ctx) or return need_search($ctx);
203 require PublicInbox::View;
204 my $foot = footer($ctx);
205 $ctx->{flat} = $flat;
206 PublicInbox::View::thread_html($ctx, $foot, $srch);
211 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
215 my ($ctx, $key) = @_;
216 my $val = $ctx->{$key};
217 (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
224 if (open(my $fh, '<', $path)) {
234 return '' unless $ctx;
235 my $git_dir = ctx_get($ctx, 'git_dir');
237 # favor user-supplied footer
238 my $footer = try_cat("$git_dir/public-inbox/footer.html");
239 if (defined $footer) {
241 $ctx->{footer} = $footer;
245 # auto-generate a footer
246 my $listname = ctx_get($ctx, 'listname');
247 my $desc = try_cat("$git_dir/description");
248 $desc = '$GIT_DIR/description missing' unless defined $desc;
251 my $urls = try_cat("$git_dir/cloneurl");
252 my @urls = split(/\r?\n/, $urls || '');
253 my $nurls = scalar @urls;
255 $urls = '($GIT_DIR/cloneurl missing)';
256 } elsif ($nurls == 1) {
257 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
258 '">ssoma</a>: ' . $urls[0];
260 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
261 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
264 my $addr = $pi_config->get($listname, 'address');
265 if (ref($addr) eq 'ARRAY') {
266 $addr = $addr->[0]; # first address is primary
269 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
271 $ctx->{footer} = join("\n",
273 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
274 'anybody may post in plain-text (not HTML):',
280 # search support is optional, returns undef if Xapian is not installed
281 # or not configured for the given GIT_DIR
285 require PublicInbox::Search;
286 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
293 <html><head><title>Search not available for this
294 public-inbox</title><body><pre>Search is not available for this public-inbox
295 <a href="../">Return to index</a></pre></body></html>
297 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
300 # /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
301 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
302 # note: I'm not a big fan of other compression formats since they're
303 # significantly more expensive on CPU than gzip and less-widely available,
304 # especially on older systems. Stick to zlib since that's what git uses.
305 sub get_thread_mbox {
306 my ($ctx, $sfx) = @_;
307 my $srch = searcher($ctx) or return need_search($ctx);
308 require PublicInbox::Mbox;
309 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
313 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
314 sub get_thread_atom {
316 searcher($ctx) or return need_search($ctx);
317 $ctx->{self_url} = self_url($ctx->{cgi});
318 require PublicInbox::Feed;
319 PublicInbox::Feed::generate_thread_atom($ctx);
322 sub legacy_redirects {
323 my ($ctx, $path_info) = @_;
325 # single-message pages
326 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
328 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
329 r301($ctx, $1, $2, 'raw');
331 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
332 r301($ctx, $1, $2, 'f/');
335 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
336 r301($ctx, $1, $2, 't/#u');
338 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
339 r301($ctx, $1, $2, "t.mbox$3");
341 # even older legacy redirects
342 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
345 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
346 r301($ctx, $1, $2, 't/#u');
348 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
349 r301($ctx, $1, $2, 'f/');
351 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
352 r301($ctx, $1, $2, 'raw');
354 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
355 r301($ctx, $1, $2, "t$3");
357 # legacy convenience redirects, order still matters
358 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
360 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
361 r301($ctx, $1, $2, 't/#u');
362 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
363 r301($ctx, $1, $2, 'f/');
365 # some Message-IDs have slashes in them and the HTTP server
366 # may try to be clever and unescape them :<
367 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
368 msg_page($ctx, $1, $2, $3);
370 # in case people leave off the trailing slash:
371 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
372 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
379 my ($ctx, $listname, $mid, $suffix) = @_;
380 my $cgi = $ctx->{cgi};
382 if (ref($cgi) eq 'CGI') {
383 $url = $cgi->url(-base) . '/';
385 $url = $cgi->base->as_string;
388 $url .= $listname . '/';
389 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
390 $url .= $suffix if (defined $suffix);
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);