1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
8 use PublicInbox::Hval qw(prurl fmt_ts);
9 use PublicInbox::Linkify;
10 use PublicInbox::GzipFilter qw(gzf_maybe);
11 use PublicInbox::ConfigIter;
12 use bytes (); # bytes::length
16 my $mtime = $ibx->modified;
17 my $ts = fmt_ts($mtime);
18 my $url = prurl($ctx->{env}, $ibx->{url});
23 if (defined(my $info_url = $ibx->{infourl})) {
24 $tmp .= ' ' . prurl($ctx->{env}, $info_url) . "\n";
26 push @{$ctx->{-list}}, [ $mtime, $tmp ];
29 sub list_match_i { # ConfigIter callback
30 my ($cfg, $section, $re, $ctx) = @_;
31 if (defined($section)) {
32 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
33 my $ibx = $cfg->lookup_name($1) or return;
34 if (!$ibx->{-hide}->{$ctx->hide_key} &&
35 grep(/$re/, @{$ibx->{url}})) {
36 $ctx->ibx_entry($ibx);
38 } else { # undef == "EOF"
39 $ctx->{-wcb}->($ctx->psgi_triple);
44 my ($ctx, $key, $default) = @_;
45 $key //= 'publicInbox.wwwListing';
47 my $v = $ctx->{www}->{pi_config}->{lc $key} // $default;
49 if ($v eq 'match=domain') {
50 my $h = $ctx->{env}->{HTTP_HOST} // $ctx->{env}->{SERVER_NAME};
52 qr!\A(?:https?:)?//\Q$h\E(?::[0-9]+)?/!i;
53 } elsif ($v eq 'all') {
55 } elsif ($v eq '404') {
59 `$v' is not a valid value for `$key'
60 $key be one of `all', `match=domain', or `404'
62 $v = $default; # 'match=domain' or 'all'
67 sub hide_key { 'www' }
70 my ($class, $ctx) = @_;
72 my $re = $ctx->url_regexp or return $ctx->psgi_triple;
73 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_config},
74 \&list_match_i, $re, $ctx);
76 $ctx->{-wcb} = $_[0]; # HTTP server callback
77 $ctx->{env}->{'pi-httpd.async'} ?
78 $iter->event_step : $iter->each_section;
84 my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
85 'Content-Length', undef ];
86 my $gzf = gzf_maybe($h, $ctx->{env});
87 $gzf->zmore('<html><head><title>' .
88 'public-inbox listing</title>' .
89 '</head><body><pre>');
91 if (my $list = $ctx->{-list}) {
94 @$list = map { $_->[1] } sort { $b->[0] <=> $a->[0] } @$list;
95 $list = join("\n", @$list);
96 my $l = PublicInbox::Linkify->new;
97 $gzf->zmore($l->to_html($list));
99 $gzf->zmore('no inboxes, yet');
101 my $out = $gzf->zflush('</pre><hr><pre>'.
102 PublicInbox::WwwStream::code_footer($ctx->{env}) .
103 '</pre></body></html>');
104 $h->[3] = bytes::length($out);
105 [ $code, $h, [ $out ] ];