]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
www: a note .onion URLs require the usage of Tor
[public-inbox.git] / lib / PublicInbox / WwwStream.pm
1 # Copyright (C) 2016 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 use constant PI_URL => 'https://public-inbox.org/';
11 our $TOR_URL = 'https://www.torproject.org/';
12
13 sub close {}
14
15 sub new {
16         my ($class, $ctx, $cb) = @_;
17         bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
18 }
19
20 sub response {
21         my ($class, $ctx, $code, $cb) = @_;
22         [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
23           $class->new($ctx, $cb) ]
24 }
25
26 sub _html_top ($) {
27         my ($self) = @_;
28         my $ctx = $self->{ctx};
29         my $obj = $ctx->{-inbox};
30         my $desc = ascii_html($obj->description);
31         my $title = $ctx->{-title_html} || $desc;
32         my $upfx = $ctx->{-upfx} || '';
33         my $atom = $ctx->{-atom} || $upfx.'new.atom';
34         my $tip = $ctx->{-html_tip} || '';
35         my $top = "<b>$desc</b> (<a\nhref=\"$atom\">Atom feed</a>)";
36         if ($obj->search) {
37                 my $q_val = $ctx->{-q_value_html};
38                 if (defined $q_val && $q_val ne '') {
39                         $q_val = qq(\nvalue="$q_val" );
40                 } else {
41                         $q_val = '';
42                 }
43                 # XXX gross, for SearchView.pm
44                 my $extra = $ctx->{-extra_form_html} || '';
45                 my $action = $upfx eq '' ? './' : $upfx;
46                 $top = qq{<form\naction="$action"><pre>$top} .
47                           qq{ <input\nname=q\ntype=text$q_val/>} .
48                           $extra .
49                           qq{<input\ntype=submit\nvalue=search />} .
50                           q{</pre></form>}
51         } else {
52                 $top = '<pre>' . $top . '</pre>';
53         }
54         "<html><head><title>$title</title>" .
55                 "<link\nrel=alternate\ntitle=\"Atom feed\"\n".
56                 "href=\"$atom\"\ntype=\"application/atom+xml\"/>" .
57                 PublicInbox::Hval::STYLE .
58                 "</head><body>". $top . $tip;
59 }
60
61 sub _html_end {
62         my ($self) = @_;
63         my $urls = 'Archives are clonable:';
64         my $ctx = $self->{ctx};
65         my $obj = $ctx->{-inbox};
66         my $desc = ascii_html($obj->description);
67
68         my $http = $obj->base_url($ctx->{env});
69         chop $http;
70         my %seen = ( $http => 1 );
71         my @urls = ($http);
72         my $need_tor;
73         foreach my $u (@{$obj->cloneurl}) {
74                 next if $seen{$u};
75                 $need_tor = 1 if $u =~ m!\A[^:]+://\w+\.onion/!;
76                 $seen{$u} = 1;
77                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
78         }
79         if (scalar(@urls) == 1) {
80                 $urls .= " git clone --mirror $http";
81         } else {
82                 $urls .= "\n" .
83                         join("\n", map { "\tgit clone --mirror $_" } @urls);
84         }
85
86         if ($need_tor) {
87                 $urls .= "\n note: .onion URLs require Tor: ";
88                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
89         }
90         my $url = PublicInbox::Hval::prurl($ctx->{env}, PI_URL);
91         '<hr><pre>'.join("\n\n",
92                 $desc,
93                 $urls,
94                 'Served with public-inbox: '.
95                 qq(git clone <a\nhref="$url">$url</a> public-inbox)
96         ).'</pre></body></html>';
97 }
98
99 sub getline {
100         my ($self) = @_;
101         my $nr = $self->{nr}++;
102
103         return _html_top($self) if $nr == 0;
104
105         if (my $mid = $self->{cb}) { # middle
106                 $mid = $mid->($nr, $self->{ctx}) and return $mid;
107         }
108
109         delete $self->{cb} ? _html_end($self) : undef;
110 }
111
112 1;