]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NNTPD.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / NNTPD.pm
1 # Copyright (C) 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 v5.10.1;
9 use Sys::Hostname;
10 use PublicInbox::Config;
11 use PublicInbox::InboxIdle;
12 use PublicInbox::NNTP;
13
14 sub new {
15         my ($class) = @_;
16         bless {
17                 err => \*STDERR,
18                 out => \*STDOUT,
19                 # pi_cfg => $pi_cfg,
20                 # ssl_ctx_opt => { SSL_cert_file => ..., SSL_key_file => ... }
21                 # idler => PublicInbox::InboxIdle
22         }, $class;
23 }
24
25 sub refresh_groups {
26         my ($self, $sig) = @_;
27         my $pi_cfg = PublicInbox::Config->new;
28         my $name = $pi_cfg->{'publicinbox.nntpserver'};
29         if (!defined($name) or $name eq '') {
30                 $name = hostname;
31         } elsif (ref($name) eq 'ARRAY') {
32                 $name = $name->[0];
33         }
34         if ($name ne ($self->{servername} // '')) {
35                 $self->{servername} = $name;
36                 $self->{greet} = \"201 $name ready - post via email\r\n";
37         }
38         my $groups = $pi_cfg->{-by_newsgroup}; # filled during each_inbox
39         my $cache = eval { $pi_cfg->ALL->misc->nntpd_cache_load } // {};
40         $pi_cfg->each_inbox(sub {
41                 my ($ibx) = @_;
42                 my $ngname = $ibx->{newsgroup} // return;
43                 my $ce = $cache->{$ngname};
44                 if (($ce and (%$ibx = (%$ibx, %$ce))) || $ibx->nntp_usable) {
45                         # only valid if msgmap and over works
46                         # preload to avoid fragmentation:
47                         $ibx->description;
48                 } else {
49                         delete $groups->{$ngname};
50                         # Note: don't be tempted to delete more for memory
51                         # savings just yet: NNTP, IMAP, and WWW may all
52                         # run in the same process someday.
53                 }
54         });
55         @{$self->{groupnames}} = sort(keys %$groups);
56         # this will destroy old groups that got deleted
57         $self->{pi_cfg} = $pi_cfg;
58 }
59
60 sub idler_start {
61         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
62 }
63
64 1;