# slow storage.
#
# data notes:
-# * NNTP article numbers are UIDs
+#
+# * NNTP article numbers are UIDs, mm->created_at is UIDVALIDITY
+#
+# * public-inboxes are sliced into mailboxes of 50K messages
+# to not overload MUAs: $NEWSGROUP_NAME.$SLICE_INDEX
+# Slices are similar in concept to v2 "epochs". Epochs
+# are for the limitations of git clients, while slices are
+# for the limitations of IMAP clients.
+#
+# * sequence numbers are estimated based on slice
package PublicInbox::IMAP;
use strict;
sub LINE_MAX () { 8000 } # RFC 2683 3.2.1.5
# changing this will cause grief for clients which cache
-sub UID_BLOCK () { 50_000 }
+sub UID_SLICE () { 50_000 }
# these values area also used for sorting
sub NEED_SMSG () { 1 }
my ($self, $ibx) = @_;
my $new = $ibx->over->max;
my $uid_base = $self->{uid_base} // 0;
- my $uid_end = $uid_base + UID_BLOCK;
+ my $uid_end = $uid_base + UID_SLICE;
defined(my $old = $self->{-idle_max}) or die 'BUG: -idle_max unset';
$new = $uid_end if $new > $uid_end;
if ($new > $old) {
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_BLOCK;
+ my $uid_end = $self->{uid_base} + UID_SLICE;
my $sock = $self->{sock} or return;
my $fd = fileno($sock);
# only do inotify on most recent slice
"$idle_tag OK Idle done\r\n";
}
-sub ensure_ranges_exist ($$$) {
+sub ensure_slices_exist ($$$) {
my ($imapd, $ibx, $max) = @_;
defined(my $mb_top = $ibx->{newsgroup}) or return;
my $mailboxes = $imapd->{mailboxes};
my @created;
- for (my $i = int($max/UID_BLOCK); $i >= 0; --$i) {
+ for (my $i = int($max/UID_SLICE); $i >= 0; --$i) {
my $sub_mailbox = "$mb_top.$i";
last if exists $mailboxes->{$sub_mailbox};
$mailboxes->{$sub_mailbox} = $ibx;
my ($self, $mailbox) = @_;
my ($ibx, $exists, $uidnext, $uid_base);
if ($mailbox =~ /\A(.+)\.([0-9]+)\z/) {
- # old mail: inbox.comp.foo.$uid_block_idx
+ # old mail: inbox.comp.foo.$SLICE_IDX
my $mb_top = $1;
- $uid_base = $2 * UID_BLOCK;
+ $uid_base = $2 * UID_SLICE;
$ibx = $self->{imapd}->{mailboxes}->{lc $mailbox} or return;
my $max;
($exists, $uidnext, $max) = $ibx->over->imap_status($uid_base,
- $uid_base + UID_BLOCK);
- ensure_ranges_exist($self->{imapd}, $ibx, $max);
+ $uid_base + UID_SLICE);
+ ensure_slices_exist($self->{imapd}, $ibx, $max);
} else { # check for dummy inboxes
$mailbox = lc $mailbox;
$ibx = $self->{imapd}->{mailboxes}->{$mailbox} or return;
# 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_ranges_exist($self->{imapd}, $z, $z->over->max);
+ ensure_slices_exist($self->{imapd}, $z, $z->over->max);
}
$uid_base = $exists = 0;
sub uid_clamp ($$$) {
my ($self, $beg, $end) = @_;
my $uid_min = $self->{uid_base} + 1;
- my $uid_end = $uid_min + UID_BLOCK - 1;
+ my $uid_end = $uid_min + UID_SLICE - 1;
$$beg = $uid_min if $$beg < $uid_min;
$$end = $uid_end if $$end > $uid_end;
}
$$range_csv = undef;
}
my $uid_base = $self->{uid_base};
- my $uid_end = $uid_base + UID_BLOCK;
+ my $uid_end = $uid_base + UID_SLICE;
if ($range =~ /\A([0-9]+):([0-9]+)\z/) {
($beg, $end) = ($1 + 0, $2 + 0);
uid_clamp($self, \$beg, \$end);
timegm(0, 0, 0, $dd, $mm, $yyyy);
}
-sub msn_convert ($$) {
- my ($self, $uids) = @_;
- my $adj = $self->{uid_base};
- $_ -= $adj for @$uids;
-}
-
sub search_uid_range { # long_response
- my ($self, $tag, $sql, $range_info, $want_msn) = @_;
+ my ($self, $tag, $sql, $range_info) = @_;
my $uids = [];
if (defined(my $err = refill_uids($self, $uids, $range_info, $sql))) {
$err ||= 'OK Search done';
$self->write("\r\n$tag $err\r\n");
return;
}
- msn_convert($self, $uids) if $want_msn;
$self->msg_more(join(' ', '', @$uids));
1; # more
}
}
sub search_xap_range { # long_response
- my ($self, $tag, $q, $range_info, $want_msn) = @_;
+ my ($self, $tag, $q, $range_info) = @_;
my $uids = [];
if (defined(my $err = refill_xap($self, $uids, $range_info, $q))) {
$err ||= 'OK Search done';
$self->write("\r\n$tag $err\r\n");
return;
}
- msn_convert($self, $uids) if $want_msn;
$self->msg_more(join(' ', '', @$uids));
1; # more
}
-sub search_common {
- my ($self, $tag, $rest, $want_msn) = @_;
+sub cmd_uid_search ($$$;) {
+ my ($self, $tag) = splice(@_, 0, 2);
my $ibx = $self->{ibx} or return "$tag BAD No mailbox selected\r\n";
- my $q = parse_query($self, $rest);
+ my $q = parse_query($self, \@_);
return "$tag $q\r\n" if !ref($q);
my ($sql, $range_info) = delete @$q{qw(sql range_info)};
if (!scalar(keys %$q)) { # overview.sqlite3
$self->msg_more('* SEARCH');
long_response($self, \&search_uid_range,
- $tag, $sql, $range_info, $want_msn);
+ $tag, $sql, $range_info);
} elsif ($q = $q->{xap}) {
$self->msg_more('* SEARCH');
long_response($self, \&search_xap_range,
- $tag, $q, $range_info, $want_msn);
+ $tag, $q, $range_info);
} else {
"$tag BAD Error\r\n";
}
}
-sub cmd_uid_search ($$$;) {
- my ($self, $tag) = splice(@_, 0, 2);
- search_common($self, $tag, \@_);
-}
-
-sub cmd_search ($$$;) {
- my ($self, $tag) = splice(@_, 0, 2);
- search_common($self, $tag, \@_, 1);
-}
+# note: MSN SEARCH is NOT supported. Do any widely-used MUAs
+# rely on MSNs from SEARCH results? Let us know at meta@public-inbox.org
sub args_ok ($$) { # duplicated from PublicInbox::NNTP
my ($cb, $argc) = @_;
# returns 1 if we can continue, 0 if not due to buffered writes or disconnect
sub process_line ($$) {
my ($self, $l) = @_;
+
+ # TODO: IMAP allows literals for big requests to upload messages
+ # (which we don't support) but maybe some big search queries use it.
my ($tag, $req, @args) = parse_line('[ \t]+', 0, $l);
pop(@args) if (@args && !defined($args[-1]));
if (@args && uc($req) eq 'UID') {