]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NewsGroup.pm
adac919fe0b2e15143528d113b2d122f25266a5a
[public-inbox.git] / lib / PublicInbox / NewsGroup.pm
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # Used only by the NNTP server to represent a public-inbox git repository
5 # as a newsgroup
6 package PublicInbox::NewsGroup;
7 use strict;
8 use warnings;
9 use Scalar::Util qw(weaken);
10 require Danga::Socket;
11 require PublicInbox::Msgmap;
12 require PublicInbox::Search;
13 require PublicInbox::Git;
14
15 sub new {
16         my ($class, $name, $git_dir, $address) = @_;
17         $address = $address->[0] if ref($address);
18         my $self = bless {
19                 name => $name,
20                 git_dir => $git_dir,
21                 address => $address,
22         }, $class;
23         $self->{domain} = ($address =~ /\@(\S+)\z/) ? $1 : 'localhost';
24         $self;
25 }
26
27 sub weaken_all {
28         my ($self) = @_;
29         weaken($self->{$_}) foreach qw(gcf mm search);
30 }
31
32 sub gcf {
33         my ($self) = @_;
34         $self->{gcf} ||= eval { PublicInbox::Git->new($self->{git_dir}) };
35 }
36
37 sub usable {
38         my ($self) = @_;
39         eval {
40                 PublicInbox::Msgmap->new($self->{git_dir});
41                 PublicInbox::Search->new($self->{git_dir});
42         };
43 }
44
45 sub mm {
46         my ($self) = @_;
47         $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{git_dir}) };
48 }
49
50 sub search {
51         my ($self) = @_;
52         $self->{search} ||= eval { PublicInbox::Search->new($self->{git_dir}) };
53 }
54
55 sub description {
56         my ($self) = @_;
57         open my $fh, '<', "$self->{git_dir}/description" or return '';
58         my $desc = eval { local $/; <$fh> };
59         chomp $desc;
60         $desc =~ s/\s+/ /smg;
61         $desc;
62 }
63
64 sub update {
65         my ($self, $new) = @_;
66         $self->{address} = $new->{address};
67         $self->{domain} = $new->{domain};
68         if ($self->{git_dir} ne $new->{git_dir}) {
69                 # new git_dir requires a new mm and gcf
70                 $self->{mm} = $self->{gcf} = undef;
71                 $self->{git_dir} = $new->{git_dir};
72         }
73 }
74
75 1;