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