]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IMAPD.pm
imapd: use nntpd_cache to speed up startup/reload time
[public-inbox.git] / lib / PublicInbox / IMAPD.pm
1 # Copyright (C) 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, see script/public-inbox-imapd for how it is used
5 package PublicInbox::IMAPD;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::Config;
9 use PublicInbox::InboxIdle;
10 use PublicInbox::IMAP;
11 use PublicInbox::DummyInbox;
12 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
13
14 sub new {
15         my ($class) = @_;
16         bless {
17                 # mailboxes => {},
18                 err => \*STDERR,
19                 out => \*STDOUT,
20                 # ssl_ctx_opt => { SSL_cert_file => ..., SSL_key_file => ... }
21                 # pi_cfg => PublicInbox::Config
22                 # idler => PublicInbox::InboxIdle
23         }, $class;
24 }
25
26 sub imapd_refresh_ibx { # pi_cfg->each_inbox cb
27         my ($ibx, $imapd, $cache, $dummies) = @_;
28         my $ngname = $ibx->{newsgroup} // return;
29
30         # We require lower-case since IMAP mailbox names are
31         # case-insensitive (but -nntpd matches INN in being
32         # case-sensitive)
33         if ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
34                         # don't confuse with 50K slices
35                         $ngname =~ /\.[0-9]+\z/) {
36                 warn "mailbox name invalid: newsgroup=`$ngname'\n";
37                 return;
38         }
39         my $ce = $cache->{$ngname};
40         %$ibx = (%$ibx, %$ce) if $ce;
41         # only valid if msgmap and over works:
42         if (defined($ibx->uidvalidity)) {
43                 # fill ->{mailboxes}:
44                 PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx);
45                 # preload to avoid fragmentation:
46                 $ibx->description;
47                 $ibx->base_url;
48                 # ensure dummies are selectable:
49                 do {
50                         $dummies->{$ngname} = $dummy;
51                 } while ($ngname =~ s/\.[^\.]+\z//);
52         }
53         delete @$ibx{qw(mm over)};
54 }
55
56 sub refresh_groups {
57         my ($self, $sig) = @_;
58         my $pi_cfg = PublicInbox::Config->new;
59         my $mailboxes = $self->{mailboxes} = {};
60         my $cache = eval { $pi_cfg->ALL->misc->nntpd_cache_load } // {};
61         my $dummies = {};
62         $pi_cfg->each_inbox(\&imapd_refresh_ibx, $self, $cache, $dummies);
63         %$dummies = (%$dummies, %$mailboxes);
64         $mailboxes = $self->{mailboxes} = $dummies;
65         @{$self->{mailboxlist}} = map { $_->[2] }
66                 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
67                 map {
68                         my $u = $_; # capitalize "INBOX" for user-familiarity
69                         $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
70                         if ($mailboxes->{$_} == $dummy) {
71                                 [ $u, -1,
72                                   qq[* LIST (\\HasChildren) "." $u\r\n]]
73                         } else {
74                                 $u =~ /\A(.+)\.([0-9]+)\z/ or
75                                         die "BUG: `$u' has no slice digit(s)";
76                                 [ $1, $2 + 0,
77                                   qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
78                         }
79                 } keys %$mailboxes;
80         $self->{pi_cfg} = $pi_cfg;
81         if (my $idler = $self->{idler}) {
82                 $idler->refresh($pi_cfg);
83         }
84 }
85
86 sub idler_start {
87         $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
88 }
89
90 1;