]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: improve navigation around contemporary threads
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016-2020 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 parent qw(Exporter PublicInbox::GzipFilter);
11 our @EXPORT_OK = qw(html_oneshot);
12 use bytes (); # length
13 use PublicInbox::Hval qw(ascii_html prurl ts2str);
14 our $TOR_URL = 'https://www.torproject.org/';
15 our $CODE_URL = 'https://public-inbox.org/public-inbox.git';
16
17 sub base_url ($) {
18         my $ctx = shift;
19         my $base_url = $ctx->{-inbox}->base_url($ctx->{env});
20         chop $base_url; # no trailing slash for clone
21         $base_url;
22 }
23
24 sub init {
25         my ($ctx, $cb) = @_;
26         $ctx->{cb} = $cb;
27         $ctx->{base_url} = base_url($ctx);
28         bless $ctx, __PACKAGE__;
29 }
30
31 sub async_eml { # for async_blob_cb
32         my ($ctx, $eml) = @_;
33         $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml)));
34 }
35
36 sub html_top ($) {
37         my ($ctx) = @_;
38         my $ibx = $ctx->{-inbox};
39         my $desc = ascii_html($ibx->description);
40         my $title = delete($ctx->{-title_html}) // $desc;
41         my $upfx = $ctx->{-upfx} || '';
42         my $help = $upfx.'_/text/help';
43         my $color = $upfx.'_/text/color';
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         }
53         my $links = "<a\nhref=\"$help\">help</a> / ".
54                         "<a\nhref=\"$color\">color</a> / ".
55                         "<a\nhref=\"$atom\">Atom feed</a>";
56         if ($ibx->search) {
57                 my $q_val = delete($ctx->{-q_value_html}) // '';
58                 $q_val = qq(\nvalue="$q_val") if $q_val ne '';
59                 # XXX gross, for SearchView.pm
60                 my $extra = delete($ctx->{-extra_form_html}) // '';
61                 my $action = $upfx eq '' ? './' : $upfx;
62                 $top = qq{<form\naction="$action"><pre>$top} .
63                           qq{\n<input\nname=q\ntype=text$q_val />} .
64                           $extra .
65                           qq{<input\ntype=submit\nvalue=search />} .
66                           ' ' . $links .
67                           q{</pre></form>}
68         } else {
69                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
70         }
71         "<html><head><title>$title</title>" .
72                 qq(<link\nrel=alternate\ntitle="Atom feed"\n).
73                 qq(href="$atom"\ntype="application/atom+xml"/>) .
74                 $ctx->{www}->style($upfx) .
75                 '</head><body>'. $top . (delete($ctx->{-html_tip}) // '');
76 }
77
78 sub code_footer ($) {
79         my ($env) = @_;
80         my $u = prurl($env, $CODE_URL);
81         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a>)
82 }
83
84 sub _html_end {
85         my ($ctx) = @_;
86         my $urls = 'Archives are clonable:';
87         my $ibx = $ctx->{-inbox};
88         my $desc = ascii_html($ibx->description);
89
90         my @urls;
91         my $http = $ctx->{base_url};
92         my $max = $ibx->max_git_epoch;
93         my $dir = (split(m!/!, $http))[-1];
94         my %seen = ($http => 1);
95         if (defined($max)) { # v2
96                 for my $i (0..$max) {
97                         # old parts my be deleted:
98                         -d "$ibx->{inboxdir}/git/$i.git" or next;
99                         my $url = "$http/$i";
100                         $seen{$url} = 1;
101                         push @urls, "$url $dir/git/$i.git";
102                 }
103         } else { # v1
104                 push @urls, $http;
105         }
106
107         # FIXME: epoch splits can be different in other repositories,
108         # use the "cloneurl" file as-is for now:
109         foreach my $u (@{$ibx->cloneurl}) {
110                 next if $seen{$u}++;
111                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
112         }
113
114         if (defined($max) || scalar(@urls) > 1) {
115                 $urls .= "\n" .
116                         join("\n", map { "\tgit clone --mirror $_" } @urls);
117         } else {
118                 $urls .= " git clone --mirror $urls[0]";
119         }
120         if (defined $max) {
121                 my $addrs = $ibx->{address};
122                 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
123                 $urls .=  <<EOF
124
125
126         # If you have public-inbox 1.1+ installed, you may
127         # initialize and index your mirror using the following commands:
128         public-inbox-init -V2 $ibx->{name} $dir/ $http \\
129                 $addrs
130         public-inbox-index $dir
131 EOF
132         } else { # v1
133                 $urls .= "\n";
134         }
135
136         my $cfg_link = ($ctx->{-upfx} // '').'_/text/config/raw';
137         $urls .= qq(\nExample <a\nhref="$cfg_link">config snippet</a> for mirrors\n);
138         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
139         if (@nntp) {
140                 $urls .= "\n";
141                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
142                 $urls .= ' available over NNTP:';
143                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
144         }
145         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
146                 $urls .= "\n note: .onion URLs require Tor: ";
147                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
148         }
149         '<hr><pre>'.join("\n\n",
150                 $desc,
151                 $urls,
152                 code_footer($ctx->{env})
153         ).'</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->{-inbox}->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_oneshot ($$;$) {
174         my ($ctx, $code, $sref) = @_;
175         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8',
176                 'Content-Length' => undef ];
177         bless $ctx, __PACKAGE__;
178         $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res_hdr, $ctx->{env});
179         $ctx->{base_url} //= do {
180                 $ctx->zmore(html_top($ctx));
181                 base_url($ctx);
182         };
183         $ctx->zmore($$sref) if $sref;
184         my $bdy = $ctx->zflush(_html_end($ctx));
185         $res_hdr->[3] = bytes::length($bdy);
186         [ $code, $res_hdr, [ $bdy ] ]
187 }
188
189 sub async_next ($) {
190         my ($http) = @_; # PublicInbox::HTTP
191         my $ctx = $http->{forward} or return;
192         eval {
193                 if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
194                         $ctx->smsg_blob($smsg);
195                 } else {
196                         $ctx->{http_out}->write(
197                                         $ctx->translate(_html_end($ctx)));
198                         $ctx->close; # GzipFilter->close
199                 }
200         };
201         warn "E: $@" if $@;
202 }
203
204 sub aresponse {
205         my ($ctx, $code, $cb) = @_;
206         my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ];
207         init($ctx, $cb);
208         $ctx->psgi_response($code, $res_hdr);
209 }
210
211 1;