1 # Copyright (C) 2019 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;
9 use PublicInbox::Hval qw(ascii_html);
10 use PublicInbox::Linkify;
11 use PublicInbox::View;
14 my ($self, undef) = @_;
16 $self->{pi_config}->each_inbox(sub {
18 push @list, $ibx unless $ibx->{-hide}->{www};
23 sub list_match_domain ($$) {
24 my ($self, $env) = @_;
26 my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
27 $host =~ s/:[0-9]+\z//;
28 my $re = qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i;
29 $self->{pi_config}->each_inbox(sub {
31 push @list, $ibx if !$ibx->{-hide}->{www} && $ibx->{url} =~ $re;
36 sub list_404 ($$) { [] }
41 'match=domain' => *list_match_domain,
46 my ($class, $www) = @_;
47 my $k = 'publicinbox.wwwListing';
48 my $pi_config = $www->{pi_config};
49 my $v = $pi_config->{lc($k)} // 404;
51 pi_config => $pi_config,
52 style => $www->style("\0"),
53 list_cb => $VALID{$v} || do {
55 `$v' is not a valid value for `$k'
56 $k be one of `all', `match=domain', or `404'
64 my ($mtime, $ibx, $env) = @_;
65 my $ts = PublicInbox::View::fmt_ts($mtime);
66 my $url = PublicInbox::Hval::prurl($env, $ibx->{url});
71 if (defined(my $info_url = $ibx->{info_url})) {
72 $tmp .= "\n$info_url";
77 # not really a stand-alone PSGI app, but maybe it could be...
79 my ($self, $env) = @_;
80 my $h = [ 'Content-Type', 'text/html; charset=UTF-8' ];
81 my $list = $self->{list_cb}->($self, $env);
83 my $title = 'public-inbox';
86 # Swartzian transform since ->modified is expensive
89 } map { [ $_->modified, $_ ] } @$list;
92 $title .= ' - listing';
93 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
94 my $l = PublicInbox::Linkify->new;
96 $out = '<pre>'.$l->linkify_2(ascii_html($tmp)).'</pre><hr>';
98 $out = "<html><head><title>$title</title></head><body>" . $out;
99 $out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .
100 '</pre></body></html>';
101 [ $code, $h, [ $out ] ]