+sub imapd_refresh_finalize {
+ my ($imapd, $pi_cfg) = @_;
+ my $mailboxes;
+ if (my $next = delete $imapd->{imapd_next}) {
+ $imapd->{mailboxes} = delete $next->{mailboxes};
+ $mailboxes = delete $next->{dummies};
+ } else {
+ $mailboxes = delete $imapd->{dummies};
+ }
+ %$mailboxes = (%$mailboxes, %{$imapd->{mailboxes}});
+ $imapd->{mailboxes} = $mailboxes;
+ $imapd->{mailboxlist} = [
+ map { $_->[2] }
+ sort { $a->[0] cmp $b->[0] || $a->[1] <=> $b->[1] }
+ map {
+ my $u = $_; # capitalize "INBOX" for user-familiarity
+ $u =~ s/\Ainbox(\.|\z)/INBOX$1/i;
+ if ($mailboxes->{$_} == $dummy) {
+ [ $u, -1,
+ qq[* LIST (\\HasChildren) "." $u\r\n]]
+ } else {
+ $u =~ /\A(.+)\.([0-9]+)\z/ or
+ die "BUG: `$u' has no slice digit(s)";
+ [ $1, $2 + 0,
+ qq[* LIST (\\HasNoChildren) "." $u\r\n] ]
+ }
+ } keys %$mailboxes
+ ];
+ $imapd->{pi_cfg} = $pi_cfg;
+ if (my $idler = $imapd->{idler}) {
+ $idler->refresh($pi_cfg);
+ }
+}
+
+sub imapd_refresh_step { # PublicInbox::ConfigIter cb
+ my ($pi_cfg, $section, $imapd) = @_;
+ if (defined($section)) {
+ return if $section !~ m!\Apublicinbox\.([^/]+)\z!;
+ my $ibx = $pi_cfg->lookup_name($1) or return;
+ imapd_refresh_ibx($ibx, $imapd->{imapd_next});
+ } else { # undef == "EOF"
+ imapd_refresh_finalize($imapd, $pi_cfg);
+ }
+}