]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NNTPD.pm
nntpd: move {newsgroup} name check to config
[public-inbox.git] / lib / PublicInbox / NNTPD.pm
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # represents an NNTPD (currently a singleton),
5 # see script/public-inbox-nntpd for how it is used
6 package PublicInbox::NNTPD;
7 use strict;
8 use warnings;
9 use Sys::Hostname;
10 use PublicInbox::Config;
11 use PublicInbox::InboxIdle;
12
13 sub new {
14         my ($class) = @_;
15         my $pi_config = PublicInbox::Config->new;
16         my $name = $pi_config->{'publicinbox.nntpserver'};
17         if (!defined($name) or $name eq '') {
18                 $name = hostname;
19         } elsif (ref($name) eq 'ARRAY') {
20                 $name = $name->[0];
21         }
22
23         bless {
24                 groups => {},
25                 err => \*STDERR,
26                 out => \*STDOUT,
27                 pi_config => $pi_config,
28                 servername => $name,
29                 greet => \"201 $name ready - post via email\r\n",
30                 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
31                 # idler => PublicInbox::InboxIdle
32         }, $class;
33 }
34
35 sub refresh_groups {
36         my ($self, $sig) = @_;
37         my $pi_config = $sig ? PublicInbox::Config->new : $self->{pi_config};
38         my $groups = $pi_config->{-by_newsgroup}; # filled during each_inbox
39         $pi_config->each_inbox(sub {
40                 my ($ibx) = @_;
41                 my $ngname = $ibx->{newsgroup} // return;
42                 if ($ibx->nntp_usable) { # only valid if msgmap and over works
43                         # preload to avoid fragmentation:
44                         $ibx->description;
45                         $ibx->base_url;
46                 } else {
47                         delete $groups->{$ngname};
48                 }
49         });
50         $self->{groupnames} = [ sort(keys %$groups) ];
51         # this will destroy old groups that got deleted
52         $self->{pi_config} = $pi_config;
53 }
54
55 sub idler_start {
56         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
57 }
58
59 1;