]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www_stream: note existence of IMAP and NNTP URLs
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016-2021 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}->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         bless $ctx, __PACKAGE__;
31 }
32
33 sub async_eml { # for async_blob_cb
34         my ($ctx, $eml) = @_;
35         $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml)));
36 }
37
38 sub html_top ($) {
39         my ($ctx) = @_;
40         my $ibx = $ctx->{ibx};
41         my $desc = ascii_html($ibx->description);
42         my $title = delete($ctx->{-title_html}) // $desc;
43         my $upfx = $ctx->{-upfx} || '';
44         my $atom = $ctx->{-atom} || $upfx.'new.atom';
45         my $top = "<b>$desc</b>";
46         if (my $t_max = $ctx->{-t_max}) {
47                 $t_max = ts2str($t_max);
48                 $top = qq(<a\nhref="$upfx?t=$t_max">$top</a>);
49         # we had some kind of query, link to /$INBOX/?t=YYYYMMDDhhmmss
50         } elsif ($ctx->{qp}->{t}) {
51                 $top = qq(<a\nhref="./">$top</a>);
52         } elsif (length($upfx)) {
53                 $top = qq(<a\nhref="$upfx">$top</a>);
54         }
55         my $code = $ibx->{coderepo} ? qq( / <a\nhref=#code>code</a>) : '';
56         # id=mirror must exist for legacy bookmarks
57         my $links = qq(<a\nhref="${upfx}_/text/help/">help</a> / ).
58                         qq(<a\nhref="${upfx}_/text/color/">color</a> / ).
59                         qq(<a\nid=mirror) .
60                         qq(\nhref="${upfx}_/text/mirror/">mirror</a>$code / ).
61                         qq(<a\nhref="$atom">Atom feed</a>);
62         if ($ibx->isrch) {
63                 my $q_val = delete($ctx->{-q_value_html}) // '';
64                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
65                 # XXX gross, for SearchView.pm
66                 my $extra = delete($ctx->{-extra_form_html}) // '';
67                 my $action = $upfx eq '' ? './' : $upfx;
68                 $top = qq{<form\naction="$action"><pre>$top} .
69                           qq{\n<input\nname=q\ntype=text$q_val />} .
70                           $extra .
71                           qq{<input\ntype=submit\nvalue=search />} .
72                           ' ' . $links .
73                           q{</pre></form>}
74         } else {
75                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
76         }
77         "<html><head><title>$title</title>" .
78                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
79                 qq(href="$atom"\ntype="application/atom+xml"/>) .
80                 $ctx->{www}->style($upfx) .
81                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
82 }
83
84 sub coderepos ($) {
85         my ($ctx) = @_;
86         my $cr = $ctx->{ibx}->{coderepo} // return ();
87         my $cfg = $ctx->{www}->{pi_cfg};
88         my $upfx = ($ctx->{-upfx} // ''). '../';
89         my @ret;
90         for my $cr_name (@$cr) {
91                 $ret[0] //= <<EOF;
92 <a id=code>Code repositories for project(s) associated with this inbox:
93 EOF
94                 my $urls = $cfg->get_all("coderepo.$cr_name.cgiturl");
95                 if ($urls) {
96                         for (@$urls) {
97                                 # relative or absolute URL?, prefix relative
98                                 # "foo.git" with appropriate number of "../"
99                                 my $u = m!\A(?:[a-z\+]+:)?//! ? $_ : $upfx.$_;
100                                 $u = ascii_html(prurl($ctx->{env}, $u));
101                                 $ret[0] .= qq(\n\t<a\nhref="$u">$u</a>);
102                         }
103                 } else {
104                         $ret[0] .= qq[\n\t$cr_name.git (no URL configured)];
105                 }
106         }
107         @ret; # may be empty, this sub is called as an arg for join()
108 }
109
110 sub _html_end {
111         my ($ctx) = @_;
112         my $upfx = $ctx->{-upfx} || '';
113         my $m = "${upfx}_/text/mirror/";
114         my $x;
115         if ($ctx->{ibx}->can('cloneurl')) {
116                 $x = <<EOF;
117 This is a public inbox, see <a
118 href="$m">mirroring instructions</a>
119 for how to clone and mirror all data and code used for this inbox
120 EOF
121                 my $has_nntp = @{$ctx->{ibx}->nntp_url($ctx)};
122                 my $has_imap = @{$ctx->{ibx}->imap_url($ctx)};
123                 if ($has_nntp || $has_imap) {
124                         substr($x, -1, 1) = ";\n"; # s/\n/;\n
125                         if ($has_nntp && $has_imap) {
126                                 $x .= <<EOM;
127 as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).
128 EOM
129                         } elsif ($has_nntp) {
130                                 $x .= <<EOM;
131 as well as URLs for NNTP newsgroup(s).
132 EOM
133                         } else {
134                                 $x .= <<EOM;
135 as well as URLs for IMAP folder(s).
136 EOM
137                         }
138                 }
139         } else {
140                 $x = <<EOF;
141 This is an external index of several public inboxes,
142 see <a href="$m">mirroring instructions</a> on how to clone and mirror
143 all data and code used by this external index.
144 EOF
145         }
146         chomp $x;
147         '<hr><pre>'.join("\n\n", coderepos($ctx), $x).'</pre></body></html>'
148 }
149
150 # callback for HTTP.pm (and any other PSGI servers)
151 sub getline {
152         my ($ctx) = @_;
153         my $cb = $ctx->{cb} or return;
154         while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref
155                 if (ref($x)) { # smsg
156                         my $eml = $ctx->{ibx}->smsg_eml($x) or next;
157                         $ctx->{smsg} = $x;
158                         return $ctx->translate($cb->($ctx, $eml));
159                 } else { # scalar
160                         return $ctx->translate($x);
161                 }
162         }
163         delete $ctx->{cb};
164         $ctx->zflush(_html_end($ctx));
165 }
166
167 sub html_oneshot ($$;$) {
168         my ($ctx, $code, $sref) = @_;
169         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
170                 'Content-Length' => undef ];
171         bless $ctx, __PACKAGE__;
172         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
173         $ctx->{base_url} //= do {
174                 $ctx->zmore(html_top($ctx));
175                 base_url($ctx);
176         };
177         $ctx->zmore($$sref) if $sref;
178         my $bdy = $ctx->zflush(_html_end($ctx));
179         $res_hdr->[3] = length($bdy);
180         [ $code, $res_hdr, [ $bdy ] ]
181 }
182
183 sub async_next ($) {
184         my ($http) = @_; # PublicInbox::HTTP
185         my $ctx = $http->{forward} or return;
186         eval {
187                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
188                         $ctx->smsg_blob($smsg);
189                 } else {
190                         $ctx->{http_out}->write(
191                                         $ctx->translate(_html_end($ctx)));
192                         $ctx->close; # GzipFilter->close
193                 }
194         };
195         warn "E: $@" if $@;
196 }
197
198 sub aresponse {
199         my ($ctx, $code, $cb) = @_;
200         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
201         init($ctx, $cb);
202         $ctx->psgi_response($code, $res_hdr);
203 }
204
205 1;