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 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";
223 return '' unless $ctx;
224 my $git_dir = ctx_get($ctx, 'git_dir');
226 # favor user-supplied footer
227 my $footer = try_cat("$git_dir/public-inbox/footer.html");
228 if (defined $footer) {
230 $ctx->{footer} = $footer;
234 # auto-generate a footer
235 my $listname = ctx_get($ctx, 'listname');
236 my $desc = try_cat("$git_dir/description");
237 $desc = '$GIT_DIR/description missing' unless defined $desc;
240 my $urls = try_cat("$git_dir/cloneurl");
241 my @urls = split(/\r?\n/, $urls || '');
242 my $nurls = scalar @urls;
244 $urls = '($GIT_DIR/cloneurl missing)';
245 } elsif ($nurls == 1) {
246 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
247 '">ssoma</a>: ' . $urls[0];
249 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
250 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
253 my $addr = $pi_config->get($listname, 'address');
254 if (ref($addr) eq 'ARRAY') {
255 $addr = $addr->[0]; # first address is primary
258 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
260 $ctx->{footer} = join("\n",
262 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
263 'anybody may post in plain-text (not HTML):',
269 # search support is optional, returns undef if Xapian is not installed
270 # or not configured for the given GIT_DIR
274 require PublicInbox::Search;
275 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
282 <html><head><title>Search not available for this
283 public-inbox</title><body><pre>Search is not available for this public-inbox
284 <a href="../">Return to index</a></pre></body></html>
286 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
289 # /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
290 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
291 # note: I'm not a big fan of other compression formats since they're
292 # significantly more expensive on CPU than gzip and less-widely available,
293 # especially on older systems. Stick to zlib since that's what git uses.
294 sub get_thread_mbox {
295 my ($ctx, $sfx) = @_;
296 my $srch = searcher($ctx) or return need_search($ctx);
297 require PublicInbox::Mbox;
298 PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
302 # /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
303 sub get_thread_atom {
305 searcher($ctx) or return need_search($ctx);
306 $ctx->{self_url} = self_url($ctx->{cgi});
307 require PublicInbox::Feed;
308 PublicInbox::Feed::generate_thread_atom($ctx);
311 sub legacy_redirects {
312 my ($ctx, $path_info) = @_;
314 # single-message pages
315 if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
317 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
318 r301($ctx, $1, $2, 'raw');
320 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
321 r301($ctx, $1, $2, 'f/');
324 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
325 r301($ctx, $1, $2, 't/#u');
327 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
328 r301($ctx, $1, $2, "t.mbox$3");
330 # even older legacy redirects
331 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
334 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
335 r301($ctx, $1, $2, 't/#u');
337 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
338 r301($ctx, $1, $2, 'f/');
340 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
341 r301($ctx, $1, $2, 'raw');
343 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
344 r301($ctx, $1, $2, "t$3");
346 # legacy convenience redirects, order still matters
347 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
349 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
350 r301($ctx, $1, $2, 't/#u');
351 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
352 r301($ctx, $1, $2, 'f/');
354 # some Message-IDs have slashes in them and the HTTP server
355 # may try to be clever and unescape them :<
356 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
357 msg_page($ctx, $1, $2, $3);
359 # in case people leave off the trailing slash:
360 } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
361 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
368 my ($ctx, $listname, $mid, $suffix) = @_;
369 my $cgi = $ctx->{cgi};
371 if (ref($cgi) eq 'CGI') {
372 $url = $cgi->url(-base) . '/';
374 $url = $cgi->base->as_string;
377 $url .= $listname . '/';
378 $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
379 $url .= $suffix if (defined $suffix);
382 [ Location => $url, 'Content-Type' => 'text/plain' ],
383 [ "Redirecting to $url\n" ] ]
387 my ($ctx, $list, $mid, $e) = @_;
388 unless (invalid_list_mid($ctx, $list, $mid)) {
389 '' eq $e and return get_mid_html($ctx);
390 't/' eq $e and return get_thread($ctx);
391 't.atom' eq $e and return get_thread_atom($ctx);
392 't.mbox' eq $e and return get_thread_mbox($ctx);
393 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
394 'T/' eq $e and return get_thread($ctx, 1);
395 'raw' eq $e and return get_mid_txt($ctx);
396 'f/' eq $e and return get_full_html($ctx);