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