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'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);
16 use overload '""' => \&as_string;
18 my %default_ports = (imap => 143, imaps => 993);
21 my ($class, $url) = @_;
22 $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
28 # no #frag in RFC 5092 from what I can tell
29 my ($scheme, $auth, $path, $query, $_frag) = uri_split($$self);
30 $path =~ s!\A/+!/!; # excessive leading slash
32 # lowercase the host portion
33 $auth =~ s#\A(.*@)?(.*?)(?::([0-9]+))?\z#
34 my $ret = ($1//'').lc($2);
35 if (defined(my $port = $3)) {
36 if ($default_ports{lc($scheme)} != $port) {
42 ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
47 my (undef, $auth) = uri_split($$self);
49 $auth =~ s!:[0-9]+\z!!;
50 $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
54 # unescaped, may be used for globbing
57 my (undef, undef, $path) = uri_split($$self);
59 $path =~ s/;.*\z//; # ;UIDVALIDITY=nz-number
60 $path eq '' ? undef : $path;
65 my $path = path($self);
66 defined($path) ? uri_unescape($path) : undef;
69 # TODO: UIDVALIDITY, search, and other params
73 my ($scheme, $auth) = uri_split($$self);
74 $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
79 my (undef, $auth) = uri_split($$self);
85 my (undef, $auth) = uri_split($$self);
86 $auth =~ s/@.*\z// or return undef; # drop host:port
87 $auth =~ s/;.*\z//; # drop ;AUTH=...
88 $auth =~ s/:.*\z//; # drop password
94 my (undef, $auth) = uri_split($$self);
95 $auth =~ s/@.*\z// or return undef; # drop host:port
96 $auth =~ s/;.*\z//; # drop ;AUTH=...
97 $auth =~ s/\A[^:]+:// ? uri_unescape($auth) : undef; # drop ->user
102 my (undef, $auth) = uri_split($$self);
103 $auth =~ s/@.*\z//; # drop host:port
104 $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
109 (uri_split($$self))[0];
112 sub as_string { ${$_[0]} }