]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
remove redundant NewsGroup class
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 85cb234b4d56351589bbbf8d633893e7498a17b6..cf370afa750fd5c832ab29a661f1d58bc762252d 100644 (file)
@@ -14,7 +14,7 @@ use 5.008;
 use strict;
 use warnings;
 use Plack::Request;
-use PublicInbox::Config qw(try_cat);
+use PublicInbox::Config;
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
 use constant PI_URL => '//public-inbox.org/';
@@ -23,6 +23,7 @@ use PublicInbox::GitHTTPBackend;
 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
 our $MID_RE = qr!([^/]+)!;
 our $END_RE = qr!(T/|t/|R/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
+our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
 
 sub new {
        my ($class, $pi_config) = @_;
@@ -39,7 +40,7 @@ sub run {
 sub call {
        my ($self, $env) = @_;
        my $cgi = Plack::Request->new($env);
-       my $ctx = { cgi => $cgi, pi_config => $self->{pi_config} };
+       my $ctx = {cgi => $cgi, pi_config => $self->{pi_config}, www => $self};
        my $path_info = $cgi->path_info;
 
        my $method = $cgi->method;
@@ -68,9 +69,15 @@ sub call {
                my $path = $2;
                invalid_inbox($self, $ctx, $1) ||
                        serve_git($cgi, $ctx->{git}, $path);
+       } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
+               serve_mbox_range($self, $ctx, $1, $2);
        } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
                msg_page($self, $ctx, $1, $2, $3);
 
+       } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
+               my ($idx, $fn) = ($3, $4);
+               invalid_inbox_mid($self, $ctx, $1, $2) ||
+                       get_attach($ctx, $idx, $fn);
        # in case people leave off the trailing slash:
        } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t|R)\z!o) {
                my ($inbox, $mid, $suffix) = ($1, $2, $3);
@@ -100,7 +107,7 @@ sub preload {
 
        foreach (qw(PublicInbox::Search PublicInbox::SearchView
                        PublicInbox::Mbox IO::Compress::Gzip
-                       PublicInbox::NewsWWW PublicInbox::NewsGroup)) {
+                       PublicInbox::NewsWWW)) {
                eval "require $_;";
        }
 }
@@ -122,11 +129,14 @@ sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
 
 # returns undef if valid, array ref response if invalid
 sub invalid_inbox {
-       my ($self, $ctx, $inbox, $mid) = @_;
-       my $git_dir = $ctx->{pi_config}->get($inbox, "mainrepo");
-       if (defined $git_dir) {
-               $ctx->{git_dir} = $git_dir;
-               $ctx->{git} = PublicInbox::Git->new($git_dir);
+       my ($self, $ctx, $inbox) = @_;
+       my $obj = $ctx->{pi_config}->lookup_name($inbox);
+       if (defined $obj) {
+               $ctx->{git_dir} = $obj->{mainrepo};
+               $ctx->{git} = $obj->git;
+               # for PublicInbox::HTTP::weaken_task:
+               $ctx->{cgi}->{env}->{'pi-httpd.inbox'} = $obj;
+               $ctx->{-inbox} = $obj;
                $ctx->{inbox} = $inbox;
                return;
        }
@@ -141,7 +151,7 @@ sub invalid_inbox {
 # returns undef if valid, array ref response if invalid
 sub invalid_inbox_mid {
        my ($self, $ctx, $inbox, $mid) = @_;
-       my $ret = invalid_inbox($self, $ctx, $inbox, $mid);
+       my $ret = invalid_inbox($self, $ctx, $inbox);
        return $ret if $ret;
 
        $ctx->{mid} = $mid = uri_unescape($mid);
@@ -241,27 +251,16 @@ sub ctx_get {
 sub footer {
        my ($ctx) = @_;
        return '' unless $ctx;
-       my $git_dir = ctx_get($ctx, 'git_dir');
-
-       # favor user-supplied footer
-       my $footer = try_cat("$git_dir/public-inbox/footer.html");
-       if (defined $footer) {
-               chomp $footer;
-               $ctx->{footer} = $footer;
-               return $footer;
-       }
+       my $obj = $ctx->{-inbox} or return '';
 
        # auto-generate a footer
-       my $inbox = ctx_get($ctx, 'inbox');
-       my $desc = try_cat("$git_dir/description");
-       $desc = '$GIT_DIR/description missing' unless defined $desc;
-       chomp $desc;
+       chomp(my $desc = $obj->description);
 
-       my $urls = try_cat("$git_dir/cloneurl");
-       my @urls = split(/\r?\n/, $urls || '');
+       my $urls;
+       my @urls = @{$obj->cloneurl};
        my %seen = map { $_ => 1 } @urls;
        my $cgi = $ctx->{cgi};
-       my $http = $cgi->base->as_string . $inbox;
+       my $http = $cgi->base->as_string . $obj->{name};
        $seen{$http} or unshift @urls, $http;
        my $ssoma_url = PublicInbox::Hval::prurl($cgi->{env}, SSOMA_URL);
        if (scalar(@urls) == 1) {
@@ -273,13 +272,7 @@ sub footer {
                        join("\n", map { "\tgit clone --mirror $_" } @urls);
        }
 
-       my $addr = $ctx->{pi_config}->get($inbox, 'address');
-       if (ref($addr) eq 'ARRAY') {
-               $addr = $addr->[0]; # first address is primary
-       }
-
-       $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
-
+       my $addr = $obj->{-primary_address};
        $ctx->{footer} = join("\n",
                '- ' . $desc,
                "A <a\nhref=\"" .
@@ -297,7 +290,7 @@ sub searcher {
        my ($ctx) = @_;
        eval {
                require PublicInbox::Search;
-               $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
+               $ctx->{srch} = $ctx->{-inbox}->search;
        };
 }
 
@@ -394,9 +387,14 @@ sub legacy_redirects {
 sub r301 {
        my ($ctx, $inbox, $mid, $suffix) = @_;
        my $cgi = $ctx->{cgi};
-       my $url;
+       my $obj = $ctx->{-inbox};
+       unless ($obj) {
+               my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
+               return $r404 if $r404;
+               $obj = $ctx->{-inbox};
+       }
+       my $url = $obj->base_url($cgi);
        my $qs = $cgi->env->{QUERY_STRING};
-       $url = $cgi->base->as_string . $inbox . '/';
        $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
        $url .= $suffix if (defined $suffix);
        $url .= "?$qs" if $qs ne '';
@@ -430,6 +428,15 @@ sub serve_git {
        PublicInbox::GitHTTPBackend::serve($cgi, $git, $path);
 }
 
+sub serve_mbox_range {
+       my ($self, $ctx, $inbox, $range) = @_;
+       invalid_inbox($self, $ctx, $inbox) || eval {
+               require PublicInbox::Mbox;
+               searcher($ctx);
+               PublicInbox::Mbox::emit_range($ctx, $range);
+       }
+}
+
 sub news_www {
        my ($self) = @_;
        my $nw = $self->{news_www};
@@ -438,4 +445,10 @@ sub news_www {
        $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
 }
 
+sub get_attach {
+       my ($ctx, $idx, $fn) = @_;
+       require PublicInbox::WwwAttach;
+       PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
+}
+
 1;