1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # cf. RFC 5092, which the `URI' package doesn't support
5 # This depends only on the documented public API of the `URI' dist,
6 # not on internal `_'-prefixed subclasses such as `URI::_server'
8 # <https://metacpan.org/pod/URI::imap> exists, but it appears
9 # unmaintained, isn't in common distros, nor does it support
10 # ';FOO=BAR' parameters such as UIDVALIDITY
12 # RFC 2192 also describes ";TYPE=<list_type>"
13 package PublicInbox::URIimap;
15 use URI::Split qw(uri_split uri_join); # part of URI
16 use URI::Escape qw(uri_unescape);
17 use overload '""' => \&as_string;
19 my %default_ports = (imap => 143, imaps => 993);
22 my ($class, $url) = @_;
23 $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
29 # no #frag in RFC 5092 from what I can tell
30 my ($scheme, $auth, $path, $query, $_frag) = uri_split($$self);
31 $path =~ s!\A/+!/!; # excessive leading slash
33 # upper-case uidvalidity= and uid= parameter names
34 $path =~ s/;([^=]+)=([^;]*)/;\U$1\E=$2/g;
36 # lowercase the host portion
37 $auth =~ s#\A(.*@)?(.*?)(?::([0-9]+))?\z#
38 my $ret = ($1//'').lc($2);
39 if (defined(my $port = $3)) {
40 if ($default_ports{lc($scheme)} != $port) {
46 ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
51 my (undef, $auth) = uri_split($$self);
53 $auth =~ s!:[0-9]+\z!!;
54 $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
58 # unescaped, may be used for globbing
61 my (undef, undef, $path) = uri_split($$self);
63 $path =~ s![/;].*\z!!; # [;UIDVALIDITY=nz-number]/;UID=nz-number
64 $path eq '' ? undef : $path;
69 my $path = path($self);
70 defined($path) ? uri_unescape($path) : undef;
73 sub uidvalidity { # read/write
74 my ($self, $val) = @_;
75 my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
77 if ($path =~ s!;UIDVALIDITY=[^;/]*\b!;UIDVALIDITY=$val!i or
78 $path =~ s!/;!;UIDVALIDITY=$val/;!i) {
79 # s// already changed it
80 } else { # both s// failed, so just append
81 $path .= ";UIDVALIDITY=$val";
83 $$self = uri_join($scheme, $auth, $path, $query, $frag);
86 $path =~ m!\A[^;/]+;UIDVALIDITY=([1-9][0-9]*)\b!i ? ($1 + 0) : undef;
90 my ($self, $val) = @_;
91 my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
93 if ($path =~ s!/;UID=[^;/]*\b!/;UID=$val!i) {
94 # s// already changed it
95 } else { # both s// failed, so just append
98 $$self = uri_join($scheme, $auth, $path, $query);
100 $path =~ m!\A/[^/;]+(?:;UIDVALIDITY=[^;/]+)?/;UID=([1-9][0-9]*)\b!i ?
106 my ($scheme, $auth) = uri_split($$self);
107 $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
112 my (undef, $auth) = uri_split($$self);
118 my (undef, $auth) = uri_split($$self);
119 $auth =~ s/@.*\z// or return undef; # drop host:port
120 $auth =~ s/;.*\z//; # drop ;AUTH=...
121 $auth =~ s/:.*\z//; # drop password
127 my (undef, $auth) = uri_split($$self);
128 $auth =~ s/@.*\z// or return undef; # drop host:port
129 $auth =~ s/;.*\z//; # drop ;AUTH=...
130 $auth =~ s/\A[^:]+:// ? uri_unescape($auth) : undef; # drop ->user
135 my (undef, $auth) = uri_split($$self);
136 $auth =~ s/@.*\z//; # drop host:port
137 $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
142 (uri_split($$self))[0];
145 sub as_string { ${$_[0]} }
147 sub clone { ref($_[0])->new(as_string($_[0])) }