2 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
5 # We focus on the lowest common denominators here:
6 # - targeted at text-only console browsers (lynx, w3m, etc..)
7 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
8 # - No JavaScript, graphics or icons allowed.
9 # - Must not rely on static content
10 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
15 use CGI qw(:cgi :escapeHTML -nosticky); # PSGI/FastCGI/mod_perl compat
16 use Encode qw(decode_utf8);
17 use PublicInbox::Config;
18 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
21 $pi_config = PublicInbox::Config->new;
22 # TODO: detect and reload config as needed
28 binmode STDOUT, ':utf8';
32 my ($status, $headers, $body) = @$ret;
33 if (@ARGV && $ARGV[0] eq 'static') {
36 print "Status: $status\r\n";
37 while (my ($k, $v) = each %$headers) {
45 # private functions below
50 if ($cgi->request_method !~ /\AGET|HEAD\z/) {
51 return r("405 Method Not Allowed");
53 my $path_info = decode_utf8($ENV{PATH_INFO});
55 # top-level indices and feeds
56 if ($path_info eq "/") {
58 } elsif ($path_info =~ m!$LISTNAME_RE/?\z!o) {
59 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
60 } elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
61 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
62 } elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
63 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
65 # single-message pages
66 } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.txt\z!o) {
67 get_mid_txt($cgi, $1, $2);
68 } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.html\z!o) {
69 get_mid_html($cgi, $1, $2);
70 } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\z!o) {
71 redirect_mid_html($cgi, $1, $2);
77 sub r404 { r("404 Not Found") }
79 # simple response for errors
80 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
82 # returns undef if valid, array ref response if invalid
84 my ($ctx, $listname) = @_;
85 my $git_dir = $pi_config->get($listname, "mainrepo");
86 if (defined $git_dir) {
87 $ctx->{git_dir} = $git_dir;
88 $ctx->{listname} = $listname;
94 # returns undef if valid, array ref response if invalid
95 sub invalid_list_mid {
96 my ($ctx, $listname, $mid) = @_;
97 my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
103 my ($ctx, $cgi, $top) = @_;
104 require PublicInbox::Feed;
105 [ '200 OK', { 'Content-Type' => 'application/xml' },
106 PublicInbox::Feed->generate({
107 git_dir => $ctx->{git_dir},
108 listname => $ctx->{listname},
109 pi_config => $pi_config,
117 my ($ctx, $cgi, $top) = @_;
118 require PublicInbox::Feed;
119 [ '200 OK', { 'Content-Type' => 'text/html' },
120 PublicInbox::Feed->generate_html_index({
121 git_dir => $ctx->{git_dir},
122 listname => $ctx->{listname},
123 pi_config => $pi_config,