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::InboxIdle;
10 use PublicInbox::IMAP;
11 use PublicInbox::DummyInbox;
12 my $dummy = bless { uidvalidity => 0 }, 'PublicInbox::DummyInbox';
20 # accept_tls => { SSL_server => 1, ..., SSL_reuse_ctx => ... }
21 # pi_config => PublicInbox::Config
22 # idler => PublicInbox::InboxIdle
26 sub imapd_refresh_ibx { # pi_config->each_inbox cb
27 my ($ibx, $imapd) = @_;
28 my $ngname = $ibx->{newsgroup} or return;
30 warn 'multiple newsgroups not supported: '.
31 join(', ', @$ngname). "\n";
33 } elsif ($ngname =~ m![^a-z0-9/_\.\-\~\@\+\=:]! ||
34 $ngname =~ /\.[0-9]+\z/) {
35 warn "mailbox name invalid: newsgroup=`$ngname'\n";
40 my $mm = $ibx->mm or return;
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 defined($ibx->{uidvalidity} = $mm->created_at) or return;
47 PublicInbox::IMAP::ensure_slices_exist($imapd, $ibx, $mm->max // 0);
49 # preload to avoid fragmentation:
53 # ensure dummies are selectable
54 my $dummies = $imapd->{dummies};
56 $dummies->{$ngname} = $dummy;
57 } while ($ngname =~ s/\.[^\.]+\z//);
60 sub imapd_refresh_finalize {
61 my ($imapd, $pi_config) = @_;
63 if (my $next = delete $imapd->{imapd_next}) {
64 $imapd->{mailboxes} = delete $next->{mailboxes};
65 $mailboxes = delete $next->{dummies};
67 $mailboxes = delete $imapd->{dummies};
69 %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
70 $imapd->{mailboxes} = $mailboxes;
71 $imapd->{inboxlist} = [
73 my $no = $mailboxes->{$_} == $dummy ? '' : 'No';
74 my $u = $_; # capitalize "INBOX" for user-familiarity
75 $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
76 qq[* LIST (\\Has${no}Children) "." $u\r\n]
79 $imapd->{pi_config} = $pi_config;
80 if (my $idler = $imapd->{idler}) {
81 $idler->refresh($pi_config);
85 sub imapd_refresh_step { # pi_config->iterate_start cb
86 my ($pi_config, $section, $imapd) = @_;
87 if (defined($section)) {
88 return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
89 my $ibx = $pi_config->lookup_name($1) or return;
90 imapd_refresh_ibx($ibx, $imapd->{imapd_next});
91 } else { # undef == "EOF"
92 imapd_refresh_finalize($imapd, $pi_config);
97 my ($self, $sig) = @_;
98 my $pi_config = PublicInbox::Config->new;
99 if ($sig) { # SIGHUP is handled through the event loop
100 $self->{imapd_next} = { dummies => {}, mailboxes => {} };
101 $pi_config->iterate_start(\&imapd_refresh_step, $self);
102 PublicInbox::DS::requeue($pi_config); # call event_step
103 } else { # initial start is synchronous
104 $self->{dummies} = {};
105 $pi_config->each_inbox(\&imapd_refresh_ibx, $self);
106 imapd_refresh_finalize($self, $pi_config);
111 $_[0]->{idler} //= PublicInbox::InboxIdle->new($_[0]->{pi_config});