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
133 my $hex = $ctx->{mid};
134 my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
136 unless (defined $x38) {
137 # compatibility with old links
139 $hex = Digest::SHA::sha1_hex($hex);
140 ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
141 defined $x38 or die "BUG: not a SHA-1 hex: $hex";
144 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
145 qw(cat-file blob), "HEAD:$x2/$x38");
146 my $cmd = join(' ', @cmd);
147 my $pid = open my $fh, '-|';
148 defined $pid or die "fork failed: $!\n";
150 open STDERR, '>', '/dev/null'; # ignore errors
151 exec @cmd or die "exec failed: $!\n";
153 my $blob = eval { local $/; <$fh> };
155 $? == 0 ? \$blob : undef;
159 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
161 my ($ctx, $cgi) = @_;
162 my $x = mid2blob($ctx);
163 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
166 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
168 my ($ctx, $cgi) = @_;
169 my $x = mid2blob($ctx);
170 return r404() unless $x;
172 require PublicInbox::View;
173 my $mid_href = PublicInbox::Hval::ascii_html(
174 uri_escape_utf8($ctx->{mid}));
175 my $pfx = "../f/$mid_href.html";
176 my $foot = footer($ctx);
178 my $mime = Email::MIME->new($x);
179 my $srch = searcher($ctx);
180 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
181 [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
184 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
186 my ($ctx, $cgi) = @_;
187 my $x = mid2blob($ctx);
188 return r404() unless $x;
189 require PublicInbox::View;
190 my $foot = footer($ctx);
192 my $mime = Email::MIME->new($x);
193 my $srch = searcher($ctx);
194 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
195 [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
200 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
203 sub redirect_list_index {
204 my ($ctx, $cgi) = @_;
205 do_redirect(self_url($cgi) . "/");
209 my ($ctx, $cgi) = @_;
210 my $url = self_url($cgi);
212 do_redirect($url . '.html');
218 [ Location => $url, 'Content-Type' => 'text/plain' ],
219 [ "Redirecting to $url\n" ]
224 my ($ctx, $key) = @_;
225 my $val = $ctx->{$key};
226 (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
233 if (open(my $fh, '<', $path)) {
243 return '' unless $ctx;
244 my $git_dir = ctx_get($ctx, 'git_dir');
246 # favor user-supplied footer
247 my $footer = try_cat("$git_dir/public-inbox/footer.html");
248 if (defined $footer) {
253 # auto-generate a footer
254 my $listname = ctx_get($ctx, 'listname');
255 my $desc = try_cat("$git_dir/description");
256 $desc = '$GIT_DIR/description missing' unless defined $desc;
259 my $urls = try_cat("$git_dir/cloneurl");
260 my @urls = split(/\r?\n/, $urls || '');
261 my $nurls = scalar @urls;
263 $urls = '($GIT_DIR/cloneurl missing)';
264 } elsif ($nurls == 1) {
265 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
266 '">ssoma</a>: ' . $urls[0];
268 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
269 "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
272 my $addr = $pi_config->get($listname, 'address');
273 if (ref($addr) eq 'ARRAY') {
274 $addr = $addr->[0]; # first address is primary
277 $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
281 "A <a\nhref=\"" . PI_URL . '">public-inbox</a>, ' .
282 'anybody may post in plain-text (not HTML):',
288 # search support is optional, returns undef if Xapian is not installed
289 # or not configured for the given GIT_DIR
293 require PublicInbox::Search;
294 PublicInbox::Search->new($ctx->{git_dir});