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(ascii_html prurl fmt_ts);
9 use PublicInbox::Linkify;
10 use PublicInbox::GzipFilter qw(gzf_maybe);
11 use PublicInbox::ManifestJsGz;
12 use bytes (); # bytes::length
16 my ($list, $hide_key) = @$arg;
17 push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
21 my ($self, $env, $hide_key) = @_;
23 $self->{pi_config}->each_inbox(\&list_all_i, [ $list, $hide_key ]);
27 sub list_match_domain_i {
29 my ($list, $hide_key, $re) = @$arg;
30 if (!$ibx->{-hide}->{$hide_key} && grep(/$re/, @{$ibx->{url}})) {
35 sub list_match_domain ($$$) {
36 my ($self, $env, $hide_key) = @_;
38 my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
39 $host =~ s/:[0-9]+\z//;
40 my $arg = [ $list, $hide_key,
41 qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i ];
42 $self->{pi_config}->each_inbox(\&list_match_domain_i, $arg);
46 sub list_404 ($$) { [] }
51 'match=domain' => \&list_match_domain,
56 my ($pi_config, $k, $default) = @_;
57 my $v = $pi_config->{lc $k} // $default;
60 `$v' is not a valid value for `$k'
61 $k be one of `all', `match=domain', or `404'
68 my ($class, $www) = @_;
69 my $pi_config = $www->{pi_config};
71 pi_config => $pi_config,
72 style => $www->style("\0"),
73 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
74 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
80 my ($mtime, $ibx, $env) = @_;
81 my $ts = fmt_ts($mtime);
82 my $url = prurl($env, $ibx->{url});
87 if (defined(my $info_url = $ibx->{infourl})) {
88 $tmp .= ' ' . prurl($env, $info_url) . "\n";
94 my ($env, $list) = @_;
95 my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
96 'Content-Length', undef ];
97 my $gzf = gzf_maybe($h, $env);
98 $gzf->zmore('<html><head><title>' .
99 'public-inbox listing</title>' .
100 '</head><body><pre>');
104 # Schwartzian transform since Inbox->modified is expensive
107 } map { [ $_->modified, $_ ] } @$list;
109 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
110 my $l = PublicInbox::Linkify->new;
111 $gzf->zmore($l->to_html($tmp));
113 $gzf->zmore('no inboxes, yet');
115 my $out = $gzf->zflush('</pre><hr><pre>'.
116 PublicInbox::WwwStream::code_footer($env) .
117 '</pre></body></html>');
118 $h->[3] = bytes::length($out);
119 [ $code, $h, [ $out ] ];
122 # not really a stand-alone PSGI app, but maybe it could be...
124 my ($self, $env) = @_;
126 if ($env->{PATH_INFO} eq '/manifest.js.gz') {
127 # grokmirror uses relative paths, so it's domain-dependent
128 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
129 PublicInbox::ManifestJsGz::response($env, $list);
131 my $list = $self->{www_cb}->($self, $env, 'www');