]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NNTPD.pm
nntp: use grep operation for wildmat matching
[public-inbox.git] / lib / PublicInbox / NNTPD.pm
1 # Copyright (C) 2016-2020 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 warnings;
9 use Sys::Hostname;
10 use PublicInbox::Config;
11 use PublicInbox::InboxIdle;
12
13 sub new {
14         my ($class) = @_;
15         my $pi_config = PublicInbox::Config->new;
16         my $name = $pi_config->{'publicinbox.nntpserver'};
17         if (!defined($name) or $name eq '') {
18                 $name = hostname;
19         } elsif (ref($name) eq 'ARRAY') {
20                 $name = $name->[0];
21         }
22
23         bless {
24                 groups => {},
25                 err => \*STDERR,
26                 out => \*STDOUT,
27                 grouplist => [],
28                 pi_config => $pi_config,
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_config = $sig ? PublicInbox::Config->new : $self->{pi_config};
39         my $groups = $pi_config->{-by_newsgroup}; # filled during each_inbox
40         $pi_config->each_inbox(sub {
41                 my ($ibx) = @_;
42                 my $ngname = $ibx->{newsgroup} or return;
43                 if (ref $ngname) {
44                         warn 'multiple newsgroups not supported: '.
45                                 join(', ', @$ngname). "\n";
46                 # Newsgroup name needs to be compatible with RFC 3977
47                 # wildmat-exact and RFC 3501 (IMAP) ATOM-CHAR.
48                 # Leave out a few chars likely to cause problems or conflicts:
49                 # '|', '<', '>', ';', '#', '$', '&',
50                 } elsif ($ngname =~ m![^A-Za-z0-9/_\.\-\~\@\+\=:]!) {
51                         warn "newsgroup name invalid: `$ngname'\n";
52                         delete $groups->{$ngname};
53                 } elsif ($ibx->nntp_usable) {
54                         # Only valid if msgmap and search works
55
56                         # preload to avoid fragmentation:
57                         $ibx->description;
58                         $ibx->base_url;
59                 } else {
60                         delete $groups->{$ngname};
61                 }
62         });
63         my @names = sort(keys %$groups);
64         $self->{grouplist} = [ map { $groups->{$_} } @names ];
65         $self->{groupnames} = \@names;
66         $self->{pi_config} = $pi_config;
67         # this will destroy old groups that got deleted
68         $self->{groups} = $groups;
69 }
70
71 sub idler_start {
72         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});
73 }
74
75 1;