]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: avoid incorrect instructions for extindex
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # HTML body stream for which yields getline+close methods for
5 # generic PSGI servers and callbacks for public-inbox-httpd.
6 #
7 # See PublicInbox::GzipFilter parent class for more info.
8 package PublicInbox::WwwStream;
9 use strict;
10 use v5.10.1;
11 use parent qw(Exporter PublicInbox::GzipFilter);
12 our @EXPORT_OK = qw(html_oneshot);
13 use PublicInbox::Hval qw(ascii_html prurl ts2str);
14 our $TOR_URL = 'https://www.torproject.org/';
15
16 our $CODE_URL = [ qw(
17 http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git
18 https://public-inbox.org/public-inbox.git) ];
19
20 sub base_url ($) {
21         my $ctx = shift;
22         my $base_url = $ctx->{ibx}->base_url($ctx->{env});
23         chop $base_url; # no trailing slash for clone
24         $base_url;
25 }
26
27 sub init {
28         my ($ctx, $cb) = @_;
29         $ctx->{cb} = $cb;
30         $ctx->{base_url} = base_url($ctx);
31         bless $ctx, __PACKAGE__;
32 }
33
34 sub async_eml { # for async_blob_cb
35         my ($ctx, $eml) = @_;
36         $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml)));
37 }
38
39 sub html_top ($) {
40         my ($ctx) = @_;
41         my $ibx = $ctx->{ibx};
42         my $desc = ascii_html($ibx->description);
43         my $title = delete($ctx->{-title_html}) // $desc;
44         my $upfx = $ctx->{-upfx} || '';
45         my $help = $upfx.'_/text/help/';
46         my $color = $upfx.'_/text/color/';
47         my $atom = $ctx->{-atom} || $upfx.'new.atom';
48         my $top = "<b>$desc</b>";
49         if (my $t_max = $ctx->{-t_max}) {
50                 $t_max = ts2str($t_max);
51                 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
52         # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
53         } elsif ($ctx->{qp}->{t}) {
54                 $top = qq(<a\nhref="./">$top</a>);
55         }
56         my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
57         my $links = qq(<a\nhref="$help">help</a> / ).
58                         qq(<a\nhref="$color">color</a> / ).
59                         qq(<a\nhref=#mirror>mirror</a>$code / ).
60                         qq(<a\nhref="$atom">Atom feed</a>);
61         if ($ibx->isrch) {
62                 my $q_val = delete($ctx->{-q_value_html}) // '';
63                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
64                 # XXX gross, for SearchView.pm
65                 my $extra = delete($ctx->{-extra_form_html}) // '';
66                 my $action = $upfx eq '' ? './' : $upfx;
67                 $top = qq{<form\naction="$action"><pre>$top} .
68                           qq{\n<input\nname=q\ntype=text$q_val />} .
69                           $extra .
70                           qq{<input\ntype=submit\nvalue=search />} .
71                           ' ' . $links .
72                           q{</pre></form>}
73         } else {
74                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
75         }
76         "<html><head><title>$title</title>" .
77                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
78                 qq(href="$atom"\ntype="application/atom+xml"/>) .
79                 $ctx->{www}->style($upfx) .
80                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
81 }
82
83 sub coderepos ($) {
84         my ($ctx) = @_;
85         my $cr = $ctx->{ibx}->{coderepo} // return ();
86         my $cfg = $ctx->{www}->{pi_cfg};
87         my $upfx = ($ctx->{-upfx} // ''). '../';
88         my @ret;
89         for my $cr_name (@$cr) {
90                 $ret[0] //= <<EOF;
91 <a id=code>code repositories for project(s) associated with this inbox:
92 EOF
93                 my $urls = $cfg->get_all("coderepo.$cr_name.cgiturl");
94                 if ($urls) {
95                         for (@$urls) {
96                                 # relative or absolute URL?, prefix relative
97                                 # "foo.git" with appropriate number of "../"
98                                 my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $upfx.$_;
99                                 $u = ascii_html(prurl($ctx->{env}, $u));
100                                 $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
101                         }
102                 } else {
103                         $ret[0] .= qq[\n\t$cr_name.git (no URL configured)];
104                 }
105         }
106         @ret; # may be empty, this sub is called as an arg for join()
107 }
108
109 sub code_footer ($) {
110         my ($env) = @_;
111         my $u = prurl($env, $CODE_URL);
112         my $arg = $u;
113         if ($arg =~ s!\A(https?://)([^/\.]+)\.onion/!$1\$hostname\.onion/!i) {
114                 "AGPL code for this site:\n\thostname=$2\n\t" .
115                 qq(torsocks git clone <a\nhref="$u">$arg</a>)
116         } else {
117                 qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
118         }
119 }
120
121 sub _html_end {
122         my ($ctx) = @_;
123         my $ibx = $ctx->{ibx};
124         my $desc = ascii_html($ibx->description);
125         my $s = "<a\nid=mirror>";
126         my $http = $ctx->{base_url};
127         my $dir = (split(m!/!, $http))[-1];
128         my %seen = ($http => 1);
129         if ($ibx->can('cloneurl')) { # PublicInbox::Inbox
130                 $s .= "This inbox may be cloned and mirrored by anyone:</a>\n";
131                 my @urls;
132                 my $max = $ibx->max_git_epoch;
133                 # TODO: some of these URLs may be too long and we may need to
134                 # do something like code_footer() above, but these are local
135                 # admin-defined
136                 if (defined($max)) { # v2
137                         for my $i (0..$max) {
138                                 # old epochs my be deleted:
139                                 -d "$ibx->{inboxdir}/git/$i.git" or next;
140                                 my $url = "$http/$i";
141                                 $seen{$url} = 1;
142                                 push @urls, "$url $dir/git/$i.git";
143                         }
144                         my $nr = scalar(@urls);
145                         if ($nr > 1) {
146                                 $s .= "\n\t";
147                                 $s .= "# this inbox consists of $nr epochs:";
148                                 $urls[0] .= "\t# oldest";
149                                 $urls[-1] .= "\t# newest";
150                         }
151                 } else { # v1
152                         push @urls, $http;
153                 }
154                 # FIXME: epoch splits can be different in other repositories,
155                 # use the "cloneurl" file as-is for now:
156                 for my $u (@{$ibx->cloneurl}) {
157                         next if $seen{$u}++;
158                         push @urls, ($u =~ /\Ahttps?:/ ?
159                                         qq(<a\nhref="$u">$u</a>) : $u);
160                 }
161                 $s .= "\n";
162                 $s .= join('', map { "\tgit clone --mirror $_\n" } @urls);
163                 if (my $addrs = $ibx->{address}) {
164                         $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
165                         my $v = defined $max ? '-V2' : '-V1';
166                         $s .= <<EOF;
167
168         # If you have public-inbox 1.1+ installed, you may
169         # initialize and index your mirror using the following commands:
170         public-inbox-init $v $ibx->{name} $dir/ $http \\
171                 $addrs
172         public-inbox-index $dir
173 EOF
174                 }
175         } else { # PublicInbox::ExtSearch
176                 $s .= <<EOM;
177 This is an extindex which is an amalgamation of several public-inboxes</a>
178 EOM
179                 my $v = $ctx->{www}->{pi_cfg}->{lc('publicInbox.wwwListing')};
180                 if (($v // '') =~ /\A(?:all|match=domain)\z/) {
181                         my $upfx = ($ctx->{-upfx} // ''). '../';
182                         $s .= <<EOM;
183 A list of them is available in the <a\nhref="$upfx">listing</a>
184 EOM
185                 }
186         }
187
188         my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
189         $s .= <<EOF;
190
191 Example <a
192 href="$cfg_link">config snippet</a> for mirrors.
193 EOF
194         if ($ibx->can('nntp_url')) {
195                 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
196                 if (@nntp) {
197                         $s .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
198                         $s .= ' available over NNTP:';
199                         $s .= "\n\t" . join("\n\t", @nntp) . "\n";
200                 }
201         }
202         if ($s =~ m!\b[^:]+://\w+\.onion/!) {
203                 $s .= " note: .onion URLs require Tor: ";
204                 $s .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
205         }
206         '<hr><pre>'.join("\n\n",
207                 $desc,
208                 $s,
209                 coderepos($ctx),
210                 code_footer($ctx->{env})
211         ).'</pre></body></html>';
212 }
213
214 # callback for HTTP.pm (and any other PSGI servers)
215 sub getline {
216         my ($ctx) = @_;
217         my $cb = $ctx->{cb} or return;
218         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
219                 if (ref($x)) { # smsg
220                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
221                         $ctx->{smsg} = $x;
222                         return $ctx->translate($cb->($ctx, $eml));
223                 } else { # scalar
224                         return $ctx->translate($x);
225                 }
226         }
227         delete $ctx->{cb};
228         $ctx->zflush(_html_end($ctx));
229 }
230
231 sub html_oneshot ($$;$) {
232         my ($ctx, $code, $sref) = @_;
233         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
234                 'Content-Length' => undef ];
235         bless $ctx, __PACKAGE__;
236         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
237         $ctx->{base_url} //= do {
238                 $ctx->zmore(html_top($ctx));
239                 base_url($ctx);
240         };
241         $ctx->zmore($$sref) if $sref;
242         my $bdy = $ctx->zflush(_html_end($ctx));
243         $res_hdr->[3] = length($bdy);
244         [ $code, $res_hdr, [ $bdy ] ]
245 }
246
247 sub async_next ($) {
248         my ($http) = @_; # PublicInbox::HTTP
249         my $ctx = $http->{forward} or return;
250         eval {
251                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
252                         $ctx->smsg_blob($smsg);
253                 } else {
254                         $ctx->{http_out}->write(
255                                         $ctx->translate(_html_end($ctx)));
256                         $ctx->close; # GzipFilter->close
257                 }
258         };
259         warn "E: $@" if $@;
260 }
261
262 sub aresponse {
263         my ($ctx, $code, $cb) = @_;
264         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
265         init($ctx, $cb);
266         $ctx->psgi_response($code, $res_hdr);
267 }
268
269 1;