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 (currently a singleton),
5 # see script/public-inbox-imapd for how it is used
6 package PublicInbox::IMAPD;
9 use PublicInbox::Config;
10 use PublicInbox::ConfigIter;
11 use PublicInbox::InboxIdle;
12 use PublicInbox::IMAPdeflate; # loads PublicInbox::IMAP
13 use PublicInbox::DummyInbox;
14 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
22 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
23 # pi_cfg => PublicInbox::Config
24 # idler => PublicInbox::InboxIdle
28 sub imapd_refresh_ibx { # pi_cfg->each_inbox cb
29 my ($ibx, $imapd) = @_;
30 my $ngname = $ibx->{newsgroup} or return;
32 # We require lower-case since IMAP mailbox names are
33 # case-insensitive (but -nntpd matches INN in being
35 if ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
36 # don't confuse with 50K slices
37 $ngname =~ /\.[0-9]+\z/) {
38 warn "mailbox name invalid: newsgroup=`$ngname'\n";
44 # RFC 3501 2.3.1.1 - "A good UIDVALIDITY value to use in
45 # this case is a 32-bit representation of the creation
46 # date/time of the mailbox"
47 eval { $ibx->uidvalidity };
48 my $mm = delete($ibx->{mm}) or return;
49 defined($ibx->{uidvalidity}) or return;
50 PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx, $mm->max);
52 # preload to avoid fragmentation:
56 # ensure dummies are selectable
57 my $dummies = $imapd->{dummies};
59 $dummies->{$ngname} = $dummy;
60 } while ($ngname =~ s/\.[^\.]+\z//);
63 sub imapd_refresh_finalize {
64 my ($imapd, $pi_cfg) = @_;
66 if (my $next = delete $imapd->{imapd_next}) {
67 $imapd->{mailboxes} = delete $next->{mailboxes};
68 $mailboxes = delete $next->{dummies};
70 $mailboxes = delete $imapd->{dummies};
72 %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
73 $imapd->{mailboxes} = $mailboxes;
74 $imapd->{mailboxlist} = [
76 sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
78 my $u = $_; # capitalize "INBOX" for user-familiarity
79 $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
80 if ($mailboxes->{$_} == $dummy) {
82 qq[* LIST (\\HasChildren) "." $u\r\n]]
84 $u =~ /\A(.+)\.([0-9]+)\z/ or
85 die "BUG: `$u' has no slice digit(s)";
87 qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
91 $imapd->{pi_cfg} = $pi_cfg;
92 if (my $idler = $imapd->{idler}) {
93 $idler->refresh($pi_cfg);
97 sub imapd_refresh_step { # PublicInbox::ConfigIter cb
98 my ($pi_cfg, $section, $imapd) = @_;
99 if (defined($section)) {
100 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
101 my $ibx = $pi_cfg->lookup_name($1) or return;
102 imapd_refresh_ibx($ibx, $imapd->{imapd_next});
103 } else { # undef == "EOF"
104 imapd_refresh_finalize($imapd, $pi_cfg);
109 my ($self, $sig) = @_;
110 my $pi_cfg = PublicInbox::Config->new;
111 if ($sig) { # SIGHUP is handled through the event loop
112 $self->{imapd_next} = { dummies => {}, mailboxes => {} };
113 my $iter = PublicInbox::ConfigIter->new($pi_cfg,
114 \&imapd_refresh_step, $self);
116 } else { # initial start is synchronous
117 $self->{dummies} = {};
118 $pi_cfg->each_inbox(\&imapd_refresh_ibx, $self);
119 imapd_refresh_finalize($self, $pi_cfg);
124 $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_cfg});