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>
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);
76 my $http = $obj->base_url($ctx->{env});
77 chop $http; # no trailing slash for clone
78 my $part = $obj->max_git_part;
79 my $dir = (split(m!/!, $http))[-1];
80 if (defined($part)) { # v2
82 for my $i (0..$part) {
83 # old parts my be deleted:
84 -d "$obj->{mainrepo}/git/$i.git" or next;
87 push @urls, "$url $dir/git/$i.git";
94 # FIXME: partitioning in can be different in other repositories,
95 # use the "cloneurl" file as-is for now:
96 foreach my $u (@{$obj->cloneurl}) {
99 push @urls, $u =~ /\Ahttps?:/ ? qq(<a\nhref="$u">$u</a>) : $u;
102 if (scalar(@urls) == 1) {
103 $urls .= " git clone --mirror $urls[0]";
106 join("\n", map { "\tgit clone --mirror $_" } @urls);
109 my $addrs = $obj->{address};
110 $addrs = join(' ', @$addrs) if ref($addrs) eq 'ARRAY';
114 # If you have public-inbox 1.1+ installed, you may
115 # initialize and index your mirror using the following commands:
116 public-inbox-init -V2 $obj->{name} $dir/ $http \\
118 public-inbox-index $dir
121 my @nntp = map { qq(<a\nhref="$_">$_</a>) } @{$obj->nntp_url};
124 $urls .= @nntp == 1 ? 'Newsgroup' : 'Newsgroups are';
125 $urls .= ' available over NNTP:';
126 $urls .= "\n\t" . join("\n\t", @nntp) . "\n";
128 if ($urls =~ m!\b[^:]+://\w+\.onion/!) {
129 $urls .= "\n note: .onion URLs require Tor: ";
130 $urls .= qq[<a\nhref="$TOR_URL">$TOR_URL</a>];
132 $urls .= "\n or Tor2web: ";
133 $urls .= qq[<a\nhref="$TOR2WEB_URL">$TOR2WEB_URL</a>];
136 my $url = PublicInbox::Hval::prurl($ctx->{env}, $CODE_URL);
137 '<hr><pre>'.join("\n\n",
140 'AGPL code for this site: '.
141 qq(git clone <a\nhref="$url">$url</a> $PROJECT)
142 ).'</pre></body></html>';
145 # callback for HTTP.pm (and any other PSGI servers)
148 my $nr = $self->{nr}++;
150 return _html_top($self) if $nr == 0;
152 if (my $middle = $self->{cb}) {
153 $middle = $middle->($nr, $self->{ctx}) and return $middle;
156 delete $self->{cb} ? _html_end($self) : undef;