1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # We focus on the lowest common denominators here:
5 # - targeted at text-only console browsers (lynx, w3m, etc..)
6 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
7 # - No JavaScript, graphics or icons allowed.
8 # - Must not rely on static content
9 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
10 package PublicInbox::WWW;
14 use PublicInbox::Config;
15 use URI::Escape qw(uri_escape_utf8 uri_unescape);
16 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
17 use constant PI_URL => 'http://public-inbox.org/';
18 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
21 $pi_config = PublicInbox::Config->new;
25 my ($cgi, $method) = @_;
27 if ($method !~ /\AGET|HEAD\z/) {
28 return r(405, 'Method Not Allowed');
30 my $path_info = $cgi->path_info;
32 # top-level indices and feeds
33 if ($path_info eq '/') {
35 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
36 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
37 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
38 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
39 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
40 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
42 # single-message pages
43 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
44 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
45 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
46 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
49 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
50 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
52 # convenience redirects, order matters
53 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
54 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
61 # for CoW-friendliness, MOOOOO!
63 require PublicInbox::Feed;
64 require PublicInbox::View;
65 require PublicInbox::Thread;
69 require XML::Atom::SimpleFeed;
72 # private functions below
74 sub r404 { r(404, 'Not Found') }
76 # simple response for errors
77 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
79 # returns undef if valid, array ref response if invalid
81 my ($ctx, $listname) = @_;
82 my $git_dir = $pi_config->get($listname, "mainrepo");
83 if (defined $git_dir) {
84 $ctx->{git_dir} = $git_dir;
85 $ctx->{listname} = $listname;
91 # returns undef if valid, array ref response if invalid
92 sub invalid_list_mid {
93 my ($ctx, $listname, $mid) = @_;
94 my $ret = invalid_list($ctx, $listname, $mid);
95 $ctx->{mid} = uri_unescape($mid) unless $ret;
99 # /$LISTNAME/atom.xml -> Atom feed, includes replies
101 my ($ctx, $cgi, $top) = @_;
102 require PublicInbox::Feed;
103 [ 200, [ 'Content-Type' => 'application/xml' ],
104 [ PublicInbox::Feed->generate({
105 git_dir => $ctx->{git_dir},
106 listname => $ctx->{listname},
107 pi_config => $pi_config,
114 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
116 my ($ctx, $cgi, $top) = @_;
117 require PublicInbox::Feed;
118 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
119 [ PublicInbox::Feed->generate_html_index({
120 git_dir => $ctx->{git_dir},
121 listname => $ctx->{listname},
122 pi_config => $pi_config,
124 footer => footer($ctx),
130 # just returns a string ref for the blob in the current ctx
134 my $hex = Digest::SHA::sha1_hex($ctx->{mid});
135 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
136 die "BUG: not a SHA-1 hex: $hex";
138 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
139 qw(cat-file blob), "HEAD:$1/$2");
140 my $cmd = join(' ', @cmd);
141 my $pid = open my $fh, '-|';
142 defined $pid or die "fork failed: $!\n";
144 open STDERR, '>', '/dev/null'; # ignore errors
145 exec @cmd or die "exec failed: $!\n";
147 my $blob = eval { local $/; <$fh> };
149 $? == 0 ? \$blob : undef;
153 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
155 my ($ctx, $cgi) = @_;
156 my $x = mid2blob($ctx);
157 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
160 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
162 my ($ctx, $cgi) = @_;
163 my $x = mid2blob($ctx);
164 return r404() unless $x;
166 require PublicInbox::View;
167 my $mid_href = PublicInbox::Hval::ascii_html(
168 uri_escape_utf8($ctx->{mid}));
169 my $pfx = "../f/$mid_href.html";
170 my $foot = footer($ctx);
172 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
173 [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx, $foot) ] ];
176 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
178 my ($ctx, $cgi) = @_;
179 my $x = mid2blob($ctx);
180 return r404() unless $x;
181 require PublicInbox::View;
183 my $foot = footer($ctx);
184 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
185 [ PublicInbox::View->msg_html(Email::MIME->new($x), undef, $foot)] ];
190 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
193 sub redirect_list_index {
194 my ($ctx, $cgi) = @_;
195 do_redirect(self_url($cgi) . "/");
199 my ($ctx, $cgi) = @_;
200 my $url = self_url($cgi);
202 do_redirect($url . '.html');
208 [ Location => $url, 'Content-Type' => 'text/plain' ],
209 [ "Redirecting to $url\n" ]
214 my ($ctx, $key) = @_;
215 my $val = $ctx->{$key};
216 (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
223 if (open(my $fh, '<', $path)) {
233 return '' unless $ctx;
234 my $git_dir = ctx_get($ctx, 'git_dir');
236 # favor user-supplied footer
237 my $footer = try_cat("$git_dir/public-inbox/footer.html");
238 if (defined $footer) {
243 # auto-generate a footer
244 my $listname = ctx_get($ctx, 'listname');
245 my $desc = try_cat("$git_dir/description");
246 $desc = '$GIT_DIR/description missing' unless defined $desc;
249 my $urls = try_cat("$git_dir/cloneurl");
250 my @urls = split(/\r?\n/, $urls || '');
251 my $nurls = scalar @urls;
253 $urls = '($GIT_DIR/cloneurl missing)';
254 } elsif ($nurls == 1) {
255 $urls = 'git archive URL for <a href="' . SSOMA_URL .
256 '">ssoma</a>: ' . $urls[0];
258 $urls = 'git archive URLs for <a href="' . SSOMA_URL .
259 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
262 my $addr = $pi_config->get($listname, 'address');
263 if (ref($addr) eq 'ARRAY') {
264 $addr = $addr->[0]; # first address is primary
267 $addr = "<a href=\"mailto:$addr\">$addr</a>";
271 'This is a <a href="' . PI_URL . '">public-inbox</a>, '.
273 "\t$addr (text-only, no HTML please)",