]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwStream.pm
wwwstream: improve documentation and variable naming
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
index be6ce2e6bc9f2b8efb60314662c97714710e0362..01f7b31ba186001723d21dcbe2d3291e5f4383bc 100644 (file)
@@ -7,11 +7,17 @@ use strict;
 use warnings;
 use PublicInbox::Hval qw(ascii_html);
 use URI;
-use constant PI_URL => 'https://public-inbox.org/';
+our $TOR_URL = 'https://www.torproject.org/';
+our $TOR2WEB_URL = 'https://www.tor2web.org/';
+our $CODE_URL = 'https://public-inbox.org/';
+our $PROJECT = 'public-inbox';
+
+# noop for HTTP.pm (and any other PSGI servers)
+sub close {}
 
 sub new {
        my ($class, $ctx, $cb) = @_;
-       bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
+       bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
 }
 
 sub response {
@@ -27,13 +33,16 @@ sub _html_top ($) {
        my $desc = ascii_html($obj->description);
        my $title = $ctx->{-title_html} || $desc;
        my $upfx = $ctx->{-upfx} || '';
+       my $help = $upfx.'_/text/help';
        my $atom = $ctx->{-atom} || $upfx.'new.atom';
        my $tip = $ctx->{-html_tip} || '';
-       my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
+       my $top = "<b>$desc</b>";
+       my $links = "<a\nhref=\"$help\">help</a> / ".
+                       "<a\nhref=\"$atom\">Atom feed</a>";
        if ($obj->search) {
                my $q_val = $ctx->{-q_value_html};
                if (defined $q_val && $q_val ne '') {
-                       $q_val = qq(\nvalue="$q_val" );
+                       $q_val = qq(\nvalue="$q_val");
                } else {
                        $q_val = '';
                }
@@ -41,12 +50,13 @@ sub _html_top ($) {
                my $extra = $ctx->{-extra_form_html} || '';
                my $action = $upfx eq '' ? './' : $upfx;
                $top = qq{<form\naction="$action"><pre>$top} .
-                         qq{ <input\nname=q\ntype=text$q_val/>} .
+                         qq{\n<input\nname=q\ntype=text$q_val />} .
                          $extra .
                          qq{<input\ntype=submit\nvalue=search />} .
+                         ' ' . $links .
                          q{</pre></form>}
        } else {
-               $top = '<pre>' . $top . '</pre>';
+               $top = '<pre>' . $top . "\n" . $links . '</pre>';
        }
        "<html><head><title>$title</title>" .
                "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
@@ -57,7 +67,7 @@ sub _html_top ($) {
 
 sub _html_end {
        my ($self) = @_;
-       my $urls = 'Archives are clone-able:';
+       my $urls = 'Archives are clonable:';
        my $ctx = $self->{ctx};
        my $obj = $ctx->{-inbox};
        my $desc = ascii_html($obj->description);
@@ -77,28 +87,43 @@ sub _html_end {
                $urls .= "\n" .
                        join("\n", map { "\tgit clone --mirror $_" } @urls);
        }
-       my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
-       '<pre>'.join("\n",
-               '- ' . $desc,
+
+       my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
+       if (@nntp) {
+               $urls .= "\n\n";
+               $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
+               $urls .= ' available over NNTP:';
+               $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
+       }
+       if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
+               $urls .= "\n note: .onion URLs require Tor: ";
+               $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
+               if ($TOR2WEB_URL) {
+                       $urls .= "\n       or Tor2web: ";
+                       $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
+               }
+       }
+       my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
+       '<hr><pre>'.join("\n\n",
+               $desc,
                $urls,
-               'Archived served using code from public-inbox:',
-               qq(\tgit clone <a\nhref="$url">$url</a> public-inbox),
+               'AGPL code for this site: '.
+               qq(git clone <a\nhref="$url">$url</a> $PROJECT)
        ).'</pre></body></html>';
 }
 
+# callback for HTTP.pm (and any other PSGI servers)
 sub getline {
        my ($self) = @_;
        my $nr = $self->{nr}++;
 
        return _html_top($self) if $nr == 0;
 
-       if (my $mid = $self->{cb}) { # middle
-               $mid = $mid->($nr, $self->{ctx}) and return $mid;
+       if (my $middle = $self->{cb}) {
+               $middle = $middle->($nr, $self->{ctx}) and return $middle;
        }
 
        delete $self->{cb} ? _html_end($self) : undef;
 }
 
-sub close {}
-
 1;