user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] inbox: ->mailboxid accessor
Date: Tue,  2 Mar 2021 09:13:07 +0000	[thread overview]
Message-ID: <20210302091307.19331-1-e@80x24.org> (raw)

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 | 12 ++++++++++++
 t/inbox.t                |  7 +++++++
 2 files changed, 19 insertions(+)

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();

                 reply	other threads:[~2021-03-02  9:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210302091307.19331-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).