]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-nntpd
nntpd: hoist out daemon management code
[public-inbox.git] / public-inbox-nntpd
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 use strict;
5 use warnings;
6 require PublicInbox::Daemon;
7 require PublicInbox::NewsGroup;
8 require PublicInbox::NNTP;
9 my $nntpd = PublicInbox::NNTPD->new;
10 daemon_run('0.0.0.0:119',
11         sub { $nntpd->refresh_groups },
12         sub ($) { PublicInbox::NNTP->new($_[0], $nntpd) });
13
14 1;
15 package PublicInbox::NNTPD;
16 use strict;
17 use warnings;
18 use fields qw(groups grouplist err out);
19
20 sub new {
21         my ($class) = @_;
22         my $self = fields::new($class);
23         $self->{groups} = {};
24         $self->{err} = \*STDERR;
25         $self->{out} = \*STDOUT;
26         $self->{grouplist} = [];
27         $self;
28 }
29
30 sub refresh_groups () {
31         my ($self) = @_;
32         require PublicInbox::Config;
33         my $pi_config = PublicInbox::Config->new;
34         my $new = {};
35         my @list;
36         foreach my $k (keys %$pi_config) {
37                 $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
38                 my $g = $1;
39                 my $git_dir = $pi_config->{$k};
40                 my $addr = $pi_config->{"publicinbox.$g.address"};
41                 my $ngname = $pi_config->{"publicinbox.$g.newsgroup"};
42                 if (defined $ngname) {
43                         next if ($ngname eq ''); # disabled
44                         $g = $ngname;
45                 }
46                 my $ng = PublicInbox::NewsGroup->new($g, $git_dir, $addr);
47                 my $old_ng = $self->{groups}->{$g};
48
49                 # Reuse the old one if possible since it can hold
50                 # references to valid mm and gcf objects
51                 if ($old_ng) {
52                         $old_ng->update($ng);
53                         $ng = $old_ng;
54                 }
55
56                 # Only valid if Msgmap works
57                 if ($ng->mm(1)) {
58                         $new->{$g} = $ng;
59                         push @list, $ng;
60                 }
61         }
62         @list = sort { $a->{name} cmp $b->{name} } @list;
63         $self->{grouplist} = \@list;
64         # this will destroy old groups that got deleted
65         %{$self->{groups}} = %$new;
66 }
67
68 1;