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 # lowercase the host portion
34 $auth =~ s#\A(.*@)?(.*?)(?::([0-9]+))?\z#
35 my $ret = ($1//'').lc($2);
36 if (defined(my $port = $3)) {
37 if ($default_ports{lc($scheme)} != $port) {
43 ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
48 my (undef, $auth) = uri_split($$self);
50 $auth =~ s!:[0-9]+\z!!;
51 $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
55 # unescaped, may be used for globbing
58 my (undef, undef, $path) = uri_split($$self);
60 $path =~ s![/;].*\z!!; # [;UIDVALIDITY=nz-number]/;UID=nz-number
61 $path eq '' ? undef : $path;
66 my $path = path($self);
67 defined($path) ? uri_unescape($path) : undef;
70 sub uidvalidity { # read/write
71 my ($self, $val) = @_;
72 my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
74 if ($path =~ s!;UIDVALIDITY=[^;/]*\b!;UIDVALIDITY=$val!i or
75 $path =~ s!/;!;UIDVALIDITY=$val/;!i) {
76 # s// already changed it
77 } else { # both s// failed, so just append
78 $path .= ";UIDVALIDITY=$val";
80 $$self = uri_join($scheme, $auth, $path, $query, $frag);
83 $path =~ m!\A[^;/]+;UIDVALIDITY=([1-9][0-9]*)\b!i ? ($1 + 0) : undef;
87 my ($self, $val) = @_;
88 my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
90 if ($path =~ s!/;UID=[^;/]*\b!/;UID=$val!i) {
91 # s// already changed it
92 } else { # both s// failed, so just append
95 $$self = uri_join($scheme, $auth, $path, $query);
97 $path =~ m!\A/[^/;]+(?:;UIDVALIDITY=[^;/]+)?/;UID=([1-9][0-9]*)\b!i ?
103 my ($scheme, $auth) = uri_split($$self);
104 $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
109 my (undef, $auth) = uri_split($$self);
115 my (undef, $auth) = uri_split($$self);
116 $auth =~ s/@.*\z// or return undef; # drop host:port
117 $auth =~ s/;.*\z//; # drop ;AUTH=...
118 $auth =~ s/:.*\z//; # drop password
124 my (undef, $auth) = uri_split($$self);
125 $auth =~ s/@.*\z// or return undef; # drop host:port
126 $auth =~ s/;.*\z//; # drop ;AUTH=...
127 $auth =~ s/\A[^:]+:// ? uri_unescape($auth) : undef; # drop ->user
132 my (undef, $auth) = uri_split($$self);
133 $auth =~ s/@.*\z//; # drop host:port
134 $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
139 (uri_split($$self))[0];
142 sub as_string { ${$_[0]} }