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);
53 } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
54 invalid_list_mid(\%ctx, $1, $2) || get_thread(\%ctx, $cgi);
56 # subject_path display
57 } elsif ($path_info =~ m!$LISTNAME_RE/s/(\S+)\.html\z!o) {
59 invalid_list(\%ctx, $1) || get_subject_path(\%ctx, $cgi, $sp);
61 # convenience redirects, order matters
62 } elsif ($path_info =~ m!$LISTNAME_RE/(m|f|t|s)/(\S+)\z!o) {
64 invalid_list_mid(\%ctx, $1, $3) ||
65 redirect_mid(\%ctx, $cgi, $2);
72 # for CoW-friendliness, MOOOOO!
74 require PublicInbox::Feed;
75 require PublicInbox::View;
76 require PublicInbox::Thread;
80 require XML::Atom::SimpleFeed;
83 # private functions below
85 sub r404 { r(404, 'Not Found') }
87 # simple response for errors
88 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
90 # returns undef if valid, array ref response if invalid
92 my ($ctx, $listname) = @_;
93 my $git_dir = $pi_config->get($listname, "mainrepo");
94 if (defined $git_dir) {
95 $ctx->{git_dir} = $git_dir;
96 $ctx->{listname} = $listname;
102 # returns undef if valid, array ref response if invalid
103 sub invalid_list_mid {
104 my ($ctx, $listname, $mid) = @_;
105 my $ret = invalid_list($ctx, $listname, $mid);
106 $ctx->{mid} = uri_unescape($mid) unless $ret;
110 # /$LISTNAME/atom.xml -> Atom feed, includes replies
112 my ($ctx, $cgi, $top) = @_;
113 require PublicInbox::Feed;
114 [ 200, [ 'Content-Type' => 'application/xml' ],
115 [ PublicInbox::Feed->generate({
116 git_dir => $ctx->{git_dir},
117 listname => $ctx->{listname},
118 pi_config => $pi_config,
125 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
127 my ($ctx, $cgi, $top) = @_;
128 require PublicInbox::Feed;
129 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
130 [ PublicInbox::Feed->generate_html_index({
131 git_dir => $ctx->{git_dir},
132 listname => $ctx->{listname},
133 pi_config => $pi_config,
135 footer => footer($ctx),
141 # just returns a string ref for the blob in the current ctx
144 my $hex = $ctx->{mid};
145 my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
147 unless (defined $x38) {
148 # compatibility with old links
150 $hex = Digest::SHA::sha1_hex($hex);
151 ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
152 defined $x38 or die "BUG: not a SHA-1 hex: $hex";
155 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
156 qw(cat-file blob), "HEAD:$x2/$x38");
157 my $cmd = join(' ', @cmd);
158 my $pid = open my $fh, '-|';
159 defined $pid or die "fork failed: $!\n";
161 open STDERR, '>', '/dev/null'; # ignore errors
162 exec @cmd or die "exec failed: $!\n";
164 my $blob = eval { local $/; <$fh> };
166 $? == 0 ? \$blob : undef;
170 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
172 my ($ctx, $cgi) = @_;
173 my $x = mid2blob($ctx);
174 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
177 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
179 my ($ctx, $cgi) = @_;
180 my $x = mid2blob($ctx);
181 return r404() unless $x;
183 require PublicInbox::View;
184 my $pfx = msg_pfx($ctx);
185 my $foot = footer($ctx);
187 my $mime = Email::MIME->new($x);
188 my $srch = searcher($ctx);
189 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
190 [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
193 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
195 my ($ctx, $cgi) = @_;
196 my $x = mid2blob($ctx);
197 return r404() unless $x;
198 require PublicInbox::View;
199 my $foot = footer($ctx);
201 my $mime = Email::MIME->new($x);
202 my $srch = searcher($ctx);
203 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
204 [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
207 # /$LISTNAME/t/$MESSAGE_ID.html
209 my ($ctx, $cgi) = @_;
210 my $srch = searcher($ctx) or return need_search($ctx);
211 require PublicInbox::View;
212 my $foot = footer($ctx);
213 my $body = PublicInbox::View->thread_html($ctx, $foot, $srch) or
215 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
219 # /$LISTNAME/s/$SUBJECT_PATH.html
220 sub get_subject_path {
221 my ($ctx, $cgi, $sp) = @_;
222 $ctx->{subject_path} = $sp;
223 my $srch = searcher($ctx) or return need_search($ctx);
224 require PublicInbox::View;
225 my $foot = footer($ctx);
226 my $body = PublicInbox::View->subject_path_html($ctx, $foot, $srch) or
228 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
234 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
237 sub redirect_list_index {
238 my ($ctx, $cgi) = @_;
239 do_redirect(self_url($cgi) . "/");
243 my ($ctx, $cgi, $pfx) = @_;
244 my $url = self_url($cgi);
246 if (lc($pfx) eq 't') {
247 $anchor = '#u'; # <u id='#u'> is used to highlight in View.pm
249 do_redirect($url . ".html$anchor");
255 [ Location => $url, 'Content-Type' => 'text/plain' ],
256 [ "Redirecting to $url\n" ]
261 my ($ctx, $key) = @_;
262 my $val = $ctx->{$key};
263 (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
270 if (open(my $fh, '<', $path)) {
280 return '' unless $ctx;
281 my $git_dir = ctx_get($ctx, 'git_dir');
283 # favor user-supplied footer
284 my $footer = try_cat("$git_dir/public-inbox/footer.html");
285 if (defined $footer) {
290 # auto-generate a footer
291 my $listname = ctx_get($ctx, 'listname');
292 my $desc = try_cat("$git_dir/description");
293 $desc = '$GIT_DIR/description missing' unless defined $desc;
296 my $urls = try_cat("$git_dir/cloneurl");
297 my @urls = split(/\r?\n/, $urls || '');
298 my $nurls = scalar @urls;
300 $urls = '($GIT_DIR/cloneurl missing)';
301 } elsif ($nurls == 1) {
302 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
303 '">ssoma</a>: ' . $urls[0];
305 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
306 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
309 my $addr = $pi_config->get($listname, 'address');
310 if (ref($addr) eq 'ARRAY') {
311 $addr = $addr->[0]; # first address is primary
314 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
318 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
319 'anybody may post in plain-text (not HTML):',
325 # search support is optional, returns undef if Xapian is not installed
326 # or not configured for the given GIT_DIR
330 require PublicInbox::Search;
331 PublicInbox::Search->new($ctx->{git_dir});
338 <html><head><title>Search not available for this
339 public-inbox</title><body><pre>Search is not available for this public-inbox
340 <a href="../">Return to index</a></pre></body></html>
342 [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
347 my $href = PublicInbox::Hval::ascii_html(uri_escape_utf8($ctx->{mid}));