]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/NNTPD.pm
imap: support IDLE
[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
12 sub new {
13         my ($class) = @_;
14         my $pi_config = PublicInbox::Config->new;
15         my $name = $pi_config->{'publicinbox.nntpserver'};
16         if (!defined($name) or $name eq '') {
17                 $name = hostname;
18         } elsif (ref($name) eq 'ARRAY') {
19                 $name = $name->[0];
20         }
21
22         bless {
23                 groups => {},
24                 err => \*STDERR,
25                 out => \*STDOUT,
26                 grouplist => [],
27                 servername => $name,
28                 greet => \"201 $name ready - post via email\r\n",
29                 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
30         }, $class;
31 }
32
33 sub refresh_groups {
34         my ($self, $pi_config) = @_;
35         $pi_config //= PublicInbox::Config->new;
36         my $new = {};
37         my @list;
38         $pi_config->each_inbox(sub {
39                 my ($ng) = @_;
40                 my $ngname = $ng->{newsgroup} or return;
41                 if (ref $ngname) {
42                         warn 'multiple newsgroups not supported: '.
43                                 join(', ', @$ngname). "\n";
44                 # Newsgroup name needs to be compatible with RFC 3977
45                 # wildmat-exact and RFC 3501 (IMAP) ATOM-CHAR.
46                 # Leave out a few chars likely to cause problems or conflicts:
47                 # '|', '<', '>', ';', '#', '$', '&',
48                 } elsif ($ngname =~ m![^A-Za-z0-9/_\.\-\~\@\+\=:]!) {
49                         warn "newsgroup name invalid: `$ngname'\n";
50                 } elsif ($ng->nntp_usable) {
51                         # Only valid if msgmap and search works
52                         $new->{$ngname} = $ng;
53                         push @list, $ng;
54
55                         # preload to avoid fragmentation:
56                         $ng->description;
57                         $ng->base_url;
58                 }
59         });
60         @list = sort { $a->{newsgroup} cmp $b->{newsgroup} } @list;
61         $self->{grouplist} = \@list;
62         # this will destroy old groups that got deleted
63         %{$self->{groups}} = %$new;
64 }
65
66 1;