]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwListing.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / WwwListing.pm
1 # Copyright (C) 2019-2021 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 v5.10.1;
9 use PublicInbox::Hval qw(prurl fmt_ts ascii_html);
10 use PublicInbox::GzipFilter qw(gzf_maybe);
11 use PublicInbox::ConfigIter;
12 use PublicInbox::WwwStream;
13 use URI::Escape qw(uri_escape_utf8);
14 use PublicInbox::MID qw(mid_escape);
15
16 sub ibx_entry {
17         my ($ctx, $ibx, $ce) = @_;
18         my $desc = ascii_html($ce->{description} //= $ibx->description);
19         my $ts = fmt_ts($ce->{-modified} //= $ibx->modified);
20         my ($url, $href);
21         if (scalar(@{$ibx->{url} // []})) {
22                 $url = $href = ascii_html(prurl($ctx->{env}, $ibx->{url}));
23         } else {
24                 $href = ascii_html(uri_escape_utf8($ibx->{name})) . '/';
25                 $url = ascii_html($ibx->{name});
26         }
27         my $tmp = <<EOM;
28 * $ts - <a\nhref="$href">$url</a>
29   $desc
30 EOM
31         if (defined($url = $ibx->{infourl})) {
32                 $url = ascii_html(prurl($ctx->{env}, $url));
33                 $tmp .= qq(  <a\nhref="$url">$url</a>\n);
34         }
35         push(@{$ctx->{-list}}, (scalar(@_) == 3 ? # $misc in use, already sorted
36                                 $tmp : [ $ce->{-modified}, $tmp ] ));
37 }
38
39 sub list_match_i { # ConfigIter callback
40         my ($cfg, $section, $re, $ctx) = @_;
41         if (defined($section)) {
42                 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
43                 my $ibx = $cfg->lookup_name($1) or return;
44                 if (!$ibx->{-hide}->{$ctx->hide_key} &&
45                                         grep(/$re/, @{$ibx->{url} // []})) {
46                         $ctx->ibx_entry($ibx);
47                 }
48         } else { # undef == "EOF"
49                 $ctx->{-wcb}->($ctx->psgi_triple);
50         }
51 }
52
53 sub url_filter {
54         my ($ctx, $key, $default) = @_;
55         $key //= 'publicInbox.wwwListing';
56         $default //= '404';
57         my $v = $ctx->{www}->{pi_cfg}->{lc $key} // $default;
58 again:
59         if ($v eq 'match=domain') {
60                 my $h = $ctx->{env}->{HTTP_HOST} // $ctx->{env}->{SERVER_NAME};
61                 $h =~ s/:[0-9]+\z//;
62                 (qr!\A(?:https?:)?//\Q$h\E(?::[0-9]+)?/!i, "url:$h");
63         } elsif ($v eq 'all') {
64                 (qr/./, undef);
65         } elsif ($v eq '404') {
66                 (undef, undef);
67         } else {
68                 warn <<EOF;
69 `$v' is not a valid value for `$key'
70 $key be one of `all', `match=domain', or `404'
71 EOF
72                 $v = $default; # 'match=domain' or 'all'
73                 goto again;
74         }
75 }
76
77 sub hide_key { 'www' }
78
79 sub add_misc_ibx { # MiscSearch->retry_reopen callback
80         my ($misc, $ctx, $re, $qs) = @_;
81         require PublicInbox::SearchQuery;
82         my $q = $ctx->{-sq} = PublicInbox::SearchQuery->new($ctx->{qp});
83         my $o = $q->{o};
84         my ($asc, $min, $max);
85         if ($o < 0) {
86                 $asc = 1;
87                 $o = -($o + 1); # so [-1] is the last element, like Perl lists
88         }
89         my $r = $q->{r};
90         my $opt = {
91                 offset => $o,
92                 asc => $asc,
93                 relevance => $r,
94                 limit => $q->{l}
95         };
96         $qs .= ' type:inbox';
97
98         delete $ctx->{-list}; # reset if retried
99         my $pi_cfg = $ctx->{www}->{pi_cfg};
100         my $user_query = $q->{'q'} // '';
101         if ($user_query =~ /\S/) {
102                 $qs = "( $qs ) AND ( $user_query )";
103         } else { # special case for ALL
104                 $ctx->ibx_entry($pi_cfg->ALL // die('BUG: ->ALL expected'), {});
105         }
106         my $mset = $misc->mset($qs, $opt); # sorts by $MODIFIED (mtime)
107         my $hide_key = $ctx->hide_key;
108
109         for my $mi ($mset->items) {
110                 my $doc = $mi->get_document;
111                 my ($eidx_key) = PublicInbox::Search::xap_terms('Q', $doc);
112                 $eidx_key // next;
113                 my $ibx = $pi_cfg->lookup_eidx_key($eidx_key) // next;
114                 next if $ibx->{-hide}->{$hide_key};
115                 grep(/$re/, @{$ibx->{url} // []}) or next;
116                 $ctx->ibx_entry($ibx, $misc->doc2ibx_cache_ent($doc));
117                 if ($r) { # for descriptions in search_nav_bot
118                         my $pct = PublicInbox::Search::get_pct($mi);
119                         # only when sorting by relevance, ->items is always
120                         # ordered descending:
121                         $max //= $pct;
122                         $min = $pct;
123                 }
124         }
125         if ($r) { # for descriptions in search_nav_bot
126                 $q->{-min_pct} = $min;
127                 $q->{-max_pct} = $max;
128         }
129         $ctx->{-mset} = $mset;
130         psgi_triple($ctx);
131 }
132
133 sub response {
134         my ($class, $ctx) = @_;
135         bless $ctx, $class;
136         my ($re, $qs) = $ctx->url_filter;
137         $re // return $ctx->psgi_triple;
138         if (my $ALL = $ctx->{www}->{pi_cfg}->ALL) { # fast path
139                 if ($ctx->{qp}->{a} && # "search all inboxes"
140                                 $ctx->{qp}->{'q'}) {
141                         my $u = 'all/?q='.mid_escape($ctx->{qp}->{'q'});
142                         return [ 302, [ 'Location' => $u,
143                                 qw(Content-Type text/plain) ],
144                                 [ "Redirecting to $u\n" ] ];
145                 }
146                 # FIXME: test this in t/
147                 $ALL->misc->reopen->retry_reopen(\&add_misc_ibx,
148                                                 $ctx, $re, $qs);
149         } else { # slow path, no [extindex "all"] configured
150                 my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
151                                                 \&list_match_i, $re, $ctx);
152                 sub {
153                         $ctx->{-wcb} = $_[0]; # HTTP server callback
154                         $ctx->{env}->{'pi-httpd.async'} ?
155                                         $iter->event_step : $iter->each_section;
156                 }
157         }
158 }
159
160 sub mset_footer ($$) {
161         my ($ctx, $mset) = @_;
162         # no footer if too few matches
163         return '' if $mset->get_matches_estimated == $mset->size;
164         require PublicInbox::SearchView;
165         PublicInbox::SearchView::search_nav_bot($mset, $ctx->{-sq});
166 }
167
168 sub mset_nav_top {
169         my ($ctx, $mset) = @_;
170         my $q = $ctx->{-sq};
171         my $qh = $q->{'q'} // '';
172         utf8::decode($qh);
173         $qh = ascii_html($qh);
174         $qh = qq[\nvalue="$qh"] if $qh ne '';
175         my $rv = <<EOM;
176 <form
177 action="./"><pre><input name=q type=text$qh
178 /><input type=submit value="locate inbox"
179 /><input type=submit name=a value="search all inboxes"
180 /></pre></form><pre>
181 EOM
182         chomp $rv;
183         if (defined($q->{'q'})) {
184                 my $initial_q = $ctx->{-uxs_retried};
185                 if (defined $initial_q) {
186                         my $rewritten = $q->{'q'};
187                         utf8::decode($initial_q);
188                         utf8::decode($rewritten);
189                         $initial_q = ascii_html($initial_q);
190                         $rewritten = ascii_html($rewritten);
191                         $rv .= " Warning: Initial query:\n <b>$initial_q</b>\n";
192                         $rv .= " returned no results, used:\n";
193                         $rv .= " <b>$rewritten</b>\n instead\n\n";
194                 }
195                 $rv .= 'Search results ordered by [';
196                 if ($q->{r}) {
197                         my $d = $q->qs_html(r => 0);
198                         $rv .= qq{<a\nhref="?$d">updated</a>|<b>relevance</b>};
199                 } else {
200                         my $d = $q->qs_html(r => 1);
201                         $rv .= qq{<b>updated</b>|<a\nhref="?$d">relevance</a>};
202                 }
203                 $rv .= ']';
204         }
205         $rv .= qq{</pre>};
206 }
207
208 sub psgi_triple {
209         my ($ctx) = @_;
210         my $h = [ 'Content-Type', 'text/html; charset=UTF-8',
211                         'Content-Length', undef ];
212         my $gzf = gzf_maybe($h, $ctx->{env});
213         $gzf->zmore('<html><head><title>public-inbox listing</title>' .
214                         $ctx->{www}->style('+/') .
215                         '</head><body>');
216         my $code = 404;
217         if (my $list = delete $ctx->{-list}) {
218                 my $mset = delete $ctx->{-mset};
219                 $code = 200;
220                 if ($mset) { # already sorted, so search bar:
221                         $gzf->zmore(mset_nav_top($ctx, $mset));
222                 } else { # sort config dump by ->modified
223                         @$list = map { $_->[1] }
224                                 sort { $b->[0] <=> $a->[0] } @$list;
225                 }
226                 $gzf->zmore('<pre>');
227                 $gzf->zmore(join("\n", @$list));
228                 $gzf->zmore(mset_footer($ctx, $mset)) if $mset;
229         } elsif (my $mset = delete $ctx->{-mset}) {
230                 $gzf->zmore(mset_nav_top($ctx, $mset));
231                 $gzf->zmore('<pre>no matching inboxes');
232                 $gzf->zmore(mset_footer($ctx, $mset));
233         } else {
234                 $gzf->zmore('<pre>no inboxes, yet');
235         }
236         my $out = $gzf->zflush('</pre><hr><pre>'.
237 qq(This is a listing of public inboxes, see the `mirror' link of each inbox
238 for instructions on how to mirror all the data and code on this site.) .
239                         '</pre></body></html>');
240         $h->[3] = length($out);
241         [ $code, $h, [ $out ] ];
242 }
243
244 1;