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