Class: MailAutoconfig::ClientConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_autoconfig/client_config.rb

Overview

Parse the Autoconfig XML file and create a ruby representation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_config_xml) ⇒ ClientConfig

Returns a new instance of ClientConfig.

Parameters:

  • client_config_xml (String)

    the raw XML to build the ClientConfig from.



60
61
62
# File 'lib/mail_autoconfig/client_config.rb', line 60

def initialize(client_config_xml)
  @config = Nokogiri::XML(client_config_xml)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/mail_autoconfig/client_config.rb', line 6

def config
  @config
end

#email_addressObject

Returns the value of attribute email_address.



7
8
9
# File 'lib/mail_autoconfig/client_config.rb', line 7

def email_address
  @email_address
end

Class Method Details

.from_file(path) ⇒ ClientConfig

Build a configuration from the specified path

Parameters:

  • path (String)

    the loction of the client configuartion file

Returns:



13
14
15
# File 'lib/mail_autoconfig/client_config.rb', line 13

def from_file(path)
  self.new(File.read(path))
end

.search(domain) ⇒ ClientConfig

Try to find a configuration for the given domain, locally or remotely

Parameters:

  • domain (String)

    the domain to lookup the configuration for

Returns:



20
21
22
# File 'lib/mail_autoconfig/client_config.rb', line 20

def search(domain)
  search_local_files(domain) || search_autoconfig_domain(domain)
end

.search_autoconfig_domain(domain) ⇒ ClientConfig

Try a remote server for the configuration for the domain. Search http://autoconfig.#{domain}/mail/config-v1.1.xml first, followed by http://#{domain}/.well-known/autoconfig/mail/config-v1.1.xml

Parameters:

  • domain (String)

    the domain to look for

Returns:

  • (ClientConfig)

    the client configuration for the domain



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mail_autoconfig/client_config.rb', line 41

def search_autoconfig_domain(domain)
  last_config = false
  match = ["http://autoconfig.#{domain}/mail/config-v1.1.xml", "http://#{domain}/.well-known/autoconfig/mail/config-v1.1.xml"].find do |url|
    begin
      response = Faraday.get(url)
      if response.status == 200
        last_config = self.new(response.body)
      else
        false
      end
    rescue
      false
    end
  end
  match ? last_config : false
end

.search_local_files(domain) ⇒ ClientConfig

Search local ISPDB for configurations matching the domain

Parameters:

  • domain (String)

    the domain to look for

Returns:



27
28
29
30
31
32
33
34
# File 'lib/mail_autoconfig/client_config.rb', line 27

def search_local_files(domain)
  last_config = false
  match = Dir.glob(File.join(MailAutoconfig.local_ispdb_path, '*')).find do |config_file|
    last_config = self.from_file(config_file)
    last_config.valid_for_domain?(domain)
  end
  match ? last_config : false
end

Instance Method Details

#domainsArray

A list of domain aliases that this configuration applies to

Returns:

  • (Array)

    list of domains for this configuration



66
67
68
# File 'lib/mail_autoconfig/client_config.rb', line 66

def domains
  @domains ||= provider.xpath('domain').map {|domain| domain.content }
end

#incoming_serversArray

The incoming servers for this configuration. There can be multiple servers per configuration e.g. for IMAP and POP3, ordered in list of preference.

Returns:

  • (Array)

    list of incoming servers



95
96
97
98
99
# File 'lib/mail_autoconfig/client_config.rb', line 95

def incoming_servers
  @incoming_servers ||= provider.xpath('incomingServer').map do |incoming|
    MailAutoconfig::IncomingServer.new(incoming, self)
  end
end

#nameString

Returns the full name of this service (e.g. Google Mail).

Returns:

  • (String)

    the full name of this service (e.g. Google Mail)



83
84
85
# File 'lib/mail_autoconfig/client_config.rb', line 83

def name
  @name ||= provider.xpath('displayName').first.content
end

#outgoing_serversArray

The outgoing servers for this configuration. There can be multiple servers per configuration, ordered in list of preference.

Returns:

  • (Array)

    list of outgoing servers



104
105
106
107
108
# File 'lib/mail_autoconfig/client_config.rb', line 104

def outgoing_servers
  @outgoing_servers ||= provider.xpath('outgoingServer').map do |outgoing|
    MailAutoconfig::OutgoingServer.new(outgoing, self)
  end
end

#provider_idString

Returns The unique string identifying this service (e.g. googlemail.com).

Returns:

  • (String)

    The unique string identifying this service (e.g. googlemail.com)



78
79
80
# File 'lib/mail_autoconfig/client_config.rb', line 78

def provider_id
  @provider_id ||= provider.attr('id').value
end

#short_nameString

Returns the short name of this service (e.g. GMail).

Returns:

  • (String)

    the short name of this service (e.g. GMail)



88
89
90
# File 'lib/mail_autoconfig/client_config.rb', line 88

def short_name
  @short_name ||= provider.xpath('displayShortName').first.content
end

#valid_for_domain?(domain) ⇒ Boolean

Does this configuration file apply to the specified domain?

Parameters:

  • domain (String)

    the domain to check

Returns:

  • (Boolean)

    Does the domain match?



73
74
75
# File 'lib/mail_autoconfig/client_config.rb', line 73

def valid_for_domain?(domain)
  domains.any? {|d| d == domain}
end