]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
config: hoist out common functions
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 6eebf628c8692761b7033628d29114ac9c880abb..5cd3bc6fdfb63caa7b067c53d5908db209347af8 100644 (file)
@@ -1,6 +1,8 @@
-# Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
+# Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
 #
+# Main web interface for mailing list archives
+#
 # We focus on the lowest common denominators here:
 # - targeted at text-only console browsers (w3m, links, etc..)
 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
@@ -11,7 +13,7 @@ package PublicInbox::WWW;
 use 5.008;
 use strict;
 use warnings;
-use PublicInbox::Config;
+use PublicInbox::Config qw(try_cat);
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
 use constant PI_URL => 'http://public-inbox.org/';
@@ -42,10 +44,9 @@ sub run {
        } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
                msg_page($ctx, $1, $2, $3);
 
-       # 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($ctx, $1, $2, $3);
+       # in case people leave off the trailing slash:
+       } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
+               r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
 
        # convenience redirects order matters
        } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
@@ -68,6 +69,7 @@ sub preload {
 
        eval {
                require PublicInbox::Search;
+               require PublicInbox::SearchView;
                require PublicInbox::Mbox;
                require IO::Compress::Gzip;
        };
@@ -79,6 +81,7 @@ sub r404 {
        my ($ctx) = @_;
        if ($ctx && $ctx->{mid}) {
                require PublicInbox::ExtMsg;
+               searcher($ctx);
                return PublicInbox::ExtMsg::ext_msg($ctx);
        }
        r(404, 'Not Found');
@@ -130,7 +133,12 @@ sub get_index {
        require PublicInbox::Feed;
        my $srch = searcher($ctx);
        footer($ctx);
-       PublicInbox::Feed::generate_html_index($ctx);
+       if (defined $ctx->{cgi}->param('q')) {
+               require PublicInbox::SearchView;
+               PublicInbox::SearchView::sres_top_html($ctx);
+       } else {
+               PublicInbox::Feed::generate_html_index($ctx);
+       }
 }
 
 # just returns a string ref for the blob in the current ctx
@@ -157,7 +165,7 @@ sub get_mid_txt {
        my ($ctx) = @_;
        my $x = mid2blob($ctx) or return r404($ctx);
        require PublicInbox::Mbox;
-       PublicInbox::Mbox::emit1($x);
+       PublicInbox::Mbox::emit1($ctx, $x);
 }
 
 # /$LISTNAME/$MESSAGE_ID/                   -> HTML content (short quotes)
@@ -210,17 +218,6 @@ sub ctx_get {
        $val;
 }
 
-sub try_cat {
-       my ($path) = @_;
-       my $rv;
-       if (open(my $fh, '<', $path)) {
-               local $/;
-               $rv = <$fh>;
-               close $fh;
-       }
-       $rv;
-}
-
 sub footer {
        my ($ctx) = @_;
        return '' unless $ctx;
@@ -354,6 +351,14 @@ sub legacy_redirects {
        } 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($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 {
                r404();
        }