]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
a7c7cbc1239e6025e6594524a68ed803263dfcb1
[public-inbox.git] / lib / PublicInbox / WwwListing.pm
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>
3
4 # Provide an HTTP-accessible listing of inboxes.
5 # Used by PublicInbox::WWW
6 package PublicInbox::WwwListing;
7 use strict;
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
13
14 sub list_all_i {
15         my ($ibx, $list, $hide_key) = @_;
16         push @$list, $ibx unless $ibx->{-hide}->{$hide_key};
17 }
18
19 sub list_all ($$$) {
20         my ($self, $env, $hide_key) = @_;
21         my $list = [];
22         $self->{pi_config}->each_inbox(\&list_all_i, $list, $hide_key);
23         $list;
24 }
25
26 sub list_match_domain_i {
27         my ($ibx, $list, $hide_key, $re) = @_;
28         if (!$ibx->{-hide}->{$hide_key} && grep(/$re/, @{$ibx->{url}})) {
29                 push @$list, $ibx;
30         }
31 }
32
33 sub list_match_domain ($$$) {
34         my ($self, $env, $hide_key) = @_;
35         my $list = [];
36         my $host = $env->{HTTP_HOST} // $env->{SERVER_NAME};
37         $host =~ s/:[0-9]+\z//;
38         $self->{pi_config}->each_inbox(\&list_match_domain_i, $list, $hide_key,
39                                 qr!\A(?:https?:)?//\Q$host\E(?::[0-9]+)?/!i);
40         $list;
41 }
42
43 sub list_404 ($$) { [] }
44
45 # TODO: +cgit
46 my %VALID = (
47         all => \&list_all,
48         'match=domain' => \&list_match_domain,
49         404 => \&list_404,
50 );
51
52 sub set_cb ($$$) {
53         my ($pi_config, $k, $default) = @_;
54         my $v = $pi_config->{lc $k} // $default;
55         $VALID{$v} || do {
56                 warn <<"";
57 `$v' is not a valid value for `$k'
58 $k be one of `all', `match=domain', or `404'
59
60                 $VALID{$default};
61         };
62 }
63
64 sub new {
65         my ($class, $www) = @_;
66         my $pi_config = $www->{pi_config};
67         bless {
68                 pi_config => $pi_config,
69                 style => $www->style("\0"),
70                 www_cb => set_cb($pi_config, 'publicInbox.wwwListing', 404),
71                 manifest_cb => set_cb($pi_config, 'publicInbox.grokManifest',
72                                         'match=domain'),
73         }, $class;
74 }
75
76 sub ibx_entry {
77         my ($mtime, $ibx, $env) = @_;
78         my $ts = fmt_ts($mtime);
79         my $url = prurl($env, $ibx->{url});
80         my $tmp = <<"";
81 * $ts - $url
82   ${\$ibx->description}
83
84         if (defined(my $info_url = $ibx->{infourl})) {
85                 $tmp .= '  ' . prurl($env, $info_url) . "\n";
86         }
87         $tmp;
88 }
89
90 sub html ($$) {
91         my ($env, $list) = @_;
92         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
93                         'Content-Length', undef ];
94         my $gzf = gzf_maybe($h, $env);
95         $gzf->zmore('<html><head><title>' .
96                                 'public-inbox listing</title>' .
97                                 '</head><body><pre>');
98         my $code = 404;
99         if (@$list) {
100                 $code = 200;
101                 # Schwartzian transform since Inbox->modified is expensive
102                 @$list = sort {
103                         $b->[0] <=> $a->[0]
104                 } map { [ $_->modified, $_ ] } @$list;
105
106                 my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
107                 my $l = PublicInbox::Linkify->new;
108                 $gzf->zmore($l->to_html($tmp));
109         } else {
110                 $gzf->zmore('no inboxes, yet');
111         }
112         my $out = $gzf->zflush('</pre><hr><pre>'.
113                                 PublicInbox::WwwStream::code_footer($env) .
114                                 '</pre></body></html>');
115         $h->[3] = bytes::length($out);
116         [ $code, $h, [ $out ] ];
117 }
118
119 # not really a stand-alone PSGI app, but maybe it could be...
120 sub call {
121         my ($self, $env) = @_;
122
123         if ($env->{PATH_INFO} eq '/manifest.js.gz') {
124                 # grokmirror uses relative paths, so it's domain-dependent
125                 my $list = $self->{manifest_cb}->($self, $env, 'manifest');
126                 PublicInbox::ManifestJsGz::response($env, $list);
127         } else { # /
128                 my $list = $self->{www_cb}->($self, $env, 'www');
129                 html($env, $list);
130         }
131 }
132
133 1;