]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
59edad5de5bd4238212b45f06c2b174f4e6ed7ae
[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_top ($) {
42         my ($ctx) = @_;
43         my $ibx = $ctx->{ibx} // $ctx->{git};
44         my $desc = ascii_html($ibx->description);
45         my $title = delete($ctx->{-title_html}) // $desc;
46         my $upfx = $ctx->{-upfx} || '';
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         } elsif (length($upfx)) {
56                 $top = qq(<a\nhref="$upfx">$top</a>);
57         }
58         my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
59         # id=mirror must exist for legacy bookmarks
60         my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
61                         qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
62                         qq(<a\nid=mirror) .
63                         qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
64                         qq(<a\nhref="$atom">Atom feed</a>);
65         if ($ibx->isrch) {
66                 my $q_val = delete($ctx->{-q_value_html}) // '';
67                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
68                 # XXX gross, for SearchView.pm
69                 my $extra = delete($ctx->{-extra_form_html}) // '';
70                 my $action = $upfx eq '' ? './' : $upfx;
71                 $top = qq{<form\naction="$action"><pre>$top} .
72                           qq{\n<input\nname=q\ntype=text$q_val />} .
73                           $extra .
74                           qq{<input\ntype=submit\nvalue=search />} .
75                           ' ' . $links .
76                           q{</pre></form>}
77         } else {
78                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
79         }
80         "<html><head><title>$title</title>" .
81                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
82                 qq(href="$atom"\ntype="application/atom+xml"/>) .
83                 $ctx->{www}->style($upfx) .
84                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
85 }
86
87 sub inboxes { () } # TODO
88
89 sub coderepos ($) {
90         my ($ctx) = @_;
91         $ctx->{ibx} // return inboxes($ctx);
92         my $cr = $ctx->{ibx}->{coderepo} // return ();
93         my $upfx = ($ctx->{-upfx} // ''). '../';
94         my $pfx = $ctx->{base_url} //= $ctx->base_url;
95         my $up = $upfx =~ tr!/!/!;
96         $pfx =~ s!/[^/]+\z!/! for (1..$up);
97         $pfx .= '/' if substr($pfx, -1, 1) ne '/';
98         my $buf = '<a id=code>' .
99                 'Code repositories for project(s) associated with this '.
100                 $ctx->{ibx}->thing_type . "\n";
101         for my $git (@{$ctx->{www}->{pi_cfg}->repo_objs($ctx->{ibx})}) {
102                 for ($git->pub_urls($ctx->{env})) {
103                         my $u = m!\A(?:[a-z\+]+:)?//!i ? $_ : $pfx.$_;
104                         $u = ascii_html(prurl($ctx->{env}, $u));
105                         $buf .= qq(\n\t<a\nhref="$u">$u</a>);
106                 }
107         }
108         ($buf);
109 }
110
111 sub _html_end {
112         my ($ctx) = @_;
113         my $upfx = $ctx->{-upfx} || '';
114         my $m = "${upfx}_/text/mirror/";
115         my $x = '';
116         if ($ctx->{ibx} && $ctx->{ibx}->can('cloneurl')) {
117                 $x = <<EOF;
118 This is a public inbox, see <a
119 href="$m">mirroring instructions</a>
120 for how to clone and mirror all data and code used for this inbox
121 EOF
122                 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
123                 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
124                 if ($has_nntp || $has_imap) {
125                         substr($x, -1, 1) = ";\n"; # s/\n/;\n
126                         if ($has_nntp && $has_imap) {
127                                 $x .= <<EOM;
128 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
129 EOM
130                         } elsif ($has_nntp) {
131                                 $x .= <<EOM;
132 as well as URLs for NNTP newsgroup(s).
133 EOM
134                         } else {
135                                 $x .= <<EOM;
136 as well as URLs for IMAP folder(s).
137 EOM
138                         }
139                 }
140         } elsif ($ctx->{ibx}) { # extindex
141                 $x = <<EOF;
142 This is an external index of several public inboxes,
143 see <a href="$m">mirroring instructions</a> on how to clone and mirror
144 all data and code used by this external index.
145 EOF
146         } elsif ($ctx->{git}) { # coderepo
147                 $x = join('', map { "git clone $_\n" }
148                         @{$ctx->{git}->cloneurl($ctx->{env})});
149         }
150         chomp $x;
151         '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
152 }
153
154 # callback for HTTP.pm (and any other PSGI servers)
155 sub getline {
156         my ($ctx) = @_;
157         my $cb = $ctx->{cb} or return;
158         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
159                 if (ref($x)) { # smsg
160                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
161                         $ctx->{smsg} = $x;
162                         return $ctx->translate($cb->($ctx, $eml));
163                 } else { # scalar
164                         return $ctx->translate($x);
165                 }
166         }
167         delete $ctx->{cb};
168         $ctx->zflush(_html_end($ctx));
169 }
170
171 sub html_done ($;@) {
172         my $ctx = $_[0];
173         my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
174         my $res_hdr = delete $ctx->{-res_hdr};
175         push @$res_hdr, 'Content-Length', length($bdy);
176         [ 200, $res_hdr, [ $bdy ] ]
177 }
178
179 sub html_oneshot ($$;@) {
180         my ($ctx, $code) = @_[0, 1];
181         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
182                 'Content-Length' => undef ];
183         bless $ctx, __PACKAGE__;
184         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
185         my @top;
186         $ctx->{base_url} // do {
187                 @top = html_top($ctx);
188                 $ctx->{base_url} = base_url($ctx);
189         };
190         my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
191         $res_hdr->[3] = length($bdy);
192         [ $code, $res_hdr, [ $bdy ] ]
193 }
194
195 sub async_next ($) {
196         my ($http) = @_; # PublicInbox::HTTP
197         my $ctx = $http->{forward} or return;
198         eval {
199                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
200                         $ctx->smsg_blob($smsg);
201                 } else {
202                         $ctx->write(_html_end($ctx));
203                         $ctx->close; # GzipFilter->close
204                 }
205         };
206         warn "E: $@" if $@;
207 }
208
209 sub aresponse {
210         my ($ctx, $cb) = @_;
211         init($ctx, $cb);
212         $ctx->psgi_response(200, delete $ctx->{-res_hdr});
213 }
214
215 sub html_init {
216         my ($ctx) = @_;
217         $ctx->{base_url} = base_url($ctx);
218         my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
219         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
220         bless $ctx, __PACKAGE__;
221         print { $ctx->zfh } html_top($ctx);
222 }
223
224 sub DESTROY {
225         my ($ctx) = @_;
226         $ctx->{git}->cleanup if $ctx->{git} && $ctx->{git}->{-tmp};
227 }
228
229 1;