]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
No ext_urls
[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="$atom">Atom feed</a>);
112         if ($ibx->isrch) {
113                 my $q_val = delete($ctx->{-q_value_html}) // '';
114                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
115                 # XXX gross, for SearchView.pm
116                 my $extra = delete($ctx->{-extra_form_html}) // '';
117                 my $action = $upfx eq '' ? './' : $upfx;
118                 $top = qq{<form\naction="$action"><pre>$top} .
119                           qq{\n<input\nname=q\ntype=text$q_val />} .
120                           $extra .
121                           qq{<input\ntype=submit\nvalue=search />} .
122                           ' ' . $links .
123                           q{</pre></form>}
124         } else {
125                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
126         }
127         "<html><head><title>$title</title>" .
128                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
129                 qq(href="$atom"\ntype="application/atom+xml"/>) .
130                 $ctx->{www}->style($upfx) .
131                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
132 }
133
134 sub inboxes { () } # TODO
135
136 sub coderepos ($) {
137         my ($ctx) = @_;
138         $ctx->{ibx} // return inboxes($ctx);
139         my $cr = $ctx->{ibx}->{coderepo} // return ();
140         my $upfx = ($ctx->{-upfx} // ''). '../';
141         my $pfx = $ctx->{base_url} //= $ctx->base_url;
142         my $up = $upfx =~ tr!/!/!;
143         $pfx =~ s!/[^/]+\z!/! for (1..$up);
144         $pfx .= '/' if substr($pfx, -1, 1) ne '/';
145         my $buf = '<a id=code>' .
146                 'Code repositories for project(s) associated with this '.
147                 $ctx->{ibx}->thing_type . "\n";
148         for my $git (@{$ctx->{www}->{pi_cfg}->repo_objs($ctx->{ibx})}) {
149                 for ($git->pub_urls($ctx->{env})) {
150                         my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : $pfx.$_;
151                         $u = ascii_html(prurl($ctx->{env}, $u));
152                         $buf .= qq(\n\t<a\nhref="$u">$u</a>);
153                 }
154         }
155         ($buf);
156 }
157
158 sub _html_end {
159         return '</body></html>';
160         my ($ctx) = @_;
161         my $upfx = $ctx->{-upfx} || '';
162         my $m = "${upfx}_/text/mirror/";
163         my $x = '';
164         if ($ctx->{ibx} && $ctx->{ibx}->can('cloneurl')) {
165                 $x = <<EOF;
166 This is a public inbox, see <a
167 href="$m">mirroring instructions</a>
168 for how to clone and mirror all data and code used for this inbox
169 EOF
170                 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
171                 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
172                 if ($has_nntp || $has_imap) {
173                         substr($x, -1, 1) = ";\n"; # s/\n/;\n
174                         if ($has_nntp && $has_imap) {
175                                 $x .= <<EOM;
176 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
177 EOM
178                         } elsif ($has_nntp) {
179                                 $x .= <<EOM;
180 as well as URLs for NNTP newsgroup(s).
181 EOM
182                         } else {
183                                 $x .= <<EOM;
184 as well as URLs for IMAP folder(s).
185 EOM
186                         }
187                 }
188         } elsif ($ctx->{ibx}) { # extindex
189                 $x = <<EOF;
190 This is an external index of several public inboxes,
191 see <a href="$m">mirroring instructions</a> on how to clone and mirror
192 all data and code used by this external index.
193 EOF
194         } elsif ($ctx->{git}) { # coderepo
195                 $x = join('', map { "git clone $_\n" }
196                         @{$ctx->{git}->cloneurl($ctx->{env})});
197         }
198         chomp $x;
199         '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
200 }
201
202 # callback for HTTP.pm (and any other PSGI servers)
203 sub getline {
204         my ($ctx) = @_;
205         my $cb = $ctx->{cb} or return;
206         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
207                 if (ref($x)) { # smsg
208                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
209                         $ctx->{smsg} = $x;
210                         return $ctx->translate($cb->($ctx, $eml));
211                 } else { # scalar
212                         return $ctx->translate($x);
213                 }
214         }
215         delete $ctx->{cb};
216         $ctx->zflush(_html_end($ctx));
217 }
218
219 sub html_done ($;@) {
220         my $ctx = $_[0];
221         my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
222         my $res_hdr = delete $ctx->{-res_hdr};
223         push @$res_hdr, 'Content-Length', length($bdy);
224         [ 200, $res_hdr, [ $bdy ] ]
225 }
226
227 sub html_oneshot ($$;@) {
228         my ($ctx, $code) = @_[0, 1];
229         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
230                 'Content-Length' => undef ];
231         bless $ctx, __PACKAGE__;
232         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
233         my @top;
234         $ctx->{base_url} // do {
235                 @top = html_top($ctx);
236                 $ctx->{base_url} = base_url($ctx);
237         };
238         my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
239         $res_hdr->[3] = length($bdy);
240         [ $code, $res_hdr, [ $bdy ] ]
241 }
242
243 sub async_next ($) {
244         my ($http) = @_; # PublicInbox::HTTP
245         my $ctx = $http->{forward} or return;
246         eval {
247                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
248                         $ctx->smsg_blob($smsg);
249                 } else {
250                         $ctx->write(_html_end($ctx));
251                         $ctx->close; # GzipFilter->close
252                 }
253         };
254         warn "E: $@" if $@;
255 }
256
257 sub aresponse {
258         my ($ctx, $cb) = @_;
259         init($ctx, $cb);
260         $ctx->psgi_response(200, delete $ctx->{-res_hdr});
261 }
262
263 sub html_init {
264         my ($ctx) = @_;
265         $ctx->{base_url} = base_url($ctx);
266         my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
267         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
268         bless $ctx, __PACKAGE__;
269         print { $ctx->zfh } html_top($ctx);
270 }
271
272 sub DESTROY {
273         my ($ctx) = @_;
274         $ctx->{git}->cleanup if $ctx->{git} && $ctx->{git}->{-tmp};
275 }
276
277 1;