]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www_stream: coderepo-specific top bar
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 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
15 our $CODE_URL = [ qw(
16 http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git
17 https://public-inbox.org/public-inbox.git) ];
18
19 sub base_url ($) {
20         my $ctx = shift;
21         my $base_url = ($ctx->{ibx} // $ctx->{git})->base_url($ctx->{env});
22         chop $base_url; # no trailing slash for clone
23         $base_url;
24 }
25
26 sub init {
27         my ($ctx, $cb) = @_;
28         $ctx->{cb} = $cb;
29         $ctx->{base_url} = base_url($ctx);
30         $ctx->{-res_hdr} = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
31         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($ctx->{-res_hdr},
32                                                         $ctx->{env});
33         bless $ctx, __PACKAGE__;
34 }
35
36 sub async_eml { # for async_blob_cb
37         my ($ctx, $eml) = @_;
38         $ctx->write($ctx->{cb}->($ctx, $eml));
39 }
40
41 sub html_repo_top ($) {
42         my ($ctx) = @_;
43         my $git = $ctx->{git};
44         my $desc = ascii_html($git->description);
45         my $title = delete($ctx->{-title_html}) // $desc;
46         my $upfx = $ctx->{-upfx} // '';
47         my $atom = $ctx->{-atom} // (substr($upfx, -1) eq '/' ?
48                                         "${upfx}atom/" : "$upfx/atom/");
49         my $top = ascii_html($git->{nick});
50         $top = qq(<a\nhref="$upfx">$top</a>) if length($upfx);
51         $top .= <<EOM;
52   <a href='$upfx#readme'>about</a> / <a
53 href='$upfx#heads'>heads</a> / <a
54 href='$upfx#tags'>tags</a>
55 <b>$desc</b>
56 EOM
57         my @url = PublicInbox::ViewVCS::ibx_url_for($ctx);
58         if (@url) {
59                 $ctx->{-has_srch} = 1;
60                 my $base_url = base_url($ctx);
61                 my ($pfx, $sfx) = ($base_url =~ m!\A(https?://[^/]+/)(.*)\z!i);
62                 my $iupfx = '../' x (($sfx =~ tr!/!/!) + 1);
63                 $pfx = ascii_html($pfx);
64                 $pfx = qr/\A\Q$pfx\E/i;
65                 my $tmp = $top;
66                 $top = '';
67                 my ($s, $u, $same_host);
68                 my $q_val = delete($ctx->{-q_value_html}) // '';
69                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
70                 for (@url) {
71                         $u = $s = ascii_html($_);
72                         substr($u, 0, 0, $iupfx) if $u !~ m!://!;
73                         $s =~ s!$pfx!!;
74                         $s =~ s!/\z!!;
75                         $top .= qq{<form\naction="$u"><pre>$tmp} .
76                                 qq{<input\nname=q type=text$q_val />} .
77                                 qq{<input type=submit\n} .
78                                 qq{value="search mail in `$s&#39;"/>} .
79                                 q{</pre></form>};
80                         $tmp = '';
81                 }
82         } else {
83                 $top = "<pre>$top</pre>";
84         }
85         "<html><head><title>$title</title>" .
86                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
87                 qq(href="$atom"\ntype="application/atom+xml"/>) .
88                 $ctx->{www}->style($upfx) .
89                 '</head><body>'.$top;
90 }
91
92 sub html_top ($) {
93         my ($ctx) = @_;
94         my $ibx = $ctx->{ibx} // return html_repo_top($ctx);
95         my $desc = ascii_html($ibx->description);
96         my $title = delete($ctx->{-title_html}) // $desc;
97         my $upfx = $ctx->{-upfx} || '';
98         my $atom = $ctx->{-atom} || $upfx.'new.atom';
99         my $top = "<b>$desc</b>";
100         if (my $t_max = $ctx->{-t_max}) {
101                 $t_max = ts2str($t_max);
102                 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
103         # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
104         } elsif ($ctx->{qp}->{t}) {
105                 $top = qq(<a\nhref="./">$top</a>);
106         } elsif (length($upfx)) {
107                 $top = qq(<a\nhref="$upfx">$top</a>);
108         }
109         my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
110         # id=mirror must exist for legacy bookmarks
111         my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
112                         qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
113                         qq(<a\nid=mirror) .
114                         qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
115                         qq(<a\nhref="$atom">Atom feed</a>);
116         if ($ibx->isrch) {
117                 my $q_val = delete($ctx->{-q_value_html}) // '';
118                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
119                 # XXX gross, for SearchView.pm
120                 my $extra = delete($ctx->{-extra_form_html}) // '';
121                 my $action = $upfx eq '' ? './' : $upfx;
122                 $top = qq{<form\naction="$action"><pre>$top} .
123                           qq{\n<input\nname=q\ntype=text$q_val />} .
124                           $extra .
125                           qq{<input\ntype=submit\nvalue=search />} .
126                           ' ' . $links .
127                           q{</pre></form>}
128         } else {
129                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
130         }
131         "<html><head><title>$title</title>" .
132                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
133                 qq(href="$atom"\ntype="application/atom+xml"/>) .
134                 $ctx->{www}->style($upfx) .
135                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
136 }
137
138 sub inboxes { () } # TODO
139
140 sub coderepos ($) {
141         my ($ctx) = @_;
142         $ctx->{ibx} // return inboxes($ctx);
143         my $cr = $ctx->{ibx}->{coderepo} // return ();
144         my $upfx = ($ctx->{-upfx} // ''). '../';
145         my $pfx = $ctx->{base_url} //= $ctx->base_url;
146         my $up = $upfx =~ tr!/!/!;
147         $pfx =~ s!/[^/]+\z!/! for (1..$up);
148         $pfx .= '/' if substr($pfx, -1, 1) ne '/';
149         my $buf = '<a id=code>' .
150                 'Code repositories for project(s) associated with this '.
151                 $ctx->{ibx}->thing_type . "\n";
152         for my $git (@{$ctx->{www}->{pi_cfg}->repo_objs($ctx->{ibx})}) {
153                 for ($git->pub_urls($ctx->{env})) {
154                         my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : $pfx.$_;
155                         $u = ascii_html(prurl($ctx->{env}, $u));
156                         $buf .= qq(\n\t<a\nhref="$u">$u</a>);
157                 }
158         }
159         ($buf);
160 }
161
162 sub _html_end {
163         my ($ctx) = @_;
164         my $upfx = $ctx->{-upfx} || '';
165         my $m = "${upfx}_/text/mirror/";
166         my $x = '';
167         if ($ctx->{ibx} && $ctx->{ibx}->can('cloneurl')) {
168                 $x = <<EOF;
169 This is a public inbox, see <a
170 href="$m">mirroring instructions</a>
171 for how to clone and mirror all data and code used for this inbox
172 EOF
173                 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
174                 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
175                 if ($has_nntp || $has_imap) {
176                         substr($x, -1, 1) = ";\n"; # s/\n/;\n
177                         if ($has_nntp && $has_imap) {
178                                 $x .= <<EOM;
179 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
180 EOM
181                         } elsif ($has_nntp) {
182                                 $x .= <<EOM;
183 as well as URLs for NNTP newsgroup(s).
184 EOM
185                         } else {
186                                 $x .= <<EOM;
187 as well as URLs for IMAP folder(s).
188 EOM
189                         }
190                 }
191         } elsif ($ctx->{ibx}) { # extindex
192                 $x = <<EOF;
193 This is an external index of several public inboxes,
194 see <a href="$m">mirroring instructions</a> on how to clone and mirror
195 all data and code used by this external index.
196 EOF
197         } elsif ($ctx->{git}) { # coderepo
198                 $x = join('', map { "git clone $_\n" }
199                         @{$ctx->{git}->cloneurl($ctx->{env})});
200         }
201         chomp $x;
202         '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
203 }
204
205 # callback for HTTP.pm (and any other PSGI servers)
206 sub getline {
207         my ($ctx) = @_;
208         my $cb = $ctx->{cb} or return;
209         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
210                 if (ref($x)) { # smsg
211                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
212                         $ctx->{smsg} = $x;
213                         return $ctx->translate($cb->($ctx, $eml));
214                 } else { # scalar
215                         return $ctx->translate($x);
216                 }
217         }
218         delete $ctx->{cb};
219         $ctx->zflush(_html_end($ctx));
220 }
221
222 sub html_done ($;@) {
223         my $ctx = $_[0];
224         my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
225         my $res_hdr = delete $ctx->{-res_hdr};
226         push @$res_hdr, 'Content-Length', length($bdy);
227         [ 200, $res_hdr, [ $bdy ] ]
228 }
229
230 sub html_oneshot ($$;@) {
231         my ($ctx, $code) = @_[0, 1];
232         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
233                 'Content-Length' => undef ];
234         bless $ctx, __PACKAGE__;
235         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
236         my @top;
237         $ctx->{base_url} // do {
238                 @top = html_top($ctx);
239                 $ctx->{base_url} = base_url($ctx);
240         };
241         my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
242         $res_hdr->[3] = length($bdy);
243         [ $code, $res_hdr, [ $bdy ] ]
244 }
245
246 sub async_next ($) {
247         my ($http) = @_; # PublicInbox::HTTP
248         my $ctx = $http->{forward} or return;
249         eval {
250                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
251                         $ctx->smsg_blob($smsg);
252                 } else {
253                         $ctx->write(_html_end($ctx));
254                         $ctx->close; # GzipFilter->close
255                 }
256         };
257         warn "E: $@" if $@;
258 }
259
260 sub aresponse {
261         my ($ctx, $cb) = @_;
262         init($ctx, $cb);
263         $ctx->psgi_response(200, delete $ctx->{-res_hdr});
264 }
265
266 sub html_init {
267         my ($ctx) = @_;
268         $ctx->{base_url} = base_url($ctx);
269         my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
270         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
271         bless $ctx, __PACKAGE__;
272         print { $ctx->zfh } html_top($ctx);
273 }
274
275 sub DESTROY {
276         my ($ctx) = @_;
277         $ctx->{git}->cleanup if $ctx->{git} && $ctx->{git}->{-tmp};
278 }
279
280 1;