1 # Copyright (C) 2020 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's not in
11 # RFC 2192 also describes ";TYPE=<list_type>"
12 package PublicInbox::URIimap;
14 use URI::Split qw(uri_split uri_join); # part of URI
15 use URI::Escape qw(uri_unescape);
17 my %default_ports = (imap => 143, imaps => 993);
20 my ($class, $url) = @_;
21 $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
27 # no #frag in RFC 5092 from what I can tell
28 my ($scheme, $auth, $path, $query, $_frag) = uri_split($$self);
29 $path =~ s!\A/+!/!; # excessive leading slash
31 # lowercase the host portion
32 $auth =~ s#\A(.*@)?(.*?)(?::([0-9]+))?\z#
33 my $ret = ($1//'').lc($2);
34 if (defined(my $port = $3)) {
35 if ($default_ports{lc($scheme)} != $port) {
41 ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
46 my (undef, $auth) = uri_split($$self);
48 $auth =~ s!:[0-9]+\z!!;
49 $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
53 # unescaped, may be used for globbing
56 my (undef, undef, $path) = uri_split($$self);
58 $path =~ s/;.*\z//; # ;UIDVALIDITY=nz-number
59 $path eq '' ? undef : $path;
64 my $path = path($self);
65 defined($path) ? uri_unescape($path) : undef;
68 # TODO: UIDVALIDITY, search, and other params
72 my ($scheme, $auth) = uri_split($$self);
73 $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
78 my (undef, $auth) = uri_split($$self);
84 my (undef, $auth) = uri_split($$self);
85 $auth =~ s/@.*\z// or return undef; # drop host:port
86 $auth =~ s/;.*\z//; # drop ;AUTH=...
87 $auth =~ s/:.*\z//; # drop password
93 my (undef, $auth) = uri_split($$self);
94 $auth =~ s/@.*\z// or return undef; # drop host:port
95 $auth =~ s/;.*\z//; # drop ;AUTH=...
96 $auth =~ s/\A[^:]+:// ? uri_unescape($auth) : undef; # drop ->user
101 my (undef, $auth) = uri_split($$self);
102 $auth =~ s/@.*\z//; # drop host:port
103 $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
108 (uri_split($$self))[0];
111 sub as_string { ${$_[0]} }