]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/IMAP.pm
imap: always send EXISTS on uo2m_extend
[public-inbox.git] / lib / PublicInbox / IMAP.pm
index dd983dfd2822b90d08a03f2b415b5205a1b0c25c..888c9becfe0afc73610615288517a0c5f5ced8ea 100644 (file)
@@ -166,7 +166,7 @@ sub cmd_login ($$$$) {
 
 sub cmd_close ($$) {
        my ($self, $tag) = @_;
-       delete $self->{uid_base};
+       delete @$self{qw(uid_base uo2m)};
        delete $self->{ibx} ? "$tag OK Close done\r\n"
                                : "$tag BAD No mailbox\r\n";
 }
@@ -190,8 +190,6 @@ sub cmd_capability ($$) {
        '* '.capa($self)."\r\n$tag OK Capability done\r\n";
 }
 
-sub cmd_noop ($$) { "$_[1] OK Noop done\r\n" }
-
 # uo2m: UID Offset to MSN, this is an arrayref by default,
 # but uo2m_hibernate can compact and deduplicate it
 sub uo2m_ary_new ($) {
@@ -220,7 +218,7 @@ sub uo2m_hibernate ($) {
 
 sub uo2m_last_uid ($) {
        my ($self) = @_;
-       my $uo2m = $self->{uo2m} or die 'BUG: uo2m_last_uid w/o {uo2m}';
+       defined(my $uo2m = $self->{uo2m}) or die 'BUG: uo2m_last_uid w/o {uo2m}';
        (ref($uo2m) ? @$uo2m : (length($uo2m) >> 1)) + $self->{uid_base};
 }
 
@@ -233,7 +231,7 @@ sub uo2m_pack ($) {
 
 # extend {uo2m} to account for new messages which arrived since
 # {uo2m} was created.
-sub uo2m_extend ($$) {
+sub uo2m_extend ($$;$) {
        my ($self, $new_uid_max) = @_;
        defined(my $uo2m = $self->{uo2m}) or
                return($self->{uo2m} = uo2m_ary_new($self));
@@ -245,20 +243,30 @@ sub uo2m_extend ($$) {
        ++$beg;
        my $uids = $self->{ibx}->over->uid_range($beg, $base + UID_SLICE);
        my @tmp; # [$UID_OFFSET] => $MSN
+       my $write_method = $_[2] // 'msg_more';
        if (ref($uo2m)) {
                my $msn = $uo2m->[-1];
                $tmp[$_ - $beg] = ++$msn for @$uids;
+               $self->$write_method("* $msn EXISTS\r\n");
                push @$uo2m, @tmp;
                $uo2m;
        } else {
                my $msn = unpack('S', substr($uo2m, -2, 2));
                $tmp[$_ - $beg] = ++$msn for @$uids;
+               $self->$write_method("* $msn EXISTS\r\n");
                $uo2m .= uo2m_pack(\@tmp);
                my %dedupe = ($uo2m => undef);
                $self->{uo2m} = (keys %dedupe)[0];
        }
 }
 
+sub cmd_noop ($$) {
+       my ($self, $tag) = @_;
+       defined($self->{uid_base}) and
+               uo2m_extend($self, $self->{uid_base} + UID_SLICE);
+       \"$tag OK Noop done\r\n";
+}
+
 # the flexible version which works on scalars and array refs.
 # Must call uo2m_extend before this
 sub uid2msn ($$) {
@@ -295,14 +303,10 @@ sub msn_to_uid_range ($$) {
 # called by PublicInbox::InboxIdle
 sub on_inbox_unlock {
        my ($self, $ibx) = @_;
-       my $old = uo2m_last_uid($self);
        my $uid_end = $self->{uid_base} + UID_SLICE;
-       uo2m_extend($self, $uid_end);
+       uo2m_extend($self, $uid_end, 'write');
        my $new = uo2m_last_uid($self);
-       if ($new > $old) {
-               my $msn = uid2msn($self, $new);
-               $self->write(\"* $msn EXISTS\r\n");
-       } elsif ($new == $uid_end) { # max exceeded $uid_end
+       if ($new == $uid_end) { # max exceeded $uid_end
                # continue idling w/o inotify
                my $sock = $self->{sock} or return;
                $ibx->unsubscribe_unlock(fileno($sock));
@@ -329,14 +333,13 @@ sub cmd_idle ($$) {
        my ($self, $tag) = @_;
        # IDLE seems allowed by dovecot w/o a mailbox selected *shrug*
        my $ibx = $self->{ibx} or return "$tag BAD no mailbox selected\r\n";
-       $self->{-idle_tag} = $tag;
-       my $max = $ibx->over->max;
        my $uid_end = $self->{uid_base} + UID_SLICE;
+       uo2m_extend($self, $uid_end);
        my $sock = $self->{sock} or return;
        my $fd = fileno($sock);
+       $self->{-idle_tag} = $tag;
        # only do inotify on most recent slice
-       if ($max < $uid_end) {
-               uo2m_extend($self, $uid_end);
+       if ($ibx->over->max < $uid_end) {
                $ibx->subscribe_unlock($fd, $self);
                $self->{imapd}->idler_start;
        }
@@ -415,6 +418,7 @@ sub cmd_examine ($$$) {
        my ($ibx, $exists, $uidnext, $base) = inbox_lookup($self, $mailbox);
        return "$tag NO Mailbox doesn't exist: $mailbox\r\n" if !$ibx;
        $self->{uid_base} = $base;
+       delete $self->{uo2m};
 
        # XXX: do we need this? RFC 5162/7162
        my $ret = $self->{ibx} ? "* OK [CLOSED] previous closed\r\n" : '';
@@ -1154,13 +1158,8 @@ sub xap_append ($$$$) {
 }
 
 sub parse_query ($$) {
-       my ($self, $rest) = @_;
-       if (uc($rest->[0]) eq 'CHARSET') {
-               shift @$rest;
-               defined(my $c = shift @$rest) or return 'BAD missing charset';
-               $c =~ /\A(?:UTF-8|US-ASCII)\z/ or return 'NO [BADCHARSET]';
-       }
-       my $q = PublicInbox::IMAPsearchqp::parse($self, join(' ', @$rest));
+       my ($self, $query) = @_;
+       my $q = PublicInbox::IMAPsearchqp::parse($self, $query);
        if (ref($q)) {
                my $max = $self->{ibx}->over->max;
                my $beg = 1;
@@ -1176,16 +1175,13 @@ sub refill_xap ($$$$) {
        my $srch = $self->{ibx}->search;
        my $opt = { mset => 2, limit => 1000 };
        my $nshard = $srch->{nshard} // 1;
-       while (1) {
-               my $mset = $srch->query("$q uid:$beg..$end", $opt);
-               @$uids = map { mdocid($nshard, $_) } $mset->items;
-               if (@$uids) {
-                       $range_info->[0] = $uids->[-1] + 1; # update $beg
-                       return;
-               } else { # all done
-                       return 0;
-               }
+       my $mset = $srch->query("$q uid:$beg..$end", $opt);
+       @$uids = map { mdocid($nshard, $_) } $mset->items;
+       if (@$uids) {
+               $range_info->[0] = $uids->[-1] + 1; # update $beg
+               return; # possibly more
        }
+       0; # all done
 }
 
 sub search_xap_range { # long_response
@@ -1202,9 +1198,9 @@ sub search_xap_range { # long_response
 }
 
 sub search_common {
-       my ($self, $tag, $rest, $want_msn) = @_;
+       my ($self, $tag, $query, $want_msn) = @_;
        my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
-       my $q = parse_query($self, $rest);
+       my $q = parse_query($self, $query);
        return "$tag $q\r\n" if !ref($q);
        my ($sql, $range_info) = delete @$q{qw(sql range_info)};
        if (!scalar(keys %$q)) { # overview.sqlite3
@@ -1222,14 +1218,14 @@ sub search_common {
        }
 }
 
-sub cmd_uid_search ($$$;) {
-       my ($self, $tag) = splice(@_, 0, 2);
-       search_common($self, $tag, \@_);
+sub cmd_uid_search ($$$) {
+       my ($self, $tag, $query) = @_;
+       search_common($self, $tag, $query);
 }
 
 sub cmd_search ($$$;) {
-       my ($self, $tag) = splice(@_, 0, 2);
-       search_common($self, $tag, \@_, 1);
+       my ($self, $tag, $query) = @_;
+       search_common($self, $tag, $query, 1);
 }
 
 sub args_ok ($$) { # duplicated from PublicInbox::NNTP
@@ -1301,7 +1297,7 @@ sub long_step {
        } elsif ($more) { # $self->{wbuf}:
                $self->update_idle_time;
 
-               # control passed to $more may be a GitAsyncCat object
+               # control passed to git_async_cat if $more == \undef
                requeue_once($self) if !ref($more);
        } else { # all done!
                delete $self->{long_cb};