1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # represents an IMAPD, see script/public-inbox-imapd for how it is used
5 package PublicInbox::IMAPD;
8 use PublicInbox::Config;
9 use PublicInbox::InboxIdle;
10 use PublicInbox::IMAP;
11 use PublicInbox::DummyInbox;
12 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
20 # ssl_ctx_opt => { SSL_cert_file => ..., SSL_key_file => ... }
21 # pi_cfg => PublicInbox::Config
22 # idler => PublicInbox::InboxIdle
26 sub imapd_refresh_ibx { # pi_cfg->each_inbox cb
27 my ($ibx, $imapd, $cache, $dummies) = @_;
28 my $ngname = $ibx->{newsgroup} // return;
30 # We require lower-case since IMAP mailbox names are
31 # case-insensitive (but -nntpd matches INN in being
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";
39 my $ce = $cache->{$ngname};
40 %$ibx = (%$ibx, %$ce) if $ce;
41 # only valid if msgmap and over works:
42 if (defined($ibx->uidvalidity)) {
44 PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx);
45 # preload to avoid fragmentation:
48 # ensure dummies are selectable:
50 $dummies->{$ngname} = $dummy;
51 } while ($ngname =~ s/\.[^\.]+\z//);
53 delete @$ibx{qw(mm over)};
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 } // {};
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] }
68 my $u = $_; # capitalize "INBOX" for user-familiarity
69 $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
70 if ($mailboxes->{$_} == $dummy) {
72 qq[* LIST (\\HasChildren) "." $u\r\n]]
74 $u =~ /\A(.+)\.([0-9]+)\z/ or
75 die "BUG: `$u' has no slice digit(s)";
77 qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
80 $self->{pi_cfg} = $pi_cfg;
81 if (my $idler = $self->{idler}) {
82 $idler->refresh($pi_cfg);
87 $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});