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';
15 # noop for HTTP.pm (and any other PSGI servers)
19 my ($class, $ctx, $cb) = @_;
20 bless { nr => 0, cb => $cb || *close, ctx => $ctx }, $class;
24 my ($class, $ctx, $code, $cb) = @_;
25 [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
26 $class->new($ctx, $cb) ]
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>";
43 my $q_val = $ctx->{-q_value_html};
44 if (defined $q_val && $q_val ne '') {
45 $q_val = qq(\nvalue="$q_val");
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 />} .
55 qq{<input\ntype=submit\nvalue=search />} .
59 $top = '<pre>' . $top . "\n" . $links . '</pre>';
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;
70 my $urls = 'Archives are clonable:';
71 my $ctx = $self->{ctx};
72 my $obj = $ctx->{-inbox};
73 my $desc = ascii_html($obj->description);
75 my $http = $obj->base_url($ctx->{env});
77 my %seen = ( $http => 1 );
79 foreach my $u (@{$obj->cloneurl}) {
82 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
84 if (scalar(@urls) == 1) {
85 $urls .= " git clone --mirror $http";
88 join("\n", map { "\tgit clone --mirror $_" } @urls);
91 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
94 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
95 $urls .= ' available over NNTP:';
96 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
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>];
102 $urls .= "\n or Tor2web: ";
103 $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
106 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
107 '<hr><pre>'.join("\n\n",
110 'AGPL code for this site: '.
111 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
112 ).'</pre></body></html>';
115 # callback for HTTP.pm (and any other PSGI servers)
118 my $nr = $self->{nr}++;
120 return _html_top($self) if $nr == 0;
122 if (my $middle = $self->{cb}) {
123 $middle = $middle->($nr, $self->{ctx}) and return $middle;
126 delete $self->{cb} ? _html_end($self) : undef;