]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/URIimap.pm
a309fde062846cd9c89ccb17bde181645577dd89
[public-inbox.git] / lib / PublicInbox / URIimap.pm
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
4 #
5 # This depends only on the documented public API of the `URI' dist,
6 # not on internal `_'-prefixed subclasses such as `URI::_server'
7 #
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
11 #
12 # RFC 2192 also describes ";TYPE=<list_type>"
13 package PublicInbox::URIimap;
14 use strict;
15 use v5.10.1;
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;
19
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%\-_\.\!\$'\(\)\+\,\&\=\*]+/;
23
24 sub new {
25         my ($class, $url) = @_;
26         $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
27 }
28
29 sub canonical {
30         my ($self) = @_;
31
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
35
36         # upper-case uidvalidity= and uid= parameter names
37         $path =~ s/;([^=]+)=([^;]*)/;\U$1\E=$2/g;
38
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) {
44                                 $ret .= ":$port";
45                         }
46                 }
47                 $ret#ei;
48
49         ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
50 }
51
52 sub host {
53         my ($self) = @_;
54         my (undef, $auth) = uri_split($$self);
55         $auth =~ s!\A.*?@!!;
56         $auth =~ s!:[0-9]+\z!!;
57         $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
58         uri_unescape($auth);
59 }
60
61 # unescaped, may be used for globbing
62 sub path {
63         my ($self) = @_;
64         my (undef, undef, $path) = uri_split($$self);
65         $path =~ s!\A/+!!;
66         $path =~ s![/;].*\z!!; # [;UIDVALIDITY=nz-number]/;UID=nz-number
67         $path eq '' ? undef : $path;
68 }
69
70 sub mailbox {
71         my ($self) = @_;
72         my $path = path($self);
73         defined($path) ? uri_unescape($path) : undef;
74 }
75
76 sub uidvalidity { # read/write
77         my ($self, $val) = @_;
78         my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
79         if (defined $val) {
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";
85                 }
86                 $$self = uri_join($scheme, $auth, $path, $query, $frag);
87         }
88         $path =~ s!\A/+!!;
89         $path =~ m!\A[^;/]+;UIDVALIDITY=([1-9][0-9]*)\b!i ? ($1 + 0) : undef;
90 }
91
92 sub uid {
93         my ($self, $val) = @_;
94         my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
95         if (scalar(@_) == 2) {
96                 if (!defined $val) {
97                         $path =~ s!/;UID=[^;/]*\b!!i;
98                 } else {
99                         $path =~ s!/;UID=[^;/]*\b!/;UID=$val!i or
100                                 $path .= ";UID=$val";
101                 }
102                 $$self = uri_join($scheme, $auth, $path, $query);
103         }
104         $path =~ m!\A/[^/;]+(?:;UIDVALIDITY=[^;/]+)?/;UID=([1-9][0-9]*)\b!i ?
105                 ($1 + 0) : undef;
106 }
107
108 sub port {
109         my ($self) = @_;
110         my ($scheme, $auth) = uri_split($$self);
111         $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
112 }
113
114 sub authority {
115         my ($self) = @_;
116         my (undef, $auth) = uri_split($$self);
117         $auth
118 }
119
120 sub user {
121         my ($self, $val) = @_;
122         my ($scheme, $auth, $path, $query) = uri_split($$self);
123         my $at_host_port;
124         $auth =~ s/(@.*)\z// and $at_host_port = $1; # stash host:port for now
125         if (scalar(@_) == 2) { # set, this clobbers password, too
126                 if (defined $val) {
127                         my $uval = uri_escape($val);
128                         if (defined($at_host_port)) {
129                                 $auth =~ s!\A.*?(;AUTH=$achar).*!$uval$1!ix
130                                         or $auth = $uval;
131                         } else {
132                                 substr($auth, 0, 0) = "$uval@";
133                         }
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\@//;
138                         }
139                 }
140                 $at_host_port //= '';
141                 $$self = uri_join($scheme, $auth.$at_host_port, $path, $query);
142                 $val;
143         } else { # read-only
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);
148         }
149 }
150
151 sub password {
152         my ($self) = @_;
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
157 }
158
159 sub auth {
160         my ($self, $val) = @_;
161         my ($scheme, $auth, $path, $query) = uri_split($$self);
162         my $at_host_port;
163         $auth =~ s/(@.*)\z// and $at_host_port = $1; # stash host:port for now
164         if (scalar(@_) == 2) {
165                 if (defined $val) {
166                         my $uval = uri_escape($val);
167                         if ($auth =~ s!;AUTH=$achar!;AUTH=$uval!ix) {
168                                 # replaced existing
169                         } elsif (defined($at_host_port)) {
170                                 $auth .= ";AUTH=$uval";
171                         } else {
172                                 substr($auth, 0, 0) = ";AUTH=$uval@";
173                         }
174                 } else { # clobber
175                         $auth =~ s!;AUTH=$achar!!i;
176                         if ($at_host_port && $auth eq '') {
177                                 $at_host_port =~ s/\A\@//;
178                         }
179                 }
180                 $at_host_port //= '';
181                 $$self = uri_join($scheme, $auth.$at_host_port, $path, $query);
182                 $val;
183         } else { # read-only
184                 $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
185         }
186 }
187
188 sub scheme {
189         my ($self) = @_;
190         (uri_split($$self))[0];
191 }
192
193 sub as_string { ${$_[0]} }
194
195 sub clone { ref($_[0])->new(as_string($_[0])) }
196
197 1;