]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
www: reduce unused arguments in internal API
[public-inbox.git] / lib / PublicInbox / WWW.pm
index bbd438a2d939bc1d42d9fbbcc83b1634ec74d0e8..d1ee2ff07340a8cfac4b1daafaf56898424af805 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
@@ -23,7 +23,7 @@ BEGIN {
 
 sub run {
        my ($cgi, $method) = @_;
-       my %ctx;
+       my %ctx = (cgi => $cgi, pi_config => $pi_config);
        if ($method !~ /\AGET|HEAD\z/) {
                return r(405, 'Method Not Allowed');
        }
@@ -33,36 +33,38 @@ 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);
+               invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx);
        } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
-               invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
+               invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%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);
+               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);
+               invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx);
 
-       # subject_path display
-       } elsif ($path_info =~ m!$LISTNAME_RE/s/(\S+)\.html\z!o) {
-               my $sp = $2;
-               invalid_list(\%ctx, $1) || get_subject_path(\%ctx, $cgi, $sp);
+       } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.mbox(\.gz)?\z!o) {
+               my $sfx = $3;
+               invalid_list_mid(\%ctx, $1, $2) ||
+                       get_thread_mbox(\%ctx, $sfx);
+
+       } elsif ($path_info =~ m!$LISTNAME_RE/f/\S+\.txt\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || redirect_mid_txt(\%ctx);
 
        # convenience redirects, order matters
        } 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);
+               invalid_list_mid(\%ctx, $1, $3) || redirect_mid(\%ctx, $2);
 
        } else {
                r404();
@@ -74,10 +76,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
@@ -109,33 +117,18 @@ 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
@@ -158,16 +151,17 @@ 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 ($ctx) = @_;
        my $x = mid2blob($ctx);
        return r404() unless $x;
 
@@ -176,48 +170,32 @@ sub get_mid_html {
        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 ($ctx) = @_;
        my $x = mid2blob($ctx);
        return r404() unless $x;
        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 $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 ] ];
-}
-
-# /$LISTNAME/s/$SUBJECT_PATH.html
-sub get_subject_path {
-       my ($ctx, $cgi, $sp) = @_;
-       $ctx->{subject_path} = $sp;
+       my ($ctx) = @_;
        my $srch = searcher($ctx) or return need_search($ctx);
        require PublicInbox::View;
        my $foot = footer($ctx);
-       my $body = PublicInbox::View->subject_path_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 {
@@ -226,13 +204,13 @@ 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) = @_;
+       my $url = self_url($ctx->{cgi});
        my $anchor = '';
        if (lc($pfx) eq 't') {
                $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
@@ -240,6 +218,15 @@ sub redirect_mid {
        do_redirect($url . ".html$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 {
        my ($url) = @_;
        [ 301,
@@ -275,6 +262,7 @@ sub footer {
        my $footer = try_cat("$git_dir/public-inbox/footer.html");
        if (defined $footer) {
                chomp $footer;
+               $ctx->{footer} = $footer;
                return $footer;
        }
 
@@ -303,8 +291,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):',
@@ -319,7 +307,7 @@ sub searcher {
        my ($ctx) = @_;
        eval {
                require PublicInbox::Search;
-               PublicInbox::Search->new($ctx->{git_dir});
+               $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
        };
 }
 
@@ -339,4 +327,16 @@ sub msg_pfx {
        "../f/$href.html";
 }
 
+# /$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);
+}
+
 1;