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