]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NewsGroup.pm
500f61e3a9b8d82c326025f81f3e9fd0e316e318
[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, $newsgroup, $git_dir, $address, $url) = @_;
17
18         # first email address is preferred
19         $address = $address->[0] if ref($address);
20         if ($url) {
21                 # assume protocol-relative URLs which start with '//' means
22                 # the server supports both HTTP and HTTPS, favor HTTPS.
23                 $url = "https:$url" if $url =~ m!\A//!;
24                 $url .= '/' if $url !~ m!/\z!;
25         }
26         my $self = bless {
27                 newsgroup => $newsgroup,
28                 git_dir => $git_dir,
29                 address => $address,
30                 url => $url,
31         }, $class;
32         $self->{domain} = ($address =~ /\@(\S+)\z/) ? $1 : 'localhost';
33         $self;
34 }
35
36 sub weaken_all {
37         my ($self) = @_;
38         weaken($self->{$_}) foreach qw(gcf mm search);
39 }
40
41 sub gcf {
42         my ($self) = @_;
43         $self->{gcf} ||= eval { PublicInbox::Git->new($self->{git_dir}) };
44 }
45
46 sub usable {
47         my ($self) = @_;
48         eval {
49                 PublicInbox::Msgmap->new($self->{git_dir});
50                 PublicInbox::Search->new($self->{git_dir});
51         };
52 }
53
54 sub mm {
55         my ($self) = @_;
56         $self->{mm} ||= eval { PublicInbox::Msgmap->new($self->{git_dir}) };
57 }
58
59 sub search {
60         my ($self) = @_;
61         $self->{search} ||= eval { PublicInbox::Search->new($self->{git_dir}) };
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;