]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
implement per-thread Atom feeds
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 52e51c435cfbaf2a4cb6abbff0cde3d58af7dd28..c99c25f827cc79db447044c6a5ca639e29add4d9 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
@@ -17,13 +17,11 @@ use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
 use constant PI_URL => 'http://public-inbox.org/';
 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
 our $pi_config;
-BEGIN {
-       $pi_config = PublicInbox::Config->new;
-}
 
 sub run {
        my ($cgi, $method) = @_;
-       my %ctx;
+       $pi_config ||= PublicInbox::Config->new;
+       my %ctx = (cgi => $cgi, pi_config => $pi_config);
        if ($method !~ /\AGET|HEAD\z/) {
                return r(405, 'Method Not Allowed');
        }
@@ -33,31 +31,53 @@ sub run {
        if ($path_info eq '/') {
                r404();
        } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
-               invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
+               invalid_list(\%ctx, $1) || redirect_list_index($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);
        } 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);
 
        # single-message pages
-       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
-               invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
-       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
-               invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
+       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx);
+       } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
 
        # full-message page
-       } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
-               invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
+       } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx);
 
        # thread display
-       } 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+)/\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
+
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!x) {
+               my $sfx = $3;
+               invalid_list_mid(\%ctx, $1, $2) ||
+                       get_thread_mbox(\%ctx, $sfx);
+
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/atom\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_thread_atom(\%ctx);
+
+       # legacy redirects
+       } elsif ($path_info =~ m!$LISTNAME_RE/(t|m|f)/(\S+)\.html\z!o) {
+               my $pfx = $2;
+               invalid_list_mid(\%ctx, $1, $3) ||
+                       redirect_mid(\%ctx, $pfx, qr/\.html\z/, '/');
+       } elsif ($path_info =~ m!$LISTNAME_RE/(m|f)/(\S+)\.txt\z!o) {
+               my $pfx = $2;
+               invalid_list_mid(\%ctx, $1, $3) ||
+                       redirect_mid(\%ctx, $pfx, qr/\.txt\z/, '/raw');
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
+               my $end = $3;
+               invalid_list_mid(\%ctx, $1, $2) ||
+                       redirect_mid(\%ctx, 't', $end, '/mbox.gz');
 
        # 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);
+                       redirect_mid(\%ctx, $pfx, qr/\z/, '/');
 
        } else {
                r404();
@@ -69,10 +89,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,52 +130,27 @@ sub invalid_list_mid {
 
 # /$LISTNAME/atom.xml                       -> Atom feed, includes replies
 sub get_atom {
-       my ($ctx, $cgi, $top) = @_;
+       my ($ctx) = @_;
        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) = @_;
        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);
+       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");
-       my $cmd = join(' ', @cmd);
+                       qw(cat-file blob), "HEAD:$path");
        my $pid = open my $fh, '-|';
        defined $pid or die "fork failed: $!\n";
        if ($pid == 0) {
@@ -162,53 +163,50 @@ sub mid2blob {
        }
 }
 
-# /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
+# /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw mbox
 sub get_mid_txt {
-       my ($ctx, $cgi) = @_;
-       my $x = mid2blob($ctx);
-       $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
+       my ($ctx) = @_;
+       my $x = mid2blob($ctx) or return r404();
+       require PublicInbox::Mbox;
+       PublicInbox::Mbox::emit1($x);
 }
 
 # /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
 sub get_mid_html {
-       my ($ctx, $cgi) = @_;
-       my $x = mid2blob($ctx);
-       return r404() unless $x;
+       my ($ctx) = @_;
+       my $x = mid2blob($ctx) or return r404();
 
        require PublicInbox::View;
        my $pfx = msg_pfx($ctx);
        my $foot = footer($ctx);
        require Email::MIME;
        my $mime = Email::MIME->new($x);
-       my $srch = searcher($ctx);
+       searcher($ctx);
        [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
+         [ PublicInbox::View::msg_html($ctx, $mime, $pfx, $foot) ] ];
 }
 
 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
 sub get_full_html {
-       my ($ctx, $cgi) = @_;
-       my $x = mid2blob($ctx);
-       return r404() unless $x;
+       my ($ctx) = @_;
+       my $x = mid2blob($ctx) or return r404();
+
        require PublicInbox::View;
        my $foot = footer($ctx);
        require Email::MIME;
        my $mime = Email::MIME->new($x);
-       my $srch = searcher($ctx);
+       searcher($ctx);
        [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
+         [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
 }
 
 # /$LISTNAME/t/$MESSAGE_ID.html
 sub get_thread {
-       my ($ctx, $cgi) = @_;
+       my ($ctx) = @_;
        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 {
@@ -217,18 +215,28 @@ sub self_url {
 }
 
 sub redirect_list_index {
-       my ($ctx, $cgi) = @_;
+       my ($cgi) = @_;
        do_redirect(self_url($cgi) . "/");
 }
 
 sub redirect_mid {
-       my ($ctx, $cgi, $pfx) = @_;
-       my $url = self_url($cgi);
+       my ($ctx, $pfx, $old, $sfx) = @_;
+       my $url = self_url($ctx->{cgi});
        my $anchor = '';
-       if (lc($pfx) eq 't') {
+       if (lc($pfx) eq 't' && $sfx eq '/') {
                $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
        }
-       do_redirect($url . ".html$anchor");
+       $url =~ s/$old/$sfx/;
+       do_redirect($url . $anchor);
+}
+
+# only hit when somebody tries to guess URLs manually:
+sub redirect_mid_txt {
+       my ($ctx, $pfx) = @_;
+       my $listname = $ctx->{listname};
+       my $url = self_url($ctx->{cgi});
+       $url =~ s!/$listname/f/(\S+\.txt)\z!/$listname/m/$1!;
+       do_redirect($url);
 }
 
 sub do_redirect {
@@ -242,7 +250,7 @@ sub do_redirect {
 sub ctx_get {
        my ($ctx, $key) = @_;
        my $val = $ctx->{$key};
-       (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
+       (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
        $val;
 }
 
@@ -266,6 +274,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 +303,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 +319,7 @@ sub searcher {
        my ($ctx) = @_;
        eval {
                require PublicInbox::Search;
-               PublicInbox::Search->new($ctx->{git_dir});
+               $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
        };
 }
 
@@ -327,7 +336,29 @@ EOF
 sub msg_pfx {
        my ($ctx) = @_;
        my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));
-       "../f/$href.html";
+       "../../f/$href/";
+}
+
+# /$LISTNAME/t/$MESSAGE_ID/mbox           -> thread as mbox
+# /$LISTNAME/t/$MESSAGE_ID/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/t/$MESSAGE_ID/atom                  -> thread as Atom feed
+sub get_thread_atom {
+       my ($ctx) = @_;
+       searcher($ctx) or return need_search($ctx);
+       $ctx->{self_url} = self_url($ctx->{cgi});
+       require PublicInbox::Feed;
+       PublicInbox::Feed::generate_thread_atom($ctx);
 }
 
 1;