1 # Copyright (C) 2020 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_config => PublicInbox::Config
23 # idler => PublicInbox::InboxIdle
27 sub imapd_refresh_ibx { # pi_config->each_inbox cb
28 my ($ibx, $imapd) = @_;
29 my $ngname = $ibx->{newsgroup} or return;
31 warn 'multiple newsgroups not supported: '.
32 join(', ', @$ngname). "\n";
34 } elsif ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
35 $ngname =~ /\.[0-9]+\z/) {
36 warn "mailbox name invalid: newsgroup=`$ngname'\n";
41 my $mm = $ibx->mm or return;
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 defined($ibx->{uidvalidity} = $mm->created_at) or return;
48 PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx, $mm->max // 0);
50 # preload to avoid fragmentation:
54 # ensure dummies are selectable
55 my $dummies = $imapd->{dummies};
57 $dummies->{$ngname} = $dummy;
58 } while ($ngname =~ s/\.[^\.]+\z//);
61 sub imapd_refresh_finalize {
62 my ($imapd, $pi_config) = @_;
64 if (my $next = delete $imapd->{imapd_next}) {
65 $imapd->{mailboxes} = delete $next->{mailboxes};
66 $mailboxes = delete $next->{dummies};
68 $mailboxes = delete $imapd->{dummies};
70 %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
71 $imapd->{mailboxes} = $mailboxes;
72 $imapd->{inboxlist} = [
74 my $no = $mailboxes->{$_} == $dummy ? '' : 'No';
75 my $u = $_; # capitalize "INBOX" for user-familiarity
76 $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
77 qq[* LIST (\\Has${no}Children) "." $u\r\n]
80 $imapd->{pi_config} = $pi_config;
81 if (my $idler = $imapd->{idler}) {
82 $idler->refresh($pi_config);
86 sub imapd_refresh_step { # pi_config->iterate_start cb
87 my ($pi_config, $section, $imapd) = @_;
88 if (defined($section)) {
89 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
90 my $ibx = $pi_config->lookup_name($1) or return;
91 imapd_refresh_ibx($ibx, $imapd->{imapd_next});
92 } else { # undef == "EOF"
93 imapd_refresh_finalize($imapd, $pi_config);
98 my ($self, $sig) = @_;
99 my $pi_config = PublicInbox::Config->new;
100 if ($sig) { # SIGHUP is handled through the event loop
101 $self->{imapd_next} = { dummies => {}, mailboxes => {} };
102 my $iter = PublicInbox::ConfigIter->new($pi_config,
103 \&imapd_refresh_step, $self);
105 } else { # initial start is synchronous
106 $self->{dummies} = {};
107 $pi_config->each_inbox(\&imapd_refresh_ibx, $self);
108 imapd_refresh_finalize($self, $pi_config);
113 $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});