about summary refs log tree commit homepage
path: root/lib/PublicInbox/LeiMailSync.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-05-23 21:36:50 +0000
committerEric Wong <e@80x24.org>2021-05-24 22:31:36 +0000
commita9bfba8eb31dbb4cdb8306496a911a84f8ac6e04 (patch)
treec897e38b6de8c5e2673401bc66a2bd8088b895fe /lib/PublicInbox/LeiMailSync.pm
parent2d4ac1c886908bb1d79592913a2a2037a1a024e7 (diff)
downloadpublic-inbox-a9bfba8eb31dbb4cdb8306496a911a84f8ac6e04.tar.gz
lei inspect: use LeiMailSync->match_imap_url
Move match_imap_url into LeiMailSync so it can be used in more
places, such as "lei inspect".  Upcoming commands such as
"lei forget-mail-sync" and {add,forget,pause,resume}-watch will
also support relaxed IMAP matching rules since there's
no reasonable way to expect users use ";UIDVALIDITY=" on the
command-line.
Diffstat (limited to 'lib/PublicInbox/LeiMailSync.pm')
-rw-r--r--lib/PublicInbox/LeiMailSync.pm32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm
index 32e17c65..b2986686 100644
--- a/lib/PublicInbox/LeiMailSync.pm
+++ b/lib/PublicInbox/LeiMailSync.pm
@@ -265,4 +265,36 @@ WHERE b.oidbin = ?
         undef;
 }
 
+sub match_imap_url {
+        my ($self, $url, $all) = @_; # $all = [ $lms->folders ];
+        $all //= [ $self->folders ];
+        require PublicInbox::URIimap;
+        my $want = PublicInbox::URIimap->new($url)->canonical;
+        my ($s, $h, $mb) = ($want->scheme, $want->host, $want->mailbox);
+        my @uri = map { PublicInbox::URIimap->new($_)->canonical }
+                grep(m!\A\Q$s\E://.*?\Q$h\E\b.*?/\Q$mb\E\b!, @$all);
+        my @match;
+        for my $x (@uri) {
+                next if $x->mailbox ne $want->mailbox;
+                next if $x->host ne $want->host;
+                next if $x->port != $want->port;
+                my $x_uidval = $x->uidvalidity;
+                next if ($want->uidvalidity // $x_uidval) != $x_uidval;
+
+                # allow nothing in want to possibly match ";AUTH=ANONYMOUS"
+                if (defined($x->auth) && !defined($want->auth) &&
+                                !defined($want->user)) {
+                        push @match, $x;
+                # or maybe user was forgotten on CLI:
+                } elsif (defined($x->user) && !defined($want->user)) {
+                        push @match, $x;
+                } elsif (($x->user//"\0") eq ($want->user//"\0")) {
+                        push @match, $x;
+                }
+        }
+        return @match if wantarray;
+        scalar(@match) <= 1 ? $match[0] :
+                        "E: `$url' is ambiguous:\n\t".join("\n\t", @match)."\n";
+}
+
 1;