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;
16 use URI::Split qw(uri_split uri_join); # part of URI
17 use URI::Escape qw(uri_unescape uri_escape);
18 use overload '""' => \&as_string;
20 my %default_ports = (imap => 143, imaps => 993);
21 # for enc-auth-type and enc-user in RFC 5092
22 my $achar = qr/[A-Za-z0-9%\-_\.\!\$'\(\)\+\,\&\=\*]+/;
25 my ($class, $url) = @_;
26 $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
32 # no #frag in RFC 5092 from what I can tell
33 my ($scheme, $auth, $path, $query, $_frag) = uri_split($$self);
34 $path =~ s!\A/+!/!; # excessive leading slash
36 # upper-case uidvalidity= and uid= parameter names
37 $path =~ s/;([^=]+)=([^;]*)/;\U$1\E=$2/g;
39 # lowercase the host portion
40 $auth =~ s#\A(.*@)?(.*?)(?::([0-9]+))?\z#
41 my $ret = ($1//'').lc($2);
42 if (defined(my $port = $3)) {
43 if ($default_ports{lc($scheme)} != $port) {
49 ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
54 my (undef, $auth) = uri_split($$self);
56 $auth =~ s!:[0-9]+\z!!;
57 $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
61 # unescaped, may be used for globbing
64 my (undef, undef, $path) = uri_split($$self);
66 $path =~ s![/;].*\z!!; # [;UIDVALIDITY=nz-number]/;UID=nz-number
67 $path eq '' ? undef : $path;
72 my $path = path($self);
73 defined($path) ? uri_unescape($path) : undef;
76 sub uidvalidity { # read/write
77 my ($self, $val) = @_;
78 my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
80 if ($path =~ s!;UIDVALIDITY=[^;/]*\b!;UIDVALIDITY=$val!i or
81 $path =~ s!/;!;UIDVALIDITY=$val/;!i) {
82 # s// already changed it
83 } else { # both s// failed, so just append
84 $path .= ";UIDVALIDITY=$val";
86 $$self = uri_join($scheme, $auth, $path, $query, $frag);
89 $path =~ m!\A[^;/]+;UIDVALIDITY=([1-9][0-9]*)\b!i ? ($1 + 0) : undef;
93 my ($self, $val) = @_;
94 my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
95 if (scalar(@_) == 2) {
97 $path =~ s!/;UID=[^;/]*\b!!i;
99 $path =~ s!/;UID=[^;/]*\b!/;UID=$val!i or
100 $path .= ";UID=$val";
102 $$self = uri_join($scheme, $auth, $path, $query);
104 $path =~ m!\A/[^/;]+(?:;UIDVALIDITY=[^;/]+)?/;UID=([1-9][0-9]*)\b!i ?
110 my ($scheme, $auth) = uri_split($$self);
111 $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
116 my (undef, $auth) = uri_split($$self);
121 my ($self, $val) = @_;
122 my ($scheme, $auth, $path, $query) = uri_split($$self);
124 $auth =~ s/(@.*)\z// and $at_host_port = $1; # stash host:port for now
125 if (scalar(@_) == 2) { # set, this clobbers password, too
127 my $uval = uri_escape($val);
128 if (defined($at_host_port)) {
129 $auth =~ s!\A.*?(;AUTH=$achar).*!$uval$1!ix
132 substr($auth, 0, 0) = "$uval@";
134 } elsif (defined($at_host_port)) { # clobber
135 $auth =~ s!\A.*?(;AUTH=$achar).*!$1!i or $auth = '';
136 if ($at_host_port && $auth eq '') {
137 $at_host_port =~ s/\A\@//;
140 $at_host_port //= '';
141 $$self = uri_join($scheme, $auth.$at_host_port, $path, $query);
144 $at_host_port // return undef; # explicit undef for scalar
145 $auth =~ s/;.*\z//; # drop ;AUTH=...
146 $auth =~ s/:.*\z//; # drop password
147 $auth eq '' ? undef : uri_unescape($auth);
153 my (undef, $auth) = uri_split($$self);
154 $auth =~ s/@.*\z// or return undef; # drop host:port
155 $auth =~ s/;.*\z//; # drop ;AUTH=...
156 $auth =~ s/\A[^:]+:// ? uri_unescape($auth) : undef; # drop ->user
160 my ($self, $val) = @_;
161 my ($scheme, $auth, $path, $query) = uri_split($$self);
163 $auth =~ s/(@.*)\z// and $at_host_port = $1; # stash host:port for now
164 if (scalar(@_) == 2) {
166 my $uval = uri_escape($val);
167 if ($auth =~ s!;AUTH=$achar!;AUTH=$uval!ix) {
169 } elsif (defined($at_host_port)) {
170 $auth .= ";AUTH=$uval";
172 substr($auth, 0, 0) = ";AUTH=$uval@";
175 $auth =~ s!;AUTH=$achar!!i;
176 if ($at_host_port && $auth eq '') {
177 $at_host_port =~ s/\A\@//;
180 $at_host_port //= '';
181 $$self = uri_join($scheme, $auth.$at_host_port, $path, $query);
184 $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
190 (uri_split($$self))[0];
193 sub as_string { ${$_[0]} }
195 sub clone { ref($_[0])->new(as_string($_[0])) }