]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: support listing of inboxes
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016-2018 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
5 #
6 # public-inbox-httpd favors "getline" response bodies to take a
7 # "pull"-based approach to feeding slow clients (as opposed to a
8 # more common "push" model)
9 package PublicInbox::WwwStream;
10 use strict;
11 use warnings;
12 use PublicInbox::Hval qw(ascii_html);
13 use URI;
14 our $TOR_URL = 'https://www.torproject.org/';
15 our $TOR2WEB_URL = 'https://www.tor2web.org/';
16 our $CODE_URL = 'https://public-inbox.org/';
17 our $PROJECT = 'public-inbox';
18
19 # noop for HTTP.pm (and any other PSGI servers)
20 sub close {}
21
22 sub new {
23         my ($class, $ctx, $cb) = @_;
24         bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
25 }
26
27 sub response {
28         my ($class, $ctx, $code, $cb) = @_;
29         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
30           $class->new($ctx, $cb) ]
31 }
32
33 sub _html_top ($) {
34         my ($self) = @_;
35         my $ctx = $self->{ctx};
36         my $ibx = $ctx->{-inbox};
37         my $desc = ascii_html($ibx->description);
38         my $title = $ctx->{-title_html} || $desc;
39         my $upfx = $ctx->{-upfx} || '';
40         my $help = $upfx.'_/text/help';
41         my $color = $upfx.'_/text/color';
42         my $atom = $ctx->{-atom} || $upfx.'new.atom';
43         my $tip = $ctx->{-html_tip} || '';
44         my $top = "<b>$desc</b>";
45         my $links = "<a\nhref=\"$help\">help</a> / ".
46                         "<a\nhref=\"$color\">color</a> / ".
47                         "<a\nhref=\"$atom\">Atom feed</a>";
48         if ($ibx->search) {
49                 my $q_val = $ctx->{-q_value_html};
50                 if (defined $q_val && $q_val ne '') {
51                         $q_val = qq(\nvalue="$q_val");
52                 } else {
53                         $q_val = '';
54                 }
55                 # XXX gross, for SearchView.pm
56                 my $extra = $ctx->{-extra_form_html} || '';
57                 my $action = $upfx eq '' ? './' : $upfx;
58                 $top = qq{<form\naction="$action"><pre>$top} .
59                           qq{\n<input\nname=q\ntype=text$q_val />} .
60                           $extra .
61                           qq{<input\ntype=submit\nvalue=search />} .
62                           ' ' . $links .
63                           q{</pre></form>}
64         } else {
65                 $top = '<pre>' . $top . "\n" . $links . '</pre>';
66         }
67         "<html><head><title>$title</title>" .
68                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
69                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
70                 $ctx->{www}->style($upfx) .
71                 "</head><body>". $top . $tip;
72 }
73
74 sub code_footer ($) {
75         my ($env) = @_;
76         my $u = PublicInbox::Hval::prurl($env, $CODE_URL);
77         qq(AGPL code for this site: git clone <a\nhref="$u">$u</a> $PROJECT)
78 }
79
80 sub _html_end {
81         my ($self) = @_;
82         my $urls = 'Archives are clonable:';
83         my $ctx = $self->{ctx};
84         my $ibx = $ctx->{-inbox};
85         my $desc = ascii_html($ibx->description);
86
87         my (%seen, @urls);
88         my $http = $ibx->base_url($ctx->{env});
89         chop $http; # no trailing slash for clone
90         my $part = $ibx->max_git_part;
91         my $dir = (split(m!/!, $http))[-1];
92         if (defined($part)) { # v2
93                 $seen{$http} = 1;
94                 for my $i (0..$part) {
95                         # old parts my be deleted:
96                         -d "$ibx->{mainrepo}/git/$i.git" or next;
97                         my $url = "$http/$i";
98                         $seen{$url} = 1;
99                         push @urls, "$url $dir/git/$i.git";
100                 }
101         } else { # v1
102                 $seen{$http} = 1;
103                 push @urls, $http;
104         }
105
106         # FIXME: partitioning in can be different in other repositories,
107         # use the "cloneurl" file as-is for now:
108         foreach my $u (@{$ibx->cloneurl}) {
109                 next if $seen{$u};
110                 $seen{$u} = 1;
111                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
112         }
113
114         if (defined($part) || 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 $part) {
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         }
133         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$ibx->nntp_url};
134         if (@nntp) {
135                 $urls .= "\n\n";
136                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
137                 $urls .= ' available over NNTP:';
138                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
139         }
140         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
141                 $urls .= "\n note: .onion URLs require Tor: ";
142                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
143                 if ($TOR2WEB_URL) {
144                         $urls .= "\n       or Tor2web: ";
145                         $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
146                 }
147         }
148         '<hr><pre>'.join("\n\n",
149                 $desc,
150                 $urls,
151                 code_footer($ctx->{env})
152         ).'</pre></body></html>';
153 }
154
155 # callback for HTTP.pm (and any other PSGI servers)
156 sub getline {
157         my ($self) = @_;
158         my $nr = $self->{nr}++;
159
160         return _html_top($self) if $nr == 0;
161
162         if (my $middle = $self->{cb}) {
163                 $middle = $middle->($nr, $self->{ctx}) and return $middle;
164         }
165
166         delete $self->{cb} ? _html_end($self) : undef;
167 }
168
169 1;