]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / IMAPD.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # represents an IMAPD (currently a singleton),
5 # see script/public-inbox-imapd for how it is used
6 package PublicInbox::IMAPD;
7 use strict;
8 use PublicInbox::Config;
9 use PublicInbox::ConfigIter;
10 use PublicInbox::InboxIdle;
11 use PublicInbox::IMAP;
12 use PublicInbox::DummyInbox;
13 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
14
15 sub new {
16         my ($class) = @_;
17         bless {
18                 mailboxes => {},
19                 err => \*STDERR,
20                 out => \*STDOUT,
21                 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
22                 # pi_cfg => PublicInbox::Config
23                 # idler => PublicInbox::InboxIdle
24         }, $class;
25 }
26
27 sub imapd_refresh_ibx { # pi_cfg->each_inbox cb
28         my ($ibx, $imapd) = @_;
29         my $ngname = $ibx->{newsgroup} or return;
30
31         # We require lower-case since IMAP mailbox names are
32         # case-insensitive (but -nntpd matches INN in being
33         # case-sensitive
34         if ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
35                         # don't confuse with 50K slices
36                         $ngname =~ /\.[0-9]+\z/) {
37                 warn "mailbox name invalid: newsgroup=`$ngname'\n";
38                 return;
39         }
40         $ibx->over or return;
41         $ibx->{over} = undef;
42
43         # RFC 3501 2.3.1.1 -  "A good UIDVALIDITY value to use in
44         # this case is a 32-bit representation of the creation
45         # date/time of the mailbox"
46         eval { $ibx->uidvalidity };
47         my $mm = delete($ibx->{mm}) or return;
48         defined($ibx->{uidvalidity}) or return;
49         PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx, $mm->max);
50
51         # preload to avoid fragmentation:
52         $ibx->description;
53         $ibx->base_url;
54
55         # ensure dummies are selectable
56         my $dummies = $imapd->{dummies};
57         do {
58                 $dummies->{$ngname} = $dummy;
59         } while ($ngname =~ s/\.[^\.]+\z//);
60 }
61
62 sub imapd_refresh_finalize {
63         my ($imapd, $pi_cfg) = @_;
64         my $mailboxes;
65         if (my $next = delete $imapd->{imapd_next}) {
66                 $imapd->{mailboxes} = delete $next->{mailboxes};
67                 $mailboxes = delete $next->{dummies};
68         } else {
69                 $mailboxes = delete $imapd->{dummies};
70         }
71         %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
72         $imapd->{mailboxes} = $mailboxes;
73         $imapd->{inboxlist} = [
74                 map {
75                         my $no = $mailboxes->{$_} == $dummy ? '' : 'No';
76                         my $u = $_; # capitalize "INBOX" for user-familiarity
77                         $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
78                         qq[* LIST (\\Has${no}Children) "." $u\r\n]
79                 } keys %$mailboxes
80         ];
81         $imapd->{pi_cfg} = $pi_cfg;
82         if (my $idler = $imapd->{idler}) {
83                 $idler->refresh($pi_cfg);
84         }
85 }
86
87 sub imapd_refresh_step { # pi_cfg->iterate_start cb
88         my ($pi_cfg, $section, $imapd) = @_;
89         if (defined($section)) {
90                 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
91                 my $ibx = $pi_cfg->lookup_name($1) or return;
92                 imapd_refresh_ibx($ibx, $imapd->{imapd_next});
93         } else { # undef == "EOF"
94                 imapd_refresh_finalize($imapd, $pi_cfg);
95         }
96 }
97
98 sub refresh_groups {
99         my ($self, $sig) = @_;
100         my $pi_cfg = PublicInbox::Config->new;
101         if ($sig) { # SIGHUP is handled through the event loop
102                 $self->{imapd_next} = { dummies => {}, mailboxes => {} };
103                 my $iter = PublicInbox::ConfigIter->new($pi_cfg,
104                                                 \&imapd_refresh_step, $self);
105                 $iter->event_step;
106         } else { # initial start is synchronous
107                 $self->{dummies} = {};
108                 $pi_cfg->each_inbox(\&imapd_refresh_ibx, $self);
109                 imapd_refresh_finalize($self, $pi_cfg);
110         }
111 }
112
113 sub idler_start {
114         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
115 }
116
117 1;