]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
6aa3d12fa1e76eb4a2ab20008e281a9dfad03ec8
[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->{mailboxlist} = [
74                 map { $_->[2] }
75                 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
76                 map {
77                         my $u = $_; # capitalize "INBOX" for user-familiarity
78                         $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
79                         if ($mailboxes->{$_} == $dummy) {
80                                 [ $u, -1,
81                                   qq[* LIST (\\HasChildren) "." $u\r\n]]
82                         } else {
83                                 $u =~ /\A(.+)\.([0-9]+)\z/ or
84                                         die "BUG: `$u' has no slice digit(s)";
85                                 [ $1, $2 + 0,
86                                   qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
87                         }
88                 } keys %$mailboxes
89         ];
90         $imapd->{pi_cfg} = $pi_cfg;
91         if (my $idler = $imapd->{idler}) {
92                 $idler->refresh($pi_cfg);
93         }
94 }
95
96 sub imapd_refresh_step { # pi_cfg->iterate_start cb
97         my ($pi_cfg, $section, $imapd) = @_;
98         if (defined($section)) {
99                 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
100                 my $ibx = $pi_cfg->lookup_name($1) or return;
101                 imapd_refresh_ibx($ibx, $imapd->{imapd_next});
102         } else { # undef == "EOF"
103                 imapd_refresh_finalize($imapd, $pi_cfg);
104         }
105 }
106
107 sub refresh_groups {
108         my ($self, $sig) = @_;
109         my $pi_cfg = PublicInbox::Config->new;
110         if ($sig) { # SIGHUP is handled through the event loop
111                 $self->{imapd_next} = { dummies => {}, mailboxes => {} };
112                 my $iter = PublicInbox::ConfigIter->new($pi_cfg,
113                                                 \&imapd_refresh_step, $self);
114                 $iter->event_step;
115         } else { # initial start is synchronous
116                 $self->{dummies} = {};
117                 $pi_cfg->each_inbox(\&imapd_refresh_ibx, $self);
118                 imapd_refresh_finalize($self, $pi_cfg);
119         }
120 }
121
122 sub idler_start {
123         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
124 }
125
126 1;