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