]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwStream.pm
update copyrights for 2018
[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 $http = $obj->base_url($ctx->{env});
76         chop $http;
77         my %seen = ( $http => 1 );
78         my @urls = ($http);
79         foreach my $u (@{$obj->cloneurl}) {
80                 next if $seen{$u};
81                 $seen{$u} = 1;
82                 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
83         }
84         if (scalar(@urls) == 1) {
85                 $urls .= " git clone --mirror $http";
86         } else {
87                 $urls .= "\n" .
88                         join("\n", map { "\tgit clone --mirror $_" } @urls);
89         }
90
91         my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
92         if (@nntp) {
93                 $urls .= "\n\n";
94                 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
95                 $urls .= ' available over NNTP:';
96                 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
97         }
98         if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
99                 $urls .= "\n note: .onion URLs require Tor: ";
100                 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
101                 if ($TOR2WEB_URL) {
102                         $urls .= "\n       or Tor2web: ";
103                         $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
104                 }
105         }
106         my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
107         '<hr><pre>'.join("\n\n",
108                 $desc,
109                 $urls,
110                 'AGPL code for this site: '.
111                 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
112         ).'</pre></body></html>';
113 }
114
115 # callback for HTTP.pm (and any other PSGI servers)
116 sub getline {
117         my ($self) = @_;
118         my $nr = $self->{nr}++;
119
120         return _html_top($self) if $nr == 0;
121
122         if (my $middle = $self->{cb}) {
123                 $middle = $middle->($nr, $self->{ctx}) and return $middle;
124         }
125
126         delete $self->{cb} ? _html_end($self) : undef;
127 }
128
129 1;