+sub footer {
+ my ($ctx) = @_;
+ return '' unless $ctx;
+ my $git_dir = ctx_get($ctx, 'git_dir');
+
+ # favor user-supplied footer
+ my $footer = try_cat("$git_dir/public-inbox/footer.html");
+ if (defined $footer) {
+ chomp $footer;
+ $ctx->{footer} = $footer;
+ return $footer;
+ }
+
+ # auto-generate a footer
+ my $listname = ctx_get($ctx, 'listname');
+ my $desc = try_cat("$git_dir/description");
+ $desc = '$GIT_DIR/description missing' unless defined $desc;
+ chomp $desc;
+
+ my $urls = try_cat("$git_dir/cloneurl");
+ my @urls = split(/\r?\n/, $urls || '');
+ my %seen = map { $_ => 1 } @urls;
+ my $cgi = $ctx->{cgi};
+ my $http = $cgi->base->as_string . $listname;
+ $seen{$http} or unshift @urls, $http;
+ my $ssoma_url = PublicInbox::Hval::prurl($cgi->{env}, SSOMA_URL);
+ if (scalar(@urls) == 1) {
+ $urls = "URL for <a\nhref=\"" . $ssoma_url .
+ qq(">ssoma</a> or <b>git clone --mirror \$URL</b> :) .
+ $urls[0];
+ } else {
+ $urls = "URLs for <a\nhref=\"" . $ssoma_url .
+ qq(">ssoma</a> or <b>git clone --mirror \$URL</b>\n) .
+ join("\n", map { "\t$_" } @urls);
+ }
+
+ my $addr = $ctx->{pi_config}->get($listname, 'address');
+ if (ref($addr) eq 'ARRAY') {
+ $addr = $addr->[0]; # first address is primary
+ }
+
+ $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
+
+ $ctx->{footer} = join("\n",
+ '- ' . $desc,
+ "A <a\nhref=\"" .
+ PublicInbox::Hval::prurl($ctx->{cgi}->{env}, PI_URL) .
+ '">public-inbox</a>, ' .
+ 'anybody may post in plain-text (not HTML):',
+ $addr,
+ $urls
+ );
+}
+
+# search support is optional, returns undef if Xapian is not installed
+# or not configured for the given GIT_DIR
+sub searcher {
+ my ($ctx) = @_;
+ eval {
+ require PublicInbox::Search;
+ $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
+ };
+}
+
+sub need_search {
+ my ($ctx) = @_;
+ my $msg = <<EOF;
+<html><head><title>Search not available for this
+public-inbox</title><body><pre>Search is not available for this public-inbox
+<a href="../">Return to index</a></pre></body></html>
+EOF
+ [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
+}
+
+# /$LISTNAME/$MESSAGE_ID/t.mbox -> thread as mbox
+# /$LISTNAME/$MESSAGE_ID/t.mbox.gz -> thread as gzipped mbox
+# note: I'm not a big fan of other compression formats since they're
+# significantly more expensive on CPU than gzip and less-widely available,
+# especially on older systems. Stick to zlib since that's what git uses.
+sub get_thread_mbox {
+ my ($ctx, $sfx) = @_;
+ my $srch = searcher($ctx) or return need_search($ctx);
+ require PublicInbox::Mbox;
+ PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
+}
+
+
+# /$LISTNAME/$MESSAGE_ID/t.atom -> thread as Atom feed
+sub get_thread_atom {
+ my ($ctx) = @_;
+ searcher($ctx) or return need_search($ctx);
+ $ctx->{self_url} = $ctx->{cgi}->uri->as_string;
+ require PublicInbox::Feed;
+ PublicInbox::Feed::generate_thread_atom($ctx);
+}
+
+sub legacy_redirects {
+ my ($self, $ctx, $path_info) = @_;
+
+ # single-message pages
+ if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
+ r301($ctx, $1, $2);
+ } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
+ r301($ctx, $1, $2, 'raw');
+
+ } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
+ r301($ctx, $1, $2, 'f/');
+
+ # thread display
+ } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
+ r301($ctx, $1, $2, 't/#u');
+
+ } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
+ r301($ctx, $1, $2, "t.mbox$3");
+
+ # even older legacy redirects
+ } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
+ r301($ctx, $1, $2);
+
+ } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
+ r301($ctx, $1, $2, 't/#u');
+
+ } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
+ r301($ctx, $1, $2, 'f/');
+
+ } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
+ r301($ctx, $1, $2, 'raw');
+
+ } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
+ r301($ctx, $1, $2, "t$3");
+
+ # legacy convenience redirects, order still matters
+ } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
+ r301($ctx, $1, $2);
+ } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
+ r301($ctx, $1, $2, 't/#u');
+ } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
+ r301($ctx, $1, $2, 'f/');
+
+ # some Message-IDs have slashes in them and the HTTP server
+ # may try to be clever and unescape them :<
+ } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
+ msg_page($self, $ctx, $1, $2, $3);
+
+ # in case people leave off the trailing slash:
+ } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
+ r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
+ } else {
+ $self->news_www->call($ctx->{cgi}->{env});
+ }
+}
+
+sub r301 {
+ my ($ctx, $listname, $mid, $suffix) = @_;
+ my $cgi = $ctx->{cgi};
+ my $url;
+ my $qs = $cgi->env->{QUERY_STRING};
+ $url = $cgi->base->as_string . $listname . '/';
+ $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
+ $url .= $suffix if (defined $suffix);
+ $url .= "?$qs" if $qs ne '';
+