AnyEvent-MCP

 view release on metacpan or  search on metacpan

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

         my %scope = $value =~ /\b(imap|extra)\s*:\s*"([^"]*)"/g;
         $scopes{$name} = \%scope;
      } else {
         ($scopes{$name}) = $value =~ /^"([^"]*)"/;
      }
   }

   my ($hostnames) = $source =~ /\bvar\s+kHostnames\s*=\s*new Map\(\[\s*(.*?)^\]\);/ms
      or die "Thunderbird kHostnames layout changed\n";
   my ($issuers) = $source =~ /\bvar\s+kIssuers\s*=\s*new Map\(\[\s*(.*?)^\]\);/ms
      or die "Thunderbird kIssuers layout changed\n";

   my %issuer;
   while ($issuers =~ /\[\s*"([^"]+)"\s*,\s*\{\s*(.*?)^\s*\},?\s*\]/msg) {
      my ($name, $fields) = ($1, $2);
      my %fields;
      while ($fields =~ /\b(clientId|clientSecret|authorizationEndpoint|tokenEndpoint|redirectionEndpoint|usePKCE)\s*:\s*(?:"([^"]*)"|\x60([^\x60]*)\x60|(true|false))/g) {
         $fields{$1} = defined $2 ? $2 : defined $3 ? $3 : $4;
      }
      $issuer{$name} = \%fields;
   }
   %issuer or die "Thunderbird kIssuers entries not recognized\n";

   my %result;
   while ($hostnames =~ /\[\s*"([^"]+)"\s*,\s*\[\s*"([^"]+)"\s*,\s*(?:"([^"]*)"|([A-Z_]+_SCOPES)(?:\.(\w+))?)\s*\]\s*\]/mg) {
      my ($host, $name, $literal, $constant, $field) = ($1, $2, $3, $4, $5);
      my $scope = defined $literal ? $literal : $scopes{$constant};
      $scope = $scope->{$field || "imap"} . (defined $scope->{extra} ? " $scope->{extra}" : "")
         if ref $scope eq "HASH" && (!$field || $field eq "imap");
      next if ref $scope || !defined $scope || !length $scope;
      my $data = $issuer{$name} or next;
      $data->{clientId} && $data->{authorizationEndpoint} && $data->{tokenEndpoint} or next;
      $result{$host} = { %$data, issuer => $name, scope => $scope };
   }

   %result or die "Thunderbird hostname/scope entries not recognized\n";
   \%result
}

sub parse_ispdb {
   my ($xml, $email) = @_;
   require XML::LibXML;
   length $xml <= 1_000_000 or die "Autoconfig XML too large\n";
   my $doc = XML::LibXML->load_xml (
      string => $xml, no_network => 1, load_ext_dtd => 0, expand_entities => 0,
   );
   $doc->documentElement->nodeName eq "clientConfig"
      or die "Not a Thunderbird autoconfig document\n";

   my @servers;
   for my $server ($doc->findnodes ("/clientConfig/emailProvider/incomingServer[\@type='imap']")) {
      my %info = (
         server => $server->findvalue ("./hostname"),
         port => $server->findvalue ("./port"),
         tls => $server->findvalue ("./socketType"),
         user => $server->findvalue ("./username"),
      );
      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;



( run in 1.601 second using v1.01-cache-2.11-cpan-f5f21f1076c )