]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAP.pm
imap: ensure_slices_exist: drop needless map and array
[public-inbox.git] / lib / PublicInbox / IMAP.pm
index ce0dce0f317e8fb40a2a0b9da160594cafeb4fcf..bed633e50789cccb8b1790e29b5d752e6c6ff455 100644 (file)
@@ -121,7 +121,7 @@ sub capa ($) {
                $capa .= ' COMPRESS=DEFLATE';
        } else {
                if (!($self->{sock} // $self)->can('accept_SSL') &&
-                       $self->{imapd}->{accept_tls}) {
+                       $self->{imapd}->{ssl_ctx_opt}) {
                        $capa .= ' STARTTLS';
                }
                $capa .= ' AUTH=ANONYMOUS';
@@ -350,21 +350,18 @@ sub idle_done ($$) {
        "$idle_tag OK Idle done\r\n";
 }
 
-sub ensure_slices_exist ($$$) {
-       my ($imapd, $ibx, $max) = @_;
-       defined(my $mb_top = $ibx->{newsgroup}) or return;
+sub ensure_slices_exist ($$) {
+       my ($imapd, $ibx) = @_;
+       my $mb_top = $ibx->{newsgroup} // return;
        my $mailboxes = $imapd->{mailboxes};
-       my @created;
-       for (my $i = int($max/UID_SLICE); $i >= 0; --$i) {
+       my $list = $imapd->{mailboxlist}; # may be undef, just autoviv + noop
+       for (my $i = int($ibx->art_max/UID_SLICE); $i >= 0; --$i) {
                my $sub_mailbox = "$mb_top.$i";
                last if exists $mailboxes->{$sub_mailbox};
                $mailboxes->{$sub_mailbox} = $ibx;
                $sub_mailbox =~ s/\Ainbox\./INBOX./i; # more familiar to users
-               push @created, $sub_mailbox;
+               push @$list, qq[* LIST (\\HasNoChildren) "." $sub_mailbox\r\n]
        }
-       return unless @created;
-       my $l = $imapd->{mailboxlist} or return;
-       push @$l, map { qq[* LIST (\\HasNoChildren) "." $_\r\n] } @created;
 }
 
 sub inbox_lookup ($$;$) {
@@ -387,7 +384,8 @@ sub inbox_lookup ($$;$) {
                        my $uid_end = $uid_base + UID_SLICE;
                        $exists = $over->imap_exists($uid_base, $uid_end);
                }
-               ensure_slices_exist($self->{imapd}, $ibx, $over->max);
+               delete $ibx->{-art_max};
+               ensure_slices_exist($self->{imapd}, $ibx);
        } else {
                if ($examine) {
                        $self->{uid_base} = $uid_base;
@@ -396,9 +394,9 @@ sub inbox_lookup ($$;$) {
                }
                # if "INBOX.foo.bar" is selected and "INBOX.foo.bar.0",
                # check for new UID ranges (e.g. "INBOX.foo.bar.1")
-               if (my $z = $self->{imapd}->{mailboxes}->{"$mailbox.0"}) {
-                       ensure_slices_exist($self->{imapd}, $z,
-                                               $z->over(1)->max);
+               if (my $ibx = $self->{imapd}->{mailboxes}->{"$mailbox.0"}) {
+                       delete $ibx->{-art_max};
+                       ensure_slices_exist($self->{imapd}, $ibx);
                }
        }
        ($ibx, $exists, $uidmax + 1, $uid_base);
@@ -595,7 +593,7 @@ sub fetch_blob_cb { # called by git->cat_async via ibx_async_cat
                                        \&fetch_blob_cb, $fetch_arg);
        }
        fetch_run_ops($self, $smsg, $bref, $ops, $partial);
-       $pre ? $self->zflush : $self->requeue_once;
+       $pre ? $self->dflush : $self->requeue_once;
 }
 
 sub emit_rfc822 {
@@ -1212,8 +1210,6 @@ sub event_step {
        $self->requeue unless $pending;
 }
 
-sub compressed { undef }
-
 # RFC 4978
 sub cmd_compress ($$$) {
        my ($self, $tag, $alg) = @_;
@@ -1223,21 +1219,21 @@ sub cmd_compress ($$$) {
        # CRIME made TLS compression obsolete
        # return "$tag NO [COMPRESSIONACTIVE]\r\n" if $self->tls_compressed;
 
-       PublicInbox::IMAPdeflate->enable($self, $tag);
+       PublicInbox::IMAPdeflate->enable($self) or return
+                               \"$tag BAD failed to activate compression\r\n";
+       PublicInbox::DS::write($self, \"$tag OK DEFLATE active\r\n");
        $self->requeue;
        undef
 }
 
 sub cmd_starttls ($$) {
        my ($self, $tag) = @_;
-       my $sock = $self->{sock} or return;
-       if ($sock->can('stop_SSL') || $self->compressed) {
+       (($self->{sock} // return)->can('stop_SSL') || $self->compressed) and
                return "$tag BAD TLS or compression already enabled\r\n";
-       }
-       my $opt = $self->{imapd}->{accept_tls} or
+       $self->{imapd}->{ssl_ctx_opt} or
                return "$tag BAD can not initiate TLS negotiation\r\n";
        $self->write(\"$tag OK begin TLS negotiation now\r\n");
-       $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
+       PublicInbox::TLS::start($self->{sock}, $self->{imapd});
        $self->requeue if PublicInbox::DS::accept_tls_step($self);
        undef;
 }
@@ -1269,4 +1265,8 @@ our @ISA = qw(PublicInbox::IMAP);
 
 sub logged_in { 0 }
 
+package PublicInbox::IMAPdeflate;
+use PublicInbox::DSdeflate;
+our @ISA = qw(PublicInbox::DSdeflate PublicInbox::IMAP);
+
 1;