about summary refs log tree commit homepage
path: root/lib/PublicInbox/URIimap.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-06-27 10:03:33 +0000
committerEric Wong <e@yhbt.net>2020-06-28 22:27:11 +0000
commit40e21ffef378982a1cebad3c22227228de14c488 (patch)
tree5db6cda674d773b25d58ecef0836f14b14e95a73 /lib/PublicInbox/URIimap.pm
parentf115168f63c05832d69756723fe9197b03303048 (diff)
downloadpublic-inbox-40e21ffef378982a1cebad3c22227228de14c488.tar.gz
We'll be supporting the IMAP URL scheme described in RFC 5092
for -watch, so add this module to fill in what the `URI' package
lacks.
Diffstat (limited to 'lib/PublicInbox/URIimap.pm')
-rw-r--r--lib/PublicInbox/URIimap.pm113
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/PublicInbox/URIimap.pm b/lib/PublicInbox/URIimap.pm
new file mode 100644
index 00000000..56b6002a
--- /dev/null
+++ b/lib/PublicInbox/URIimap.pm
@@ -0,0 +1,113 @@
+# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+# cf. RFC 5092, which the `URI' package doesn't support
+#
+# This depends only on the documented public API of the `URI' dist,
+# not on internal `_'-prefixed subclasses such as `URI::_server'
+#
+# <https://metacpan.org/pod/URI::imap> exists, but it's not in
+# common distros.
+#
+# RFC 2192 also describes ";TYPE=<list_type>"
+package PublicInbox::URIimap;
+use strict;
+use URI::Split qw(uri_split uri_join); # part of URI
+use URI::Escape qw(uri_unescape);
+
+my %default_ports = (imap => 143, imaps => 993);
+
+sub new {
+        my ($class, $url) = @_;
+        $url =~ m!\Aimaps?://! ? bless \$url, $class : undef;
+}
+
+sub canonical {
+        my ($self) = @_;
+
+        # no #frag in RFC 5092 from what I can tell
+        my ($scheme, $auth, $path, $query, $_frag) = uri_split($$self);
+        $path =~ s!\A/+!/!; # excessive leading slash
+
+        # lowercase the host portion
+        $auth =~ s#\A(.*@)?(.*?)(?::([0-9]+))?\z#
+                my $ret = ($1//'').lc($2);
+                if (defined(my $port = $3)) {
+                        if ($default_ports{lc($scheme)} != $port) {
+                                $ret .= ":$port";
+                        }
+                }
+                $ret#ei;
+
+        ref($self)->new(uri_join(lc($scheme), $auth, $path, $query));
+}
+
+sub host {
+        my ($self) = @_;
+        my (undef, $auth) = uri_split($$self);
+        $auth =~ s!\A.*?@!!;
+        $auth =~ s!:[0-9]+\z!!;
+        $auth =~ s!\A\[(.*)\]\z!$1!; # IPv6
+        uri_unescape($auth);
+}
+
+# unescaped, may be used for globbing
+sub path {
+        my ($self) = @_;
+        my (undef, undef, $path) = uri_split($$self);
+        $path =~ s!\A/+!!;
+        $path =~ s/;.*\z//; # ;UIDVALIDITY=nz-number
+        $path eq '' ? undef : $path;
+}
+
+sub mailbox {
+        my ($self) = @_;
+        my $path = path($self);
+        defined($path) ? uri_unescape($path) : undef;
+}
+
+# TODO: UIDVALIDITY, search, and other params
+
+sub port {
+        my ($self) = @_;
+        my ($scheme, $auth) = uri_split($$self);
+        $auth =~ /:([0-9]+)\z/ ? $1 + 0 : $default_ports{lc($scheme)};
+}
+
+sub authority {
+        my ($self) = @_;
+        my (undef, $auth) = uri_split($$self);
+        $auth
+}
+
+sub user {
+        my ($self) = @_;
+        my (undef, $auth) = uri_split($$self);
+        $auth =~ s/@.*\z// or return undef; # drop host:port
+        $auth =~ s/;.*\z//; # drop ;AUTH=...
+        $auth =~ s/:.*\z//; # drop password
+        uri_unescape($auth);
+}
+
+sub password {
+        my ($self) = @_;
+        my (undef, $auth) = uri_split($$self);
+        $auth =~ s/@.*\z// or return undef; # drop host:port
+        $auth =~ s/;.*\z//; # drop ;AUTH=...
+        $auth =~ s/\A[^:]+:// ? uri_unescape($auth) : undef; # drop ->user
+}
+
+sub auth {
+        my ($self) = @_;
+        my (undef, $auth) = uri_split($$self);
+        $auth =~ s/@.*\z//; # drop host:port
+        $auth =~ /;AUTH=(.+)\z/i ? uri_unescape($1) : undef;
+}
+
+sub scheme {
+        my ($self) = @_;
+        (uri_split($$self))[0];
+}
+
+sub as_string { ${$_[0]} }
+
+1;