]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Inbox.pm
inbox: streamline ->nntp_url
[public-inbox.git] / lib / PublicInbox / Inbox.pm
index da7ea75f1a5741c7702aa0a85466ee908b9f019b..f234b96f782f66f76d11df40f83e2ede5710c54b 100644 (file)
@@ -8,6 +8,7 @@ use PublicInbox::Git;
 use PublicInbox::MID qw(mid2path);
 use PublicInbox::Eml;
 use List::Util qw(max);
+use Carp qw(croak);
 
 # Long-running "git-cat-file --batch" processes won't notice
 # unlinked packs, so we need to restart those processes occasionally.
@@ -108,8 +109,6 @@ sub new {
        } else {
                delete $opts->{feedmax};
        }
-       $opts->{nntpserver} ||= $pi_cfg->{'publicinbox.nntpserver'};
-
        # allow any combination of multi-line or comma-delimited hide entries
        my $hide = {};
        if (defined(my $h = $opts->{hide})) {
@@ -168,8 +167,8 @@ sub max_git_epoch {
 }
 
 sub mm {
-       my ($self) = @_;
-       $self->{mm} ||= eval {
+       my ($self, $req) = @_;
+       $self->{mm} //= eval {
                require PublicInbox::Msgmap;
                my $dir = $self->{inboxdir};
                if ($self->version >= 2) {
@@ -177,7 +176,7 @@ sub mm {
                } else {
                        PublicInbox::Msgmap->new($dir);
                }
-       };
+       } // ($req ? croak("E: $@") : undef);
 }
 
 sub search {
@@ -195,19 +194,19 @@ sub search {
 sub isrch { $_[0]->{isrch} // search($_[0]) }
 
 sub over {
-       $_[0]->{over} //= eval {
-               my $srch = $_[0]->{search} //= do {
-                       _cleanup_later($_[0]);
+       my ($self, $req) = @_;
+       $self->{over} //= eval {
+               my $srch = $self->{search} //= do {
+                       _cleanup_later($self);
                        require PublicInbox::Search;
-                       PublicInbox::Search->new($_[0]);
+                       PublicInbox::Search->new($self);
                };
                my $over = PublicInbox::Over->new("$srch->{xpfx}/over.sqlite3");
                $over->dbh; # may fail
                $over;
-       };
+       } // ($req ? croak("E: $@") : undef);
 }
 
-
 sub try_cat {
        my ($path) = @_;
        open(my $fh, '<', $path) or return '';
@@ -241,7 +240,7 @@ sub cloneurl {
 
 sub base_url {
        my ($self, $env) = @_; # env - PSGI env
-       if ($env) {
+       if ($env && $env->{'psgi.url_scheme'}) {
                my $url = PublicInbox::Git::host_prefix_url($env, '');
                # for mount in Plack::Builder
                $url .= '/' if $url !~ m!/\z!;
@@ -249,7 +248,8 @@ sub base_url {
        }
        # called from a non-PSGI environment (e.g. NNTP/POP3):
        $self->{-base_url} ||= do {
-               my $url = $self->{url}->[0] or return undef;
+               my $url = $self->{url} // return undef;
+               $url = $url->[0] // return undef;
                # expand protocol-relative URLs to HTTPS if we're
                # not inside a web server
                $url = "https:$url" if $url =~ m!\A//!;
@@ -259,22 +259,21 @@ sub base_url {
 }
 
 sub nntp_url {
-       my ($self) = @_;
+       my ($self, $ctx) = @_;
        $self->{-nntp_url} ||= do {
                # no checking for nntp_usable here, we can point entirely
                # to non-local servers or users run by a different user
-               my $ns = $self->{nntpserver};
+               my $ns = $self->{nntpserver} //
+                      $ctx->{www}->{pi_cfg}->get_all('publicinbox.nntpserver');
                my $group = $self->{newsgroup};
                my @urls;
                if ($ns && $group) {
-                       $ns = [ $ns ] if ref($ns) ne 'ARRAY';
                        @urls = map {
                                my $u = m!\Anntps?://! ? $_ : "nntp://$_";
                                $u .= '/' if $u !~ m!/\z!;
                                $u.$group;
                        } @$ns;
                }
-
                my $mirrors = $self->{nntpmirror};
                if ($mirrors) {
                        my @m;