# Copyright (C) 2015 all contributors <meta@public-inbox.org>
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
+#
+# Used only by the NNTP server to represent a public-inbox git repository
+# as a newsgroup
package PublicInbox::NewsGroup;
use strict;
use warnings;
-use fields qw(name git_dir address domain mm gcf search);
use Scalar::Util qw(weaken);
require Danga::Socket;
require PublicInbox::Msgmap;
-require PublicInbox::GitCatFile;
+require PublicInbox::Search;
+require PublicInbox::Git;
sub new {
- my ($class, $name, $git_dir, $address) = @_;
- my $self = fields::new($class);
- $self->{name} = $name;
+ my ($class, $newsgroup, $git_dir, $address, $url) = @_;
+
+ # first email address is preferred
$address = $address->[0] if ref($address);
+ if ($url) {
+ # assume protocol-relative URLs which start with '//' means
+ # the server supports both HTTP and HTTPS, favor HTTPS.
+ $url = "https:$url" if $url =~ m!\A//!;
+ $url .= '/' if $url !~ m!/\z!;
+ }
+ my $self = bless {
+ newsgroup => $newsgroup,
+ git_dir => $git_dir,
+ address => $address,
+ url => $url,
+ }, $class;
$self->{domain} = ($address =~ /\@(\S+)\z/) ? $1 : 'localhost';
- $self->{git_dir} = $git_dir;
- $self->{address} = $address;
$self;
}
-sub defer_weaken {
- my ($self, $field) = @_;
- Danga::Socket->AddTimer(30, sub { weaken($self->{$field}) });
+sub weaken_all {
+ my ($self) = @_;
+ weaken($self->{$_}) foreach qw(gcf mm search);
}
sub gcf {
my ($self) = @_;
- $self->{gcf} ||= eval {
- my $gcf = PublicInbox::GitCatFile->new($self->{git_dir});
+ $self->{gcf} ||= eval { PublicInbox::Git->new($self->{git_dir}) };
+}
- # git repos may be repacked and old packs unlinked
- defer_weaken($self, 'gcf');
- $gcf;
+sub usable {
+ my ($self) = @_;
+ eval {
+ PublicInbox::Msgmap->new($self->{git_dir});
+ PublicInbox::Search->new($self->{git_dir});
};
}
sub mm {
- my ($self, $check_only) = @_;
- if ($check_only) {
- return eval { PublicInbox::Msgmap->new($self->{git_dir}) };
- }
- $self->{mm} ||= eval {
- my $mm = PublicInbox::Msgmap->new($self->{git_dir});
-
- # may be needed if we run low on handles
- defer_weaken($self, 'mm');
- $mm;
- };
+ my ($self) = @_;
+ $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{git_dir}) };
}
sub search {
my ($self) = @_;
- $self->{search} ||= eval {
- require PublicInbox::Search;
- my $search = PublicInbox::Search->new($self->{git_dir});
-
- # may be needed if we run low on handles
- defer_weaken($self, 'search');
- $search;
- };
+ $self->{search} ||= eval { PublicInbox::Search->new($self->{git_dir}) };
}
sub description {