about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/PublicInbox/Inbox.pm12
-rw-r--r--t/inbox.t7
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index a1e34797..da7ea75f 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -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;
diff --git a/t/inbox.t b/t/inbox.t
index bc8fae9a..b7239e6d 100644
--- 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();