AnyEvent-MCP

 view release on metacpan or  search on metacpan

lib/AnyEvent/MCP/IMAP/Providers.pm  view on Meta::CPAN

      my @auth = map $_->textContent, $server->findnodes ("./authentication");
      my @methods;
      push @methods, "oauth2" if grep $_ eq "OAuth2", @auth;
      push @methods, "password" if grep $_ eq "password-cleartext", @auth;
      next unless @methods;
      $info{tls} = $info{tls} eq "SSL" ? "connect" : $info{tls} eq "STARTTLS" ? "starttls" : next;
      $info{port} =~ /^\d+$/ && $info{port} >= 1 && $info{port} <= 65535 or next;
      if (defined $email) {
         my ($local, $domain) = split /\@/, $email, 2;
         for my $key (qw(server user)) {
            $info{$key} =~ s/%EMAILADDRESS%/$email/g;
            $info{$key} =~ s/%EMAILLOCALPART%/$local/g;
            $info{$key} =~ s/%EMAILDOMAIN%/$domain/g;
         }
      }
      delete $info{user} if $info{user} =~ /%/;
      next if $info{server} !~ /\A[A-Za-z0-9.-]+\z/;
      $info{methods} = \@methods;
      push @servers, \%info;
   }
   +{
      name => scalar $doc->findvalue ("/clientConfig/emailProvider/displayName"),
      servers => \@servers,
      notes => [map $_->textContent, $doc->findnodes ("/clientConfig/emailProvider/enable/instruction[not(\@lang)]")],
      links => [map $_->getAttribute ("visiturl"), $doc->findnodes ("/clientConfig/emailProvider/enable[\@visiturl]")],
   }
}

sub valid_domain {
   $_[0] =~ /\A(?=.{1,253}\z)[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+\z/
}

sub find_oauth {
   my ($all, @hosts) = @_;
   for my $host (@hosts) {
      next unless defined $host;
      return $all->{$host} if $all->{$host};
      for my $name (keys %$all) {
         return $all->{$name} if $host eq "imap.$name";
      }
   }
   undef
}

sub quote {
   my ($s) = @_;
   return $s if $s =~ /\A[-A-Za-z0-9_@%+=:,\.\/]+\z/;
   $s =~ s/'/'\\''/g;
   "'$s'"
}

sub print_login {
   my ($name, $server, $method, $oauth) = @_;
   my @kv = (method => $method);
   for my $key (qw(server tls port user)) {
      push @kv, $key => $server->{$key} if defined $server->{$key};
   }
   if ($method eq "oauth2") {
      push @kv, "oauth-client-id" => $oauth->{clientId};
      push @kv, "oauth-client-secret" => $oauth->{clientSecret} if defined $oauth->{clientSecret};
      push @kv, "oauth-authorization-endpoint" => $oauth->{authorizationEndpoint};
      push @kv, "oauth-token-endpoint" => $oauth->{tokenEndpoint};
      push @kv, "oauth-scope" => $oauth->{scope};
      push @kv, "oauth-pkce" => $oauth->{usePKCE} && $oauth->{usePKCE} eq "true" ? 1 : 0;
      my $redirect = $oauth->{redirectionEndpoint} || "http://127.0.0.1";
      $redirect .= ":1" if $redirect =~ m{\Ahttp://(?:127\.0\.0\.1|localhost)\z};
      push @kv, "oauth-redirect-uri" => $redirect;
   }
   print "mcp-imap --name ", quote ($name), " login", @kv ? " \\\n" : "\n";
   while (@kv) {
      my ($key, $value) = splice @kv, 0, 2;
      print "  $key ", quote ($value), @kv ? " \\\n" : "\n";
   }
}

sub run {
   my ($name, $provider_file, $ispdb_file, @args) = @_;
   @args <= 1 or die "Usage: mcp-imap [OPTION]... providers [SEARCH]\n";
   my $search = $args[0];
   my $email;
   if (defined $search && $search =~ /\A([^\s\@]+)\@([A-Za-z0-9.-]+)\z/) {
      $email = $search;
      $search = $2;
   }

   my $source = defined $provider_file ? read_file ($provider_file) : download ($oauth_source);
   my $oauth = defined $source ? parse_oauth ($source) : {};
   my $index = defined $provider_file || defined $ispdb_file ? [] :
      parse_index (download ($ispdb . "generated_files.json") || "[]");
   my %known = map +($_ => 1), (@$index, keys %$oauth);
   delete @known{grep /^(?:smtp|pop|pop3|carddav|carddav-personal|caldav|graph|www\.googleapis|apidata)\.|\.test\z/, keys %known};
   my @domains = sort grep !defined $search
      || index (lc $_, lc $search) >= 0
      || ($oauth->{$_} && index (lc $oauth->{$_}{issuer}, lc $search) >= 0), keys %known;

   my @exact = defined $search ? grep lc $_ eq lc $search, @domains : ();
   @domains = @exact if @exact;
   if (!defined $search || @domains > 1 || !@domains && !valid_domain ($search)) {
      @domains or die "No matching providers\n";
      print "Thunderbird mail providers (", scalar @domains, " matches):\n";
      printf "  %-32s %s\n", $_, $oauth->{$_} ? "OAuth: $oauth->{$_}{issuer}" : "ISPDB"
         for @domains;
      print "\nSpecify a domain or email address to generate login commands.\n";
      return;
   }
   my $domain = @domains == 1 ? $domains[0] : $search;
   valid_domain ($domain) or die "Invalid provider domain\n";

   my ($xml, $origin);
   if (defined $ispdb_file) {
      $xml = read_file ($ispdb_file);
      $origin = "local XML ($ispdb_file)";
   } else {
      for my $url (
         "https://autoconfig.$domain/mail/config-v1.1.xml",
         "https://$domain/.well-known/autoconfig/mail/config-v1.1.xml",
         $ispdb . "v1.1/$domain",
      ) {
         $xml = download ($url);
         if (defined $xml) {
            $origin = $url;
            last;



( run in 1.909 second using v1.01-cache-2.11-cpan-1bcd28dbaf1 )