]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/URIimap.pm
treewide: update to v3 Tor onions
[public-inbox.git] / lib / PublicInbox / URIimap.pm
index ab0908b7197941ecbacc47fdd676b9b20af1b1d3..f6244137000dda3c185737dbc7adc228adbc0a57 100644 (file)
@@ -5,14 +5,16 @@
 # 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;
 use strict;
 use URI::Split qw(uri_split uri_join); # part of URI
 use URI::Escape qw(uri_unescape);
+use overload '""' => \&as_string;
 
 my %default_ports = (imap => 143, imaps => 993);
 
@@ -28,6 +30,9 @@ sub canonical {
        my ($scheme, $auth, $path, $query, $_frag) = uri_split($$self);
        $path =~ s!\A/+!/!; # excessive leading slash
 
+       # upper-case uidvalidity= and uid= parameter names
+       $path =~ s/;([^=]+)=([^;]*)/;\U$1\E=$2/g;
+
        # lowercase the host portion
        $auth =~ s#\A(.*@)?(.*?)(?::([0-9]+))?\z#
                my $ret = ($1//'').lc($2);
@@ -55,7 +60,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;
 }
 
@@ -65,7 +70,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) = @_;
@@ -110,4 +144,6 @@ sub scheme {
 
 sub as_string { ${$_[0]} }
 
+sub clone { ref($_[0])->new(as_string($_[0])) }
+
 1;