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