]> Sergey Matveev's repositories - public-inbox.git/commitdiff
inbox: ->mailboxid accessor
authorEric Wong <e@80x24.org>
Tue, 2 Mar 2021 09:13:07 +0000 (09:13 +0000)
committerEric Wong <e@80x24.org>
Tue, 2 Mar 2021 21:05:36 +0000 (21:05 +0000)
This will be necessary for "mailboxIds" as described in RFCs 8620 and
8621 (for JMAP).  We may implement "MAILBOXID" in RFC 8474 for IMAP,
as well.

lib/PublicInbox/Inbox.pm
t/inbox.t

index a1e34797e77d4eaa654e65ec259e7aefee46f020..da7ea75f1a5741c7702aa0a85466ee908b9f019b 100644 (file)
@@ -421,4 +421,16 @@ sub uidvalidity { $_[0]->{uidvalidity} //= eval { $_[0]->mm->created_at } }
 
 sub eidx_key { $_[0]->{newsgroup} // $_[0]->{inboxdir} }
 
+sub mailboxid { # rfc 8474, 8620, 8621
+       my ($self, $imap_slice) = @_;
+       my $pfx = defined($imap_slice) ? $self->{newsgroup} : $self->{name};
+       utf8::encode($pfx); # to octets
+       # RFC 8620, 1.2 recommends not starting with dash or digits
+       # "A good solution to these issues is to prefix every id with a single
+       #  alphabetical character."
+       'M'.join('', map { sprintf('%02x', ord) } split(//, $pfx)) .
+               (defined($imap_slice) ? sprintf('-%x', $imap_slice) : '') .
+               sprintf('-%x', uidvalidity($self) // 0)
+}
+
 1;
index bc8fae9ae0fc4b32d550bd52d7228c2e2c3097dc..b7239e6d6fe96ba4f1c800f82986259f2d41d983 100644 (file)
--- a/t/inbox.t
+++ b/t/inbox.t
@@ -32,4 +32,11 @@ is(unlink(glob("$x->{inboxdir}/*")), 2, 'unlinked cloneurl & description');
 is_deeply($x->cloneurl, ['https://example.com/inbox'], 'cloneurls memoized');
 is($x->description, "\x{100}blah", 'description memoized');
 
+$x->{name} = "2\x{100}wide";
+$x->{newsgroup} = '2.wide';
+like($x->mailboxid, qr/\AM32c48077696465-[0-9a-f]+\z/,
+       '->mailboxid w/o slice (JMAP)');
+like($x->mailboxid(78), qr/\AM322e77696465-4e-[0-9a-f]+\z/,
+       '->mailboxid w/ slice (IMAP)');
+
 done_testing();