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