]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NewsGroup.pm
02e90111ddcf2cf5af385cb98eb0c8a9acf853fc
[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 package PublicInbox::NewsGroup;
4 use strict;
5 use warnings;
6 use Scalar::Util qw(weaken);
7 require Danga::Socket;
8 require PublicInbox::Msgmap;
9 require PublicInbox::Search;
10 require PublicInbox::GitCatFile;
11
12 sub new {
13         my ($class, $name, $git_dir, $address) = @_;
14         $address = $address->[0] if ref($address);
15         my $self = bless {
16                 name => $name,
17                 git_dir => $git_dir,
18                 address => $address,
19         }, $class;
20         $self->{domain} = ($address =~ /\@(\S+)\z/) ? $1 : 'localhost';
21         $self;
22 }
23
24 sub defer_weaken {
25         my ($self, $field) = @_;
26         Danga::Socket->AddTimer(30, sub { weaken($self->{$field}) });
27 }
28
29 sub gcf {
30         my ($self) = @_;
31         $self->{gcf} ||= eval {
32                 my $gcf = PublicInbox::GitCatFile->new($self->{git_dir});
33
34                 # git repos may be repacked and old packs unlinked
35                 defer_weaken($self, 'gcf');
36                 $gcf;
37         };
38 }
39
40 sub usable {
41         my ($self) = @_;
42         eval {
43                 PublicInbox::Msgmap->new($self->{git_dir});
44                 PublicInbox::Search->new($self->{git_dir});
45         };
46 }
47
48 sub mm {
49         my ($self) = @_;
50         $self->{mm} ||= eval {
51                 my $mm = PublicInbox::Msgmap->new($self->{git_dir});
52
53                 # may be needed if we run low on handles
54                 defer_weaken($self, 'mm');
55                 $mm;
56         };
57 }
58
59 sub search {
60         my ($self) = @_;
61         $self->{search} ||= eval {
62                 my $search = PublicInbox::Search->new($self->{git_dir});
63
64                 # may be needed if we run low on handles
65                 defer_weaken($self, 'search');
66                 $search;
67         };
68 }
69
70 sub description {
71         my ($self) = @_;
72         open my $fh, '<', "$self->{git_dir}/description" or return '';
73         my $desc = eval { local $/; <$fh> };
74         chomp $desc;
75         $desc =~ s/\s+/ /smg;
76         $desc;
77 }
78
79 sub update {
80         my ($self, $new) = @_;
81         $self->{address} = $new->{address};
82         $self->{domain} = $new->{domain};
83         if ($self->{git_dir} ne $new->{git_dir}) {
84                 # new git_dir requires a new mm and gcf
85                 $self->{mm} = $self->{gcf} = undef;
86                 $self->{git_dir} = $new->{git_dir};
87         }
88 }
89
90 1;