diff options
Diffstat (limited to 'lib/PublicInbox/WwwText.pm')
-rw-r--r-- | lib/PublicInbox/WwwText.pm | 119 |
1 files changed, 67 insertions, 52 deletions
diff --git a/lib/PublicInbox/WwwText.pm b/lib/PublicInbox/WwwText.pm index 2b4e69fe..5e23005e 100644 --- a/lib/PublicInbox/WwwText.pm +++ b/lib/PublicInbox/WwwText.pm @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org> +# Copyright (C) all contributors <meta@public-inbox.org> # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> # used for displaying help texts and other non-mail content @@ -7,7 +7,7 @@ use strict; use v5.10.1; use PublicInbox::Linkify; use PublicInbox::WwwStream; -use PublicInbox::Hval qw(ascii_html prurl); +use PublicInbox::Hval qw(ascii_html prurl fmt_ts); use HTTP::Date qw(time2str); use URI::Escape qw(uri_escape_utf8); use PublicInbox::GzipFilter qw(gzf_maybe); @@ -31,20 +31,17 @@ sub get_text { my $have_tslash = ($key =~ s!/\z!!) if !$raw; my $txt = ''; - my $hdr = [ 'Content-Type', 'text/plain', 'Content-Length', undef ]; - if (!_default_text($ctx, $key, $hdr, \$txt)) { + if (!_default_text($ctx, $key, \$txt)) { $code = 404; $txt = "404 Not Found ($key)\n"; } my $env = $ctx->{env}; if ($raw) { - if ($code == 200) { - my $gzf = gzf_maybe($hdr, $env); - $txt = $gzf->translate($txt); - $txt .= $gzf->zflush; - } - $hdr->[3] = length($txt); - return [ $code, $hdr, [ $txt ] ] + my $h = delete $ctx->{-res_hdr}; + $txt = gzf_maybe($h, $env)->zflush($txt) if $code == 200; + push @$h, 'Content-Type', 'text/plain', + 'Content-Length', length($txt); + return [ $code, $h, [ $txt ] ] } # enforce trailing slash for "wget -r" compatibility @@ -71,7 +68,11 @@ sub get_text { $txt = ascii_html($txt); } $txt = '<pre>' . $l->linkify_2($txt) . '</pre>'; - PublicInbox::WwwStream::html_oneshot($ctx, $code, \$txt); + $txt =~ s!^search$!<a\nid=search>search</a>!sm; + $txt =~ s!\bPOP3\b!<a\nid=pop3>POP3</a>!; + $txt =~ s!\b(Newsgroups?)\b!<a\nid=nntp>$1</a>!; + $txt =~ s!\bIMAP\b!<a\nid=imap>IMAP</a>!; + PublicInbox::WwwStream::html_oneshot($ctx, $code, $txt); } sub _srch_prefix ($$) { @@ -167,12 +168,13 @@ EOF } # n.b. this is a perfect candidate for memoization -sub inbox_config ($$$) { - my ($ctx, $hdr, $txt) = @_; +sub inbox_config ($$) { + my ($ctx, $txt) = @_; my $ibx = $ctx->{ibx}; - push @$hdr, 'Content-Disposition', 'inline; filename=inbox.config'; + push @{$ctx->{-res_hdr}}, + 'Content-Disposition', 'inline; filename=inbox.config'; my $t = eval { $ibx->mm->created_at }; - push(@$hdr, 'Last-Modified', time2str($t)) if $t; + push(@{$ctx->{-res_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}); @@ -219,10 +221,11 @@ EOF } # n.b. this is a perfect candidate for memoization -sub extindex_config ($$$) { - my ($ctx, $hdr, $txt) = @_; +sub extindex_config ($$) { + my ($ctx, $txt) = @_; my $ibx = $ctx->{ibx}; - push @$hdr, 'Content-Disposition', 'inline; filename=extindex.config'; + push @{$ctx->{-res_hdr}}, + 'Content-Disposition', 'inline; filename=extindex.config'; my $name = dq_escape($ibx->{name}); my $base_url = $ibx->base_url($ctx->{env}); $$txt .= <<EOS; @@ -245,52 +248,64 @@ EOS sub coderepos_raw ($$) { my ($ctx, $top_url) = @_; - my $cr = $ctx->{ibx}->{coderepo} // return (); my $cfg = $ctx->{www}->{pi_cfg}; - my @ret; - for my $cr_name (@$cr) { - $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) { - # relative or absolute URL?, prefix relative - # "foo.git" with appropriate number of "../" - my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : - $top_url.$_; - $ret[0] .= "\n\t" . prurl($ctx->{env}, $u); - } - } else { - $ret[0] .= qq[\n\t$cr_name.git (no URL configured)]; + my $cr = $cfg->repo_objs($ctx->{ibx}) or return (); + my $buf = 'Code repositories for project(s) associated with this '. + $ctx->{ibx}->thing_type . ":\n"; + my @recs = PublicInbox::CodeSearch::repos_sorted($cfg, @$cr); + my $cr_score = $ctx->{ibx}->{-cr_score}; + my $env = $ctx->{env}; + for (@recs) { + my ($t, $git) = @$_; + for ($git->pub_urls($env)) { + my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : $top_url.$_; + my $nr = $cr_score->{$git->{nick}}; + $buf .= "\n"; + $buf .= $nr ? sprintf('% 9u', $nr) : (' 'x9); + $buf .= ' '.fmt_ts($t).' '.prurl($env, $u); } } - @ret; # may be empty, this sub is called as an arg for join() + ($buf); } -sub _add_imap_nntp_urls ($$) { +sub _add_non_http_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); + $urls = join("\n ", @$urls); + $urls =~ s!://([^/@]+)/!://;AUTH=ANONYMOUS\@$1/!sg; $$txt .= <<EOM +IMAP subfolder(s) are available under: + $urls # 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 .= @$urls == 1 ? "\nNewsgroup" : "\nNewsgroups are"; $$txt .= ' available over NNTP:'; $$txt .= "\n " . join("\n ", @$urls) . "\n"; } + $urls = $ctx->{ibx}->pop3_url($ctx); + if (@$urls) { + $urls = join("\n ", @$urls); + $$txt .= <<EOM; + +POP3 access is available: + $urls + +The POP3 password is: anonymous +The POP3 username is: \$(uuidgen)\@$ctx->{ibx}->{newsgroup} +where \$(uuidgen) in the output of the `uuidgen' command on your system. +The UUID in the username functions as a private cookie (don't share it). +By default, only 1000 messages are retrieved. You may download more +by appending `?limit=NUM' (without quotes) to the username, where +`NUM' is an integer between 1 and 50000. +Idle accounts will expire periodically. +EOM + } } sub _add_onion_note ($) { @@ -379,7 +394,7 @@ EOM Example config snippet for mirrors: $cfg_link EOF - _add_imap_nntp_urls($ctx, $txt); + _add_non_http_urls($ctx, $txt); _add_onion_note($txt); my $code_url = prurl($ctx->{env}, $PublicInbox::WwwStream::CODE_URL); @@ -389,16 +404,16 @@ EOF 1; } -sub _default_text ($$$$) { - my ($ctx, $key, $hdr, $txt) = @_; +sub _default_text ($$$) { + my ($ctx, $key, $txt) = @_; if ($key eq 'mirror') { return _mirror_help($ctx, $txt); } elsif ($key eq 'color') { return _colors_help($ctx, $txt); } elsif ($key eq 'config') { return $ctx->{ibx}->can('cloneurl') ? - inbox_config($ctx, $hdr, $txt) : - extindex_config($ctx, $hdr, $txt); + inbox_config($ctx, $txt) : + extindex_config($ctx, $txt); } return if $key ne 'help'; # TODO more keys? @@ -508,7 +523,7 @@ message threading EOF } # $over - _add_imap_nntp_urls($ctx, \(my $note = '')); + _add_non_http_urls($ctx, \(my $note = '')); $note and $note =~ s/^/ /gms and $$txt .= <<EOF; additional protocols -------------------- |