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 _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:
47 # ensure dummies are selectable:
49 $dummies->{$ngname} = $dummy;
50 } while ($ngname =~ s/\.[^\.]+\z//);
52 delete @$ibx{qw(mm over)};
56 my ($self, $sig) = @_;
57 my $pi_cfg = PublicInbox::Config->new;
58 $self->{mailboxes} = $pi_cfg->{-imap_mailboxes} // do {
59 my $mailboxes = $self->{mailboxes} = {};
60 my $cache = eval { $pi_cfg->ALL->misc->nntpd_cache_load } // {};
62 $pi_cfg->each_inbox(\&_refresh_ibx, $self, $cache, $dummies);
63 %$mailboxes = (%$dummies, %$mailboxes);
64 @{$pi_cfg->{-imap_mailboxlist}} = map { $_->[2] }
65 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
67 # 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 die
75 "BUG: `$u' has no slice digit(s)";
76 [ $1, $2 + 0, '* LIST '.
77 qq[(\\HasNoChildren) "." $u\r\n] ]
80 $pi_cfg->{-imap_mailboxes} = $mailboxes;
82 $self->{mailboxlist} = $pi_cfg->{-imap_mailboxlist} //
83 die 'BUG: no mailboxlist';
84 $self->{pi_cfg} = $pi_cfg;
85 if (my $idler = $self->{idler}) {
86 $idler->refresh($pi_cfg);
91 $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});
94 sub event_step { # called vai requeue for low-priority IMAP clients
96 my $imap = shift(@{$self->{-authed_q}}) // return;
97 PublicInbox::DS::requeue($self) if scalar(@{$self->{-authed_q}});
98 $imap->event_step; # PublicInbox::IMAP::event_step