]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
remove XML::Atom::SimpleFeed dependency
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 52e51c435cfbaf2a4cb6abbff0cde3d58af7dd28..30a7a436df3d12ecce2bb96c980b058e38132975 100644 (file)
@@ -2,7 +2,7 @@
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
 #
 # We focus on the lowest common denominators here:
-# - targeted at text-only console browsers (lynx, w3m, etc..)
+# - targeted at text-only console browsers (w3m, links, etc..)
 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
 # - No JavaScript, graphics or icons allowed.
 # - Must not rely on static content
@@ -35,9 +35,9 @@ sub run {
        } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
                invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
        } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
-               invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
+               invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi);
        } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
-               invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
+               invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi);
 
        # single-message pages
        } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
@@ -53,8 +53,16 @@ sub run {
        } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
                invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx, $cgi);
 
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.mbox\.gz!o) {
+               my $sfx = $3;
+               invalid_list_mid(\%ctx, $1, $2) || get_thread_mbox(\%ctx, $cgi);
+
+       } elsif ($path_info =~ m!$LISTNAME_RE/f/\S+\.txt\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) ||
+                       redirect_mid_txt(\%ctx, $cgi);
+
        # convenience redirects, order matters
-       } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t)/(\S+)\z!o) {
+       } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t|s)/(\S+)\z!o) {
                my $pfx = $2;
                invalid_list_mid(\%ctx, $1, $3) ||
                        redirect_mid(\%ctx, $cgi, $2);
@@ -69,10 +77,16 @@ sub preload {
        require PublicInbox::Feed;
        require PublicInbox::View;
        require PublicInbox::Thread;
+       require PublicInbox::GitCatFile;
        require Email::MIME;
        require Digest::SHA;
        require POSIX;
-       require XML::Atom::SimpleFeed;
+
+       eval {
+               require PublicInbox::Search;
+               require PublicInbox::Mbox;
+               require IO::Compress::Gzip;
+       };
 }
 
 # private functions below
@@ -104,51 +118,31 @@ sub invalid_list_mid {
 
 # /$LISTNAME/atom.xml                       -> Atom feed, includes replies
 sub get_atom {
-       my ($ctx, $cgi, $top) = @_;
+       my ($ctx, $cgi) = @_;
+       $ctx->{pi_config} = $pi_config;
+       $ctx->{cgi} = $cgi;
        require PublicInbox::Feed;
-       [ 200, [ 'Content-Type' => 'application/xml' ],
-         [ PublicInbox::Feed->generate({
-                       git_dir => $ctx->{git_dir},
-                       listname => $ctx->{listname},
-                       pi_config => $pi_config,
-                       cgi => $cgi,
-                       top => $top,
-               }) ]
-       ];
+       PublicInbox::Feed::generate($ctx);
 }
 
 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
 sub get_index {
-       my ($ctx, $cgi, $top) = @_;
+       my ($ctx, $cgi) = @_;
        require PublicInbox::Feed;
-       [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ PublicInbox::Feed->generate_html_index({
-                       git_dir => $ctx->{git_dir},
-                       listname => $ctx->{listname},
-                       pi_config => $pi_config,
-                       cgi => $cgi,
-                       footer => footer($ctx),
-                       top => $top,
-               }) ]
-       ];
+       my $srch = searcher($ctx);
+       $ctx->{pi_config} = $pi_config;
+       $ctx->{cgi} = $cgi;
+       footer($ctx);
+       PublicInbox::Feed::generate_html_index($ctx);
 }
 
 # just returns a string ref for the blob in the current ctx
 sub mid2blob {
        my ($ctx) = @_;
-       my $hex = $ctx->{mid};
-       my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-
-       unless (defined $x38) {
-               # compatibility with old links
-               require Digest::SHA;
-               $hex = Digest::SHA::sha1_hex($hex);
-               ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
-               defined $x38 or die "BUG: not a SHA-1 hex: $hex";
-       }
-
+       require PublicInbox::MID;
+       my $path = PublicInbox::MID::mid2path($ctx->{mid});
        my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
-                       qw(cat-file blob), "HEAD:$x2/$x38");
+                       qw(cat-file blob), "HEAD:$path");
        my $cmd = join(' ', @cmd);
        my $pid = open my $fh, '-|';
        defined $pid or die "fork failed: $!\n";
@@ -205,10 +199,7 @@ sub get_thread {
        my $srch = searcher($ctx) or return need_search($ctx);
        require PublicInbox::View;
        my $foot = footer($ctx);
-       my $body = PublicInbox::View->thread_html($ctx, $foot, $srch) or
-               return r404();
-       [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ $body ] ];
+       PublicInbox::View::thread_html($ctx, $foot, $srch);
 }
 
 sub self_url {
@@ -231,6 +222,15 @@ sub redirect_mid {
        do_redirect($url . ".html$anchor");
 }
 
+# only hit when somebody tries to guess URLs manually:
+sub redirect_mid_txt {
+       my ($ctx, $cgi, $pfx) = @_;
+       my $listname = $ctx->{listname};
+       my $url = self_url($cgi);
+       $url =~ s!/$listname/f/(\S+\.txt)\z!/$listname/m/$1!;
+       do_redirect($url);
+}
+
 sub do_redirect {
        my ($url) = @_;
        [ 301,
@@ -266,6 +266,7 @@ sub footer {
        my $footer = try_cat("$git_dir/public-inbox/footer.html");
        if (defined $footer) {
                chomp $footer;
+               $ctx->{footer} = $footer;
                return $footer;
        }
 
@@ -294,8 +295,8 @@ sub footer {
        }
 
        $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
-       $desc =  $desc;
-       join("\n",
+
+       $ctx->{footer} = join("\n",
                '- ' . $desc,
                "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
                        'anybody may post in plain-text (not HTML):',
@@ -310,7 +311,7 @@ sub searcher {
        my ($ctx) = @_;
        eval {
                require PublicInbox::Search;
-               PublicInbox::Search->new($ctx->{git_dir});
+               $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
        };
 }
 
@@ -330,4 +331,15 @@ sub msg_pfx {
        "../f/$href.html";
 }
 
+# /$LISTNAME/t/$MESSAGE_ID.mbox.gz        -> search results 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, $cgi) = @_;
+       my $srch = searcher($ctx) or return need_search($ctx);
+       require PublicInbox::Mbox;
+       PublicInbox::Mbox::thread_mbox($ctx, $srch);
+}
+
 1;