about summary refs log tree commit homepage
path: root/lib/PublicInbox/URIimap.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-04-24 09:28:42 +0000
committerEric Wong <e@80x24.org>2021-04-24 16:10:00 -0400
commit7ed233d847467ef2a963bf3f6dd25b1ee1cb1b1e (patch)
treec6f0e38a0f26350dc40fd77b2cf410fbf7cbbd47 /lib/PublicInbox/URIimap.pm
parent81ed86d5f250adcb407b7278eec37c7ce1975e05 (diff)
downloadpublic-inbox-7ed233d847467ef2a963bf3f6dd25b1ee1cb1b1e.tar.gz
URIimap: support ->uidvalidity and ->iuid
These will be useful for keyword synchronization, and perhaps
importing a single IMAP message with ->iuid.
Diffstat (limited to 'lib/PublicInbox/URIimap.pm')
-rw-r--r--lib/PublicInbox/URIimap.pm38
1 files changed, 34 insertions, 4 deletions
diff --git a/lib/PublicInbox/URIimap.pm b/lib/PublicInbox/URIimap.pm
index db84ee5e..dc193468 100644
--- a/lib/PublicInbox/URIimap.pm
+++ b/lib/PublicInbox/URIimap.pm
@@ -5,8 +5,9 @@
 # This depends only on the documented public API of the `URI' dist,
 # not on internal `_'-prefixed subclasses such as `URI::_server'
 #
-# <https://metacpan.org/pod/URI::imap> exists, but it's not in
-# common distros.
+# <https://metacpan.org/pod/URI::imap> exists, but it appears
+# unmaintained, isn't in common distros, nor does it support
+# ';FOO=BAR' parameters such as UIDVALIDITY
 #
 # RFC 2192 also describes ";TYPE=<list_type>"
 package PublicInbox::URIimap;
@@ -56,7 +57,7 @@ sub path {
         my ($self) = @_;
         my (undef, undef, $path) = uri_split($$self);
         $path =~ s!\A/+!!;
-        $path =~ s/;.*\z//; # ;UIDVALIDITY=nz-number
+        $path =~ s![/;].*\z!!; # [;UIDVALIDITY=nz-number]/;UID=nz-number
         $path eq '' ? undef : $path;
 }
 
@@ -66,7 +67,36 @@ sub mailbox {
         defined($path) ? uri_unescape($path) : undef;
 }
 
-# TODO: UIDVALIDITY, search, and other params
+sub uidvalidity { # read/write
+        my ($self, $val) = @_;
+        my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
+        if (defined $val) {
+                if ($path =~ s!;UIDVALIDITY=[^;/]*\b!;UIDVALIDITY=$val!i or
+                                $path =~ s!/;!;UIDVALIDITY=$val/;!i) {
+                        # s// already changed it
+                } else { # both s// failed, so just append
+                        $path .= ";UIDVALIDITY=$val";
+                }
+                $$self = uri_join($scheme, $auth, $path, $query, $frag);
+        }
+        $path =~ s!\A/+!!;
+        $path =~ m!\A[^;/]+;UIDVALIDITY=([1-9][0-9]*)\b!i ? ($1 + 0) : undef;
+}
+
+sub iuid {
+        my ($self, $val) = @_;
+        my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
+        if (defined $val) {
+                if ($path =~ s!/;UID=[^;/]*\b!/;UID=$val!i) {
+                        # s// already changed it
+                } else { # both s// failed, so just append
+                        $path .= ";UID=$val";
+                }
+                $$self = uri_join($scheme, $auth, $path, $query);
+        }
+        $path =~ m!\A/[^/;]+(?:;UIDVALIDITY=[^;/]+)?/;UID=([1-9][0-9]*)\b!i ?
+                ($1 + 0) : undef;
+}
 
 sub port {
         my ($self) = @_;