]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/URIimap.pm
dc19346815556095f8ae056033f819d20b318905
[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 URI::Split qw(uri_split uri_join); # part of URI
16 use URI::Escape qw(uri_unescape);
17 use overload '""' => \&as_string;
18
19 my %default_ports = (imap => 143, imaps => 993);
20
21 sub new {
22         my ($class, $url) = @_;
23         $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
24 }
25
26 sub canonical {
27         my ($self) = @_;
28
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
32
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) {
38                                 $ret .= ":$port";
39                         }
40                 }
41                 $ret#ei;
42
43         ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
44 }
45
46 sub host {
47         my ($self) = @_;
48         my (undef, $auth) = uri_split($$self);
49         $auth =~ s!\A.*?@!!;
50         $auth =~ s!:[0-9]+\z!!;
51         $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
52         uri_unescape($auth);
53 }
54
55 # unescaped, may be used for globbing
56 sub path {
57         my ($self) = @_;
58         my (undef, undef, $path) = uri_split($$self);
59         $path =~ s!\A/+!!;
60         $path =~ s![/;].*\z!!; # [;UIDVALIDITY=nz-number]/;UID=nz-number
61         $path eq '' ? undef : $path;
62 }
63
64 sub mailbox {
65         my ($self) = @_;
66         my $path = path($self);
67         defined($path) ? uri_unescape($path) : undef;
68 }
69
70 sub uidvalidity { # read/write
71         my ($self, $val) = @_;
72         my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
73         if (defined $val) {
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";
79                 }
80                 $$self = uri_join($scheme, $auth, $path, $query, $frag);
81         }
82         $path =~ s!\A/+!!;
83         $path =~ m!\A[^;/]+;UIDVALIDITY=([1-9][0-9]*)\b!i ? ($1 + 0) : undef;
84 }
85
86 sub iuid {
87         my ($self, $val) = @_;
88         my ($scheme, $auth, $path, $query, $frag) = uri_split($$self);
89         if (defined $val) {
90                 if ($path =~ s!/;UID=[^;/]*\b!/;UID=$val!i) {
91                         # s// already changed it
92                 } else { # both s// failed, so just append
93                         $path .= ";UID=$val";
94                 }
95                 $$self = uri_join($scheme, $auth, $path, $query);
96         }
97         $path =~ m!\A/[^/;]+(?:;UIDVALIDITY=[^;/]+)?/;UID=([1-9][0-9]*)\b!i ?
98                 ($1 + 0) : undef;
99 }
100
101 sub port {
102         my ($self) = @_;
103         my ($scheme, $auth) = uri_split($$self);
104         $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
105 }
106
107 sub authority {
108         my ($self) = @_;
109         my (undef, $auth) = uri_split($$self);
110         $auth
111 }
112
113 sub user {
114         my ($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
119         uri_unescape($auth);
120 }
121
122 sub password {
123         my ($self) = @_;
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
128 }
129
130 sub auth {
131         my ($self) = @_;
132         my (undef, $auth) = uri_split($$self);
133         $auth =~ s/@.*\z//; # drop host:port
134         $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
135 }
136
137 sub scheme {
138         my ($self) = @_;
139         (uri_split($$self))[0];
140 }
141
142 sub as_string { ${$_[0]} }
143
144 1;