]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-nntpd
23d269d4b778c9ffb6480f81ef3154eac56a72b3
[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 #
5 # Standalone NNTP server for public-inbox.
6 use strict;
7 use warnings;
8 require PublicInbox::Daemon;
9 require PublicInbox::NewsGroup;
10 require PublicInbox::NNTP;
11 require PublicInbox::Config;
12 my $nntpd = PublicInbox::NNTPD->new;
13 daemon_run('0.0.0.0:119',
14         sub { $nntpd->refresh_groups }, # refresh
15         sub ($$$) { PublicInbox::NNTP->new($_[0], $nntpd) }); # post_accept
16
17 1;
18 package PublicInbox::NNTPD;
19 use strict;
20 use warnings;
21
22 sub new {
23         my ($class) = @_;
24         bless {
25                 groups => {},
26                 err => \*STDERR,
27                 out => \*STDOUT,
28                 grouplist => [],
29         }, $class;
30 }
31
32 sub refresh_groups () {
33         my ($self) = @_;
34         my $pi_config = PublicInbox::Config->new;
35         my $new = {};
36         my @list;
37         foreach my $k (keys %$pi_config) {
38                 $k =~ /\Apublicinbox\.([^\.]+)\.mainrepo\z/ or next;
39                 my $g = $1;
40                 my $git_dir = $pi_config->{$k};
41                 my $addr = $pi_config->{"publicinbox.$g.address"};
42                 my $ngname = $pi_config->{"publicinbox.$g.newsgroup"};
43                 if (defined $ngname) {
44                         next if ($ngname eq ''); # disabled
45                         $g = $ngname;
46                 }
47                 my $ng = PublicInbox::NewsGroup->new($g, $git_dir, $addr);
48                 my $old_ng = $self->{groups}->{$g};
49
50                 # Reuse the old one if possible since it can hold
51                 # references to valid mm and gcf objects
52                 if ($old_ng) {
53                         $old_ng->update($ng);
54                         $ng = $old_ng;
55                 }
56
57                 # Only valid if msgmap and search works
58                 if ($ng->usable) {
59                         $new->{$g} = $ng;
60                         push @list, $ng;
61                 }
62         }
63         @list = sort { $a->{name} cmp $b->{name} } @list;
64         $self->{grouplist} = \@list;
65         # this will destroy old groups that got deleted
66         %{$self->{groups}} = %$new;
67 }
68
69 1;