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>
4 # represents an IMAPD (currently a singleton),
5 # see script/public-inbox-imapd for how it is used
6 package PublicInbox::IMAPD;
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';
21 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
22 # pi_cfg => PublicInbox::Config
23 # idler => PublicInbox::InboxIdle
27 sub imapd_refresh_ibx { # pi_cfg->each_inbox cb
28 my ($ibx, $imapd) = @_;
29 my $ngname = $ibx->{newsgroup} or return;
31 # We require lower-case since IMAP mailbox names are
32 # case-insensitive (but -nntpd matches INN in being
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";
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);
51 # preload to avoid fragmentation:
55 # ensure dummies are selectable
56 my $dummies = $imapd->{dummies};
58 $dummies->{$ngname} = $dummy;
59 } while ($ngname =~ s/\.[^\.]+\z//);
62 sub imapd_refresh_finalize {
63 my ($imapd, $pi_cfg) = @_;
65 if (my $next = delete $imapd->{imapd_next}) {
66 $imapd->{mailboxes} = delete $next->{mailboxes};
67 $mailboxes = delete $next->{dummies};
69 $mailboxes = delete $imapd->{dummies};
71 %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
72 $imapd->{mailboxes} = $mailboxes;
73 $imapd->{mailboxlist} = [
75 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
77 my $u = $_; # capitalize "INBOX" for user-familiarity
78 $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
79 if ($mailboxes->{$_} == $dummy) {
81 qq[* LIST (\\HasChildren) "." $u\r\n]]
83 $u =~ /\A(.+)\.([0-9]+)\z/ or
84 die "BUG: `$u' has no slice digit(s)";
86 qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
90 $imapd->{pi_cfg} = $pi_cfg;
91 if (my $idler = $imapd->{idler}) {
92 $idler->refresh($pi_cfg);
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);
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);
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);
123 $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});