]> Sergey Matveev's repositories - public-inbox.git/blobdiff - public-inbox-cgi
add spam/ham learning wrapper script
[public-inbox.git] / public-inbox-cgi
index 91314f06836456414a172e7b0e47669f20a5ecb4..6a6f31e23ef81acd8045001190a3e7991c012290 100755 (executable)
@@ -26,11 +26,10 @@ BEGIN {
        }
 }
 
-binmode STDOUT, ':utf8';
-
 my $ret = main();
 
 my ($status, $headers, $body) = @$ret;
+set_binmode($headers);
 if (@ARGV && $ARGV[0] eq 'static') {
        print $body;
 } else { # CGI
@@ -56,7 +55,7 @@ sub main {
        # top-level indices and feeds
        if ($path_info eq "/") {
                r404();
-       } elsif ($path_info =~ m!$LISTNAME_RE/?\z!o) {
+       } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
                invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
        } elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
                invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
@@ -70,6 +69,13 @@ sub main {
                invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
        } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
                redirect_mid_html($cgi, $1, $2);
+
+       # full-message page
+       } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
+               invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
+       } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
+               redirect_mid_html($cgi, $1, $2);
+
        } else {
                r404();
        }
@@ -100,6 +106,8 @@ sub invalid_list_mid {
        undef;
 }
 
+# /$LISTNAME/index.atom.xml                     -> Atom feed
+# /$LISTNAME/all.atom.xml                       -> Atom feed, includes replies
 sub get_atom {
        my ($ctx, $cgi, $top) = @_;
        require PublicInbox::Feed;
@@ -114,6 +122,7 @@ sub get_atom {
        ];
 }
 
+# /$LISTNAME/?before=$GIT_COMMIT                 -> HTML only
 sub get_index {
        my ($ctx, $cgi, $top) = @_;
        require PublicInbox::Feed;
@@ -128,6 +137,7 @@ sub get_index {
        ];
 }
 
+# just returns a string ref for the blob in the current ctx
 sub mid2blob {
        my ($ctx) = @_;
        local $ENV{GIT_DIR} = $ctx->{git_dir};
@@ -138,8 +148,46 @@ sub mid2blob {
        $? == 0 ? \$blob : undef;
 }
 
+# /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
 sub get_mid_txt {
        my ($ctx, $cgi) = @_;
        my $x = mid2blob($ctx);
        $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
 }
+
+# /$LISTNAME/m/$MESSAGE_ID.html                   -> HTML content (short quotes)
+sub get_mid_html {
+       my ($ctx, $cgi) = @_;
+       my $x = mid2blob($ctx);
+       return r404() unless $x;
+
+       my $pfx = $cgi->self_url;
+       $pfx =~ s!/m/.+\z!/!; # FIXME: this is not robust
+
+       require PublicInbox::View;
+       require Email::MIME;
+       [ "200 OK", {'Content-Type' => 'text/html'},
+               PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
+}
+
+# /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
+sub get_full_html {
+       my ($ctx, $cgi) = @_;
+       my $x = mid2blob($ctx);
+       return r404() unless $x;
+       require PublicInbox::View;
+       require Email::MIME;
+       [ "200 OK", {'Content-Type' => 'text/html'},
+               PublicInbox::View->as_html(Email::MIME->new($$x))];
+}
+
+# only used for CGI and static file generation modes
+sub set_binmode {
+       my ($headers) = @_;
+       if ($headers->{'Content-Type'} eq 'text/plain') {
+               # no way to validate raw messages, mixed encoding is possible.
+               binmode STDOUT;
+       } else { # strict encoding for HTML and XML
+               binmode STDOUT, ':encoding(UTF-8)';
+       }
+}