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