]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
f5b4df9fe061886b08bca636052b5e02b8f9472f
[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 $cfg = $ctx->{www}->{pi_cfg};
94         my $upfx = ($ctx->{-upfx} // ''). '../';
95         my $pfx = $ctx->{base_url} //= $ctx->base_url;
96         my $up = $upfx =~ tr!/!/!;
97         $pfx =~ s!/[^/]+\z!/! for (1..$up);
98         my @ret = ('<a id=code>' .
99                 'Code repositories for project(s) associated with this '.
100                 $ctx->{ibx}->thing_type . "\n");
101         my $objs = $cfg->repo_objs($ctx->{ibx});
102         for my $git (@$objs) {
103                 my @urls = $git->pub_urls($ctx->{env});
104                 for (@urls) {
105                         my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $pfx.$_;
106                         $u = ascii_html(prurl($ctx->{env}, $u));
107                         $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
108                 }
109         }
110         @ret; # may be empty, this sub is called as an arg for join()
111 }
112
113 sub _html_end {
114         my ($ctx) = @_;
115         my $upfx = $ctx->{-upfx} || '';
116         my $m = "${upfx}_/text/mirror/";
117         my $x = '';
118         if ($ctx->{ibx} && $ctx->{ibx}->can('cloneurl')) {
119                 $x = <<EOF;
120 This is a public inbox, see <a
121 href="$m">mirroring instructions</a>
122 for how to clone and mirror all data and code used for this inbox
123 EOF
124                 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
125                 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
126                 if ($has_nntp || $has_imap) {
127                         substr($x, -1, 1) = ";\n"; # s/\n/;\n
128                         if ($has_nntp && $has_imap) {
129                                 $x .= <<EOM;
130 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
131 EOM
132                         } elsif ($has_nntp) {
133                                 $x .= <<EOM;
134 as well as URLs for NNTP newsgroup(s).
135 EOM
136                         } else {
137                                 $x .= <<EOM;
138 as well as URLs for IMAP folder(s).
139 EOM
140                         }
141                 }
142         } elsif ($ctx->{ibx}) { # extindex
143                 $x = <<EOF;
144 This is an external index of several public inboxes,
145 see <a href="$m">mirroring instructions</a> on how to clone and mirror
146 all data and code used by this external index.
147 EOF
148         } elsif ($ctx->{git}) { # coderepo
149                 $x = join('', map { "git clone $_\n" }
150                         @{$ctx->{git}->cloneurl($ctx->{env})});
151         }
152         chomp $x;
153         '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
154 }
155
156 # callback for HTTP.pm (and any other PSGI servers)
157 sub getline {
158         my ($ctx) = @_;
159         my $cb = $ctx->{cb} or return;
160         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
161                 if (ref($x)) { # smsg
162                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
163                         $ctx->{smsg} = $x;
164                         return $ctx->translate($cb->($ctx, $eml));
165                 } else { # scalar
166                         return $ctx->translate($x);
167                 }
168         }
169         delete $ctx->{cb};
170         $ctx->zflush(_html_end($ctx));
171 }
172
173 sub html_done ($;@) {
174         my $ctx = $_[0];
175         my $bdy = $ctx->zflush(@_[1..$#_], _html_end($ctx));
176         my $res_hdr = delete $ctx->{-res_hdr};
177         push @$res_hdr, 'Content-Length', length($bdy);
178         [ 200, $res_hdr, [ $bdy ] ]
179 }
180
181 sub html_oneshot ($$;@) {
182         my ($ctx, $code) = @_[0, 1];
183         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
184                 'Content-Length' => undef ];
185         bless $ctx, __PACKAGE__;
186         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
187         my @top;
188         $ctx->{base_url} // do {
189                 @top = html_top($ctx);
190                 $ctx->{base_url} = base_url($ctx);
191         };
192         my $bdy = $ctx->zflush(@top, @_[2..$#_], _html_end($ctx));
193         $res_hdr->[3] = length($bdy);
194         [ $code, $res_hdr, [ $bdy ] ]
195 }
196
197 sub async_next ($) {
198         my ($http) = @_; # PublicInbox::HTTP
199         my $ctx = $http->{forward} or return;
200         eval {
201                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
202                         $ctx->smsg_blob($smsg);
203                 } else {
204                         $ctx->write(_html_end($ctx));
205                         $ctx->close; # GzipFilter->close
206                 }
207         };
208         warn "E: $@" if $@;
209 }
210
211 sub aresponse {
212         my ($ctx, $cb) = @_;
213         init($ctx, $cb);
214         $ctx->psgi_response(200, delete $ctx->{-res_hdr});
215 }
216
217 sub html_init {
218         my ($ctx) = @_;
219         $ctx->{base_url} = base_url($ctx);
220         my $h = $ctx->{-res_hdr} = ['Content-Type', 'text/html; charset=UTF-8'];
221         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($h, $ctx->{env});
222         bless $ctx, __PACKAGE__;
223         print { $ctx->zfh } html_top($ctx);
224 }
225
226 sub DESTROY {
227         my ($ctx) = @_;
228         $ctx->{git}->cleanup if $ctx->{git} && $ctx->{git}->{-tmp};
229 }
230
231 1;