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