1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # HTML body stream for which yields getline+close methods
5 package PublicInbox::WwwStream;
8 use PublicInbox::Hval qw(ascii_html);
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';
18 my ($class, $ctx, $cb) = @_;
19 bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
23 my ($class, $ctx, $code, $cb) = @_;
24 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
25 $class->new($ctx, $cb) ]
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>";
42 my $q_val = $ctx->{-q_value_html};
43 if (defined $q_val && $q_val ne '') {
44 $q_val = qq(\nvalue="$q_val");
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 />} .
54 qq{<input\ntype=submit\nvalue=search />} .
58 $top = '<pre>' . $top . "\n" . $links . '</pre>';
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;
69 my $urls = 'Archives are clonable:';
70 my $ctx = $self->{ctx};
71 my $obj = $ctx->{-inbox};
72 my $desc = ascii_html($obj->description);
74 my $http = $obj->base_url($ctx->{env});
76 my %seen = ( $http => 1 );
78 foreach my $u (@{$obj->cloneurl}) {
81 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
83 if (scalar(@urls) == 1) {
84 $urls .= " git clone --mirror $http";
87 join("\n", map { "\tgit clone --mirror $_" } @urls);
90 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
93 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
94 $urls .= ' available over NNTP:';
95 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
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>];
101 $urls .= "\n or Tor2web: ";
102 $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
105 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
106 '<hr><pre>'.join("\n\n",
109 'Code for this site: '.
110 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
111 ).'</pre></body></html>';
116 my $nr = $self->{nr}++;
118 return _html_top($self) if $nr == 0;
120 if (my $mid = $self->{cb}) { # middle
121 $mid = $mid->($nr, $self->{ctx}) and return $mid;
124 delete $self->{cb} ? _html_end($self) : undef;