]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/NNTPD.pm
nntp: xref_by_tc: simplify slightly
[public-inbox.git] / lib / PublicInbox / NNTPD.pm
index 925691bc52f4dd62d201fffc4404963675db2e26..03c56db30727ec2dbee9e205592bd459f78ef892 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # represents an NNTPD (currently a singleton),
@@ -8,6 +8,7 @@ use strict;
 use warnings;
 use Sys::Hostname;
 use PublicInbox::Config;
+use PublicInbox::InboxIdle;
 
 sub new {
        my ($class) = @_;
@@ -23,34 +24,41 @@ sub new {
                groups => {},
                err => \*STDERR,
                out => \*STDOUT,
-               grouplist => [],
+               pi_config => $pi_config,
                servername => $name,
                greet => \"201 $name ready - post via email\r\n",
                # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
+               # idler => PublicInbox::InboxIdle
        }, $class;
 }
 
-sub refresh_groups () {
-       my ($self) = @_;
-       my $pi_config = PublicInbox::Config->new;
-       my $new = {};
-       my @list;
+sub refresh_groups {
+       my ($self, $sig) = @_;
+       my $pi_config = $sig ? PublicInbox::Config->new : $self->{pi_config};
+       my $groups = $pi_config->{-by_newsgroup}; # filled during each_inbox
        $pi_config->each_inbox(sub {
-               my ($ng) = @_;
-               my $ngname = $ng->{newsgroup} or return;
-               if (ref $ngname) {
-                       warn 'multiple newsgroups not supported: '.
-                               join(', ', @$ngname). "\n";
-               } elsif ($ng->nntp_usable) {
-                       # Only valid if msgmap and search works
-                       $new->{$ngname} = $ng;
-                       push @list, $ng;
+               my ($ibx) = @_;
+               my $ngname = $ibx->{newsgroup};
+               if (defined($ngname) && $ibx->nntp_usable) {
+                       # only valid if msgmap and over works
+                       # preload to avoid fragmentation:
+                       $ibx->description;
+                       $ibx->base_url;
+               } else {
+                       delete $groups->{$ngname};
+                       delete $ibx->{newsgroup};
+                       # Note: don't be tempted to delete more for memory
+                       # savings just yet: NNTP, IMAP, and WWW may all
+                       # run in the same process someday.
                }
        });
-       @list = sort { $a->{newsgroup} cmp $b->{newsgroup} } @list;
-       $self->{grouplist} = \@list;
+       $self->{groupnames} = [ sort(keys %$groups) ];
        # this will destroy old groups that got deleted
-       %{$self->{groups}} = %$new;
+       $self->{pi_config} = $pi_config;
+}
+
+sub idler_start {
+       $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
 }
 
 1;