]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwText.pm
www: mirror: fix rendering of NNTP URLs
[public-inbox.git] / lib / PublicInbox / WwwText.pm
index 858fc2f70b77cc3cefff1c48ddb076067c4022ff..2b4e69fe09745e4f3019b122989d363a5ebc467f 100644 (file)
@@ -8,6 +8,7 @@ use v5.10.1;
 use PublicInbox::Linkify;
 use PublicInbox::WwwStream;
 use PublicInbox::Hval qw(ascii_html prurl);
+use HTTP::Date qw(time2str);
 use URI::Escape qw(uri_escape_utf8);
 use PublicInbox::GzipFilter qw(gzf_maybe);
 our $QP_URL = 'https://xapian.org/docs/queryparser.html';
@@ -74,10 +75,10 @@ sub get_text {
 }
 
 sub _srch_prefix ($$) {
-       my ($srch, $txt) = @_;
+       my ($ibx, $txt) = @_;
        my $pad = 0;
        my $htxt = '';
-       my $help = $srch->help;
+       my $help = $ibx->isrch->help;
        my $i;
        for ($i = 0; $i < @$help; $i += 2) {
                my $pfx = $help->[$i];
@@ -88,10 +89,9 @@ sub _srch_prefix ($$) {
                $htxt .= "\f\n";
        }
        $pad += 2;
-       my $padding = ' ' x ($pad + 8);
+       my $padding = ' ' x ($pad + 4);
        $htxt =~ s/^/$padding/gms;
-       $htxt =~ s/^$padding(\S+)\0/"        $1".
-                               (' ' x ($pad - length($1)))/egms;
+       $htxt =~ s/^$padding(\S+)\0/"    $1".(' ' x ($pad - length($1)))/egms;
        $htxt =~ s/\f\n/\n/gs;
        $$txt .= $htxt;
        1;
@@ -112,7 +112,7 @@ Users of browsers such as dillo, Firefox, or some browser
 extensions may start by downloading the following sample CSS file
 to control the colors they see:
 
-       ${base_url}userContent.css
+  ${base_url}userContent.css
 
 CSS sample
 ----------
@@ -171,6 +171,8 @@ sub inbox_config ($$$) {
        my ($ctx, $hdr, $txt) = @_;
        my $ibx = $ctx->{ibx};
        push @$hdr, 'Content-Disposition', 'inline; filename=inbox.config';
+       my $t = eval { $ibx->mm->created_at };
+       push(@$hdr, 'Last-Modified', time2str($t)) if $t;
        my $name = dq_escape($ibx->{name});
        my $inboxdir = '/path/to/top-level-inbox';
        my $base_url = $ibx->base_url($ctx->{env});
@@ -210,7 +212,8 @@ EOF
                defined(my $v = $ibx->{$k}) or next;
                $$txt .= "\t$k = $v\n";
        }
-       $$txt .= "\tnntpmirror = $_\n" for (@{$ibx->nntp_url});
+       $$txt .= "\timapmirror = $_\n" for (@{$ibx->imap_url($ctx)});
+       $$txt .= "\tnntpmirror = $_\n" for (@{$ibx->nntp_url($ctx)});
        _coderepo_config($ctx, $txt);
        1;
 }
@@ -246,9 +249,13 @@ sub coderepos_raw ($$) {
        my $cfg = $ctx->{www}->{pi_cfg};
        my @ret;
        for my $cr_name (@$cr) {
-               $ret[0] //= <<EOF;
-code repositories for project(s) associated with this inbox:
+               $ret[0] //= do {
+                       my $thing = $ctx->{ibx}->can('cloneurl') ?
+                               'public inbox' : 'external index';
+                       <<EOF;
+Code repositories for project(s) associated with this $thing
 EOF
+               };
                my $urls = $cfg->get_all("coderepo.$cr_name.cgiturl");
                if ($urls) {
                        for (@$urls) {
@@ -265,6 +272,36 @@ EOF
        @ret; # may be empty, this sub is called as an arg for join()
 }
 
+sub _add_imap_nntp_urls ($$) {
+       my ($ctx, $txt) = @_;
+       $ctx->{ibx}->can('nntp_url') or return; # TODO extindex can have IMAP
+       my $urls = $ctx->{ibx}->imap_url($ctx);
+       if (@$urls) {
+               $$txt .= "\nIMAP subfolder(s) are available under:";
+               $$txt .= "\n  " . join("\n  ", @$urls);
+               $$txt .= <<EOM
+
+  # each subfolder (starting with `0') holds 50K messages at most
+EOM
+       }
+       $urls = $ctx->{ibx}->nntp_url($ctx);
+       if (@$urls) {
+               $$txt .= "\n";
+               $$txt .= @$urls == 1 ? 'Newsgroup' : 'Newsgroups are';
+               $$txt .= ' available over NNTP:';
+               $$txt .= "\n  " . join("\n  ", @$urls) . "\n";
+       }
+}
+
+sub _add_onion_note ($) {
+       my ($txt) = @_;
+       $$txt =~ m!\b[^:]+://\w+\.onion/!i and $$txt .= <<EOM
+
+note: .onion URLs require Tor: https://www.torproject.org/
+
+EOM
+}
+
 sub _mirror_help ($$) {
        my ($ctx, $txt) = @_;
        my $ibx = $ctx->{ibx};
@@ -276,7 +313,8 @@ sub _mirror_help ($$) {
        $top_url =~ s!/[^/]+\z!/!;
        $$txt .= "public-inbox mirroring instructions\n\n";
        if ($ibx->can('cloneurl')) { # PublicInbox::Inbox
-               $$txt .= "This inbox may be cloned and mirrored by anyone:\n";
+               $$txt .=
+                 "This public inbox may be cloned and mirrored by anyone:\n";
                my @urls;
                my $max = $ibx->max_git_epoch;
                # TODO: some of these URLs may be too long and we may need to
@@ -292,8 +330,10 @@ sub _mirror_help ($$) {
                        }
                        my $nr = scalar(@urls);
                        if ($nr > 1) {
-                               $$txt .= "\n\t";
-                               $$txt .= "# this inbox consists of $nr epochs:";
+                               chomp($$txt .= <<EOM);
+
+  # this inbox consists of $nr epochs: (no need to clone all of them)
+EOM
                                $urls[0] .= " # oldest";
                                $urls[-1] .= " # newest";
                        }
@@ -307,23 +347,25 @@ sub _mirror_help ($$) {
                        push @urls, $u;
                }
                $$txt .= "\n";
-               $$txt .= join('', map { "\tgit clone --mirror $_\n" } @urls);
-               if (my $addrs = $ibx->{address}) {
-                       $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
-                       my $v = defined $max ? '-V2' : '-V1';
-                       $$txt .= <<EOF;
-
-       # If you have public-inbox 1.1+ installed, you may
-       # initialize and index your mirror using the following commands:
-       public-inbox-init $v $ibx->{name} $dir/ $base_url \\
-               $addrs
-       public-inbox-index $dir
+               $$txt .= join('', map { "  git clone --mirror $_\n" } @urls);
+               my $addrs = $ibx->{address} // 'inbox@example.com';
+               my $ng = $ibx->{newsgroup} // '';
+               substr($ng, 0, 0, ' --ng ') if $ng;
+               $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
+               my $v = defined $max ? '-V2' : '-V1';
+               $$txt .= <<EOF;
+
+  # If you have public-inbox 1.1+ installed, you may
+  # initialize and index your mirror using the following commands:
+  public-inbox-init $v$ng \\
+    $ibx->{name} ./$dir $base_url \\
+    $addrs
+  public-inbox-index ./$dir
 EOF
-               }
        } else { # PublicInbox::ExtSearch
                $$txt .= <<EOM;
-This is an extindex which is an amalgamation of several public-inboxes.
-Each public-inbox needs to be mirrored individually.
+This is an external index which is an amalgamation of several public inboxes.
+Each public inbox needs to be mirrored individually.
 EOM
                my $v = $ctx->{www}->{pi_cfg}->{lc('publicInbox.wwwListing')};
                if (($v // '') =~ /\A(?:all|match=domain)\z/) {
@@ -337,26 +379,13 @@ EOM
 
 Example config snippet for mirrors: $cfg_link
 EOF
-       if ($ibx->can('nntp_url')) {
-               my $nntp = $ibx->nntp_url;
-               if (scalar @$nntp) {
-                       $$txt .= "\n";
-                       $$txt .= @$nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
-                       $$txt .= ' available over NNTP:';
-                       $$txt .= "\n\t" . join("\n\t", @$nntp) . "\n";
-               }
-       }
-       if ($$txt =~ m!\b[^:]+://\w+\.onion/!) {
-               $$txt .= <<EOM
-
-note: .onion URLs require Tor: https://www.torproject.org/
+       _add_imap_nntp_urls($ctx, $txt);
+       _add_onion_note($txt);
 
-EOM
-       }
        my $code_url = prurl($ctx->{env}, $PublicInbox::WwwStream::CODE_URL);
        $$txt .= join("\n\n",
                coderepos_raw($ctx, $top_url), # may be empty
-               "AGPL code for this site:\n\tgit clone $code_url");
+               "AGPL code for this site:\n  git clone $code_url");
        1;
 }
 
@@ -371,128 +400,127 @@ sub _default_text ($$$$) {
                        inbox_config($ctx, $hdr, $txt) :
                        extindex_config($ctx, $hdr, $txt);
        }
-
        return if $key ne 'help'; # TODO more keys?
 
        my $ibx = $ctx->{ibx};
        my $base_url = $ibx->base_url($ctx->{env});
-       $$txt .= "public-inbox help for $base_url\n";
        $$txt .= <<EOF;
+public-inbox help for $base_url
 
 overview
 --------
 
-    public-inbox uses Message-ID identifiers in URLs.
-    One may look up messages by substituting Message-IDs
-    (without the leading '<' or trailing '>') into the URL.
-    Forward slash ('/') characters in the Message-IDs
-    need to be escaped as "%2F" (without quotes).
+  public-inbox uses Message-ID identifiers in URLs.
+  One may look up messages by substituting Message-IDs
+  (without the leading '<' or trailing '>') into the URL.
+  Forward slash ('/') characters in the Message-IDs
+  need to be escaped as "%2F" (without quotes).
 
-    Thus, it is possible to retrieve any message by its
-    Message-ID by going to:
+  Thus, it is possible to retrieve any message by its
+  Message-ID by going to:
 
-       $base_url<Message-ID>/
+    $base_url<Message-ID>/
+    (without the '<' or '>')
 
-       (without the '<' or '>')
+  Message-IDs are described at:
 
-    Message-IDs are described at:
-
-       $WIKI_URL/Message-ID
+    $WIKI_URL/Message-ID
 
 EOF
 
        # n.b. we use the Xapian DB for any regeneratable,
        # order-of-arrival-independent data.
-       my $srch = $ibx->isrch;
-       if ($srch) {
+       if ($ibx->isrch) {
                $$txt .= <<EOF;
 search
 ------
 
-    This public-inbox has search functionality provided by Xapian.
+  This public-inbox has search functionality provided by Xapian.
 
-    It supports typical AND, OR, NOT, '+', '-' queries present
-    in other search engines.
+  It supports typical AND, OR, NOT, '+', '-' queries present
+  in other search engines.
 
-    We also support search prefixes to limit the scope of the
-    search to certain fields.
+  We also support search prefixes to limit the scope of the
+  search to certain fields.
 
-    Prefixes supported in this installation include:
+  Prefixes supported in this installation include:
 
 EOF
-               _srch_prefix($srch, $txt);
-
+               _srch_prefix($ibx, $txt);
                $$txt .= <<EOF;
 
-    Most prefixes are probabilistic, meaning they support stemming
-    and wildcards ('*').  Ranges (such as 'd:') and boolean prefixes
-    do not support stemming or wildcards.
-    The upstream Xapian query parser documentation fully explains
-    the query syntax:
+  Most prefixes are probabilistic, meaning they support stemming
+  and wildcards ('*').  Ranges (such as 'd:') and boolean prefixes
+  do not support stemming or wildcards.
+  The upstream Xapian query parser documentation fully explains
+  the query syntax:
 
-       $QP_URL
+    $QP_URL
 
 EOF
        } # $srch
-       my $over = $ibx->over;
-       if ($over) {
+       if ($ibx->over) {
                $$txt .= <<EOF;
 message threading
 -----------------
 
-    Message threading is enabled for this public-inbox,
-    additional endpoints for message threads are available:
+  Message threading is enabled for this public-inbox,
+  additional endpoints for message threads are available:
 
-    * $base_url<Message-ID>/T/#u
+  * $base_url<Message-ID>/T/#u
 
-      Loads the thread belonging to the given <Message-ID>
-      in flat chronological order.  The "#u" anchor
-      focuses the browser on the given <Message-ID>.
+    Loads the thread belonging to the given <Message-ID>
+    in flat chronological order.  The "#u" anchor
+    focuses the browser on the given <Message-ID>.
 
-    * $base_url<Message-ID>/t/#u
+  * $base_url<Message-ID>/t/#u
 
-      Loads the thread belonging to the given <Message-ID>
-      in threaded order with nesting.  For deep threads,
-      this requires a wide display or horizontal scrolling.
+    Loads the thread belonging to the given <Message-ID>
+    in threaded order with nesting.  For deep threads,
+    this requires a wide display or horizontal scrolling.
 
-    Both of these HTML endpoints are suitable for offline reading
-    using the thread overview at the bottom of each page.
+  Both of these HTML endpoints are suitable for offline reading
+  using the thread overview at the bottom of each page.
 
-    Users of feed readers may follow a particular thread using:
+  The gzipped mbox for a thread is available for downloading and
+  importing into your favorite mail client:
 
-    * $base_url<Message-ID>/t.atom
+  * $base_url<Message-ID>/t.mbox.gz
 
-      Which loads the thread in Atom Syndication Standard
-      described at Wikipedia and RFC4287:
+    We use the mboxrd variant of the mbox format described at:
 
-       $WIKI_URL/Atom_(standard)
-       https://tools.ietf.org/html/rfc4287
+    $WIKI_URL/Mbox
 
-      Atom Threading Extensions (RFC4685) is supported:
+  Users of feed readers may follow a particular thread using:
 
-       https://tools.ietf.org/html/rfc4685
+  * $base_url<Message-ID>/t.atom
 
-    Finally, the gzipped mbox for a thread is available for
-    downloading and importing into your favorite mail client:
+    Which loads the thread in Atom Syndication Standard
+    described at Wikipedia and RFC4287:
 
-    * $base_url<Message-ID>/t.mbox.gz
+    $WIKI_URL/Atom_(standard)
+    https://tools.ietf.org/html/rfc4287
 
-    We use the mboxrd variant of the mbox format described
-    at:
+    Atom Threading Extensions (RFC4685) are supported:
 
-       $WIKI_URL/Mbox
+    https://tools.ietf.org/html/rfc4685
 
 EOF
        } # $over
 
+       _add_imap_nntp_urls($ctx, \(my $note = ''));
+       $note and $note =~ s/^/  /gms and $$txt .= <<EOF;
+additional protocols
+--------------------
+$note
+EOF
        $$txt .= <<EOF;
 contact
 -------
 
-    This help text is maintained by public-inbox developers
-    reachable via plain-text email at: meta\@public-inbox.org
-    Their inbox is archived at: https://public-inbox.org/meta/
-
+  This help text is maintained by public-inbox developers
+  reachable via plain-text email at: meta\@public-inbox.org
+  Their inbox is archived at: https://public-inbox.org/meta/
 EOF
        # TODO: support admin contact info in ~/.public-inbox/config
        1;