]> Sergey Matveev's repositories - public-inbox.git/commitdiff
imap: allow UID range search on timestamps
authorEric Wong <e@yhbt.net>
Wed, 10 Jun 2020 07:04:50 +0000 (07:04 +0000)
committerEric Wong <e@yhbt.net>
Sat, 13 Jun 2020 07:55:45 +0000 (07:55 +0000)
Since it seems somewhat common for IMAP clients to limit
searches by sent Date: or INTERNALDATE, we can rely on
the NNTP/WWW-optimized overview DB.

For other queries, we'll have to depend on the Xapian DB.

lib/PublicInbox/DummyInbox.pm
lib/PublicInbox/IMAP.pm
lib/PublicInbox/Over.pm
t/imapd.t

index e38f9e5a432a30045864287badd99cef2eacba4b..b6c48db1a0e1b16a5bdf8f25cfd8fa588c1d538c 100644 (file)
@@ -13,7 +13,7 @@ sub max { undef } # Msgmap::max
 sub msg_range { [] } # Msgmap::msg_range
 
 no warnings 'once';
-*query_xover = \&msg_range;
+*uid_range = *query_xover = \&msg_range;
 *over = \&mm;
 *subscribe_unlock = *unsubscribe_unlock =
        *get_art = *description = *base_url = \&max;
index f9af530aa191546519c0c9aa982d7345130f9cd6..78042b9e2eefa8c4d0ce3e4a8e0399bc4227a627 100644 (file)
@@ -811,10 +811,11 @@ sub parse_date ($) { # 02-Oct-1993
 }
 
 sub uid_search_uid_range { # long_response
-       my ($self, $tag, $beg, $end) = @_;
-       my $uids = $self->{ibx}->mm->msg_range($beg, $end, 'num');
+       my ($self, $tag, $beg, $end, $sql) = @_;
+       my $uids = $self->{ibx}->over->uid_range($$beg, $end, $sql);
        if (@$uids) {
-               $self->msg_more(join('', map { " $_->[0]" } @$uids));
+               $$beg = $uids->[-1] + 1;
+               $self->msg_more(join(' ', '', @$uids));
        } else {
                $self->write(\"\r\n$tag OK Search done\r\n");
                undef;
@@ -936,13 +937,15 @@ sub cmd_uid_search ($$$;) {
        my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
        my $q = parse_query($self, \@_);
        return "$tag $q\r\n" if !ref($q);
+       my $sql = delete $q->{sql};
 
        if (!scalar(keys %$q)) {
                $self->msg_more('* SEARCH');
                my $beg = $self->{uid_min} // 1;
                my $end = $ibx->mm->max;
                uid_clamp($self, \$beg, \$end);
-               long_response($self, \&uid_search_uid_range, $tag, \$beg, $end);
+               long_response($self, \&uid_search_uid_range,
+                               $tag, \$beg, $end, $sql);
        } elsif (my $uid = $q->{uid}) {
                if ($uid =~ /\A([0-9]+):([0-9]+|\*)\z/s) {
                        my ($beg, $end) = ($1, $2);
@@ -950,7 +953,7 @@ sub cmd_uid_search ($$$;) {
                        uid_clamp($self, \$beg, \$end);
                        $self->msg_more('* SEARCH');
                        long_response($self, \&uid_search_uid_range,
-                                       $tag, \$beg, $end);
+                                       $tag, \$beg, $end, $sql);
                } elsif ($uid =~ /\A[0-9]+\z/s) {
                        $uid = $ibx->over->get_art($uid) ? " $uid" : '';
                        "* SEARCH$uid\r\n$tag OK Search done\r\n";
index 286fb7f6b4f9b85062f0e5f39ab0c956000f9228..402cbf7ce07181144f066a252ffb5a16f25bfa17 100644 (file)
@@ -215,4 +215,17 @@ SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
        load_from_row($smsg);
 }
 
+# IMAP search
+sub uid_range {
+       my ($self, $beg, $end, $sql) = @_;
+       my $dbh = $self->connect;
+       my $q = 'SELECT num FROM over WHERE num >= ? AND num <= ?';
+
+       # This is read-only, anyways; but caller should verify it's
+       # only sending \A[0-9]+\z for ds and ts column ranges
+       $q .= $$sql if $sql;
+       $q .= ' ORDER BY num ASC LIMIT ' . DEFAULT_LIMIT;
+       $dbh->selectcol_arrayref($q, undef, $beg, $end);
+}
+
 1;
index 11b56b09dbeb0113d335e5e73e72387a5f8f1eb5..2546ab51ac6cb390da2573b52a1d337321a7c31c 100644 (file)
--- a/t/imapd.t
+++ b/t/imapd.t
@@ -326,6 +326,12 @@ Content-Type: message/rfc822\r
 Content-Disposition: attachment; filename="embed2x\.eml"\r
 \r
 EOF
+
+       my @hits = $mic->search('SENTON' => '18-Apr-2020');
+       is_deeply(\@hits, [ $uidnext ], 'search with date condition works');
+       ok($mic->examine($ng), 'EXAMINE on dummy');
+       @hits = $mic->search('SENTSINCE' => '18-Apr-2020');
+       is_deeply(\@hits, [], 'search on dummy with condition works');
 }); # each_inbox
 
 # message sequence numbers :<