]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/URIimap.pm
URIimap: overload "" to ->as_string
[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's not in
9 # common distros.
10 #
11 # RFC 2192 also describes ";TYPE=<list_type>"
12 package PublicInbox::URIimap;
13 use strict;
14 use URI::Split qw(uri_split uri_join); # part of URI
15 use URI::Escape qw(uri_unescape);
16 use overload '""' => \&as_string;
17
18 my %default_ports = (imap => 143, imaps => 993);
19
20 sub new {
21         my ($class, $url) = @_;
22         $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
23 }
24
25 sub canonical {
26         my ($self) = @_;
27
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
31
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) {
37                                 $ret .= ":$port";
38                         }
39                 }
40                 $ret#ei;
41
42         ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
43 }
44
45 sub host {
46         my ($self) = @_;
47         my (undef, $auth) = uri_split($$self);
48         $auth =~ s!\A.*?@!!;
49         $auth =~ s!:[0-9]+\z!!;
50         $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
51         uri_unescape($auth);
52 }
53
54 # unescaped, may be used for globbing
55 sub path {
56         my ($self) = @_;
57         my (undef, undef, $path) = uri_split($$self);
58         $path =~ s!\A/+!!;
59         $path =~ s/;.*\z//; # ;UIDVALIDITY=nz-number
60         $path eq '' ? undef : $path;
61 }
62
63 sub mailbox {
64         my ($self) = @_;
65         my $path = path($self);
66         defined($path) ? uri_unescape($path) : undef;
67 }
68
69 # TODO: UIDVALIDITY, search, and other params
70
71 sub port {
72         my ($self) = @_;
73         my ($scheme, $auth) = uri_split($$self);
74         $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
75 }
76
77 sub authority {
78         my ($self) = @_;
79         my (undef, $auth) = uri_split($$self);
80         $auth
81 }
82
83 sub user {
84         my ($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
89         uri_unescape($auth);
90 }
91
92 sub password {
93         my ($self) = @_;
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
98 }
99
100 sub auth {
101         my ($self) = @_;
102         my (undef, $auth) = uri_split($$self);
103         $auth =~ s/@.*\z//; # drop host:port
104         $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
105 }
106
107 sub scheme {
108         my ($self) = @_;
109         (uri_split($$self))[0];
110 }
111
112 sub as_string { ${$_[0]} }
113
114 1;