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