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.



56
57
58
# File 'lib/mail_autoconfig/client_config.rb', line 56

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/mail_autoconfig/client_config.rb', line 4

def config
  @config
end

#email_addressObject

Returns the value of attribute email_address.



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

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:



11
12
13
# File 'lib/mail_autoconfig/client_config.rb', line 11

def from_file(path)
  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:



18
19
20
# File 'lib/mail_autoconfig/client_config.rb', line 18

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mail_autoconfig/client_config.rb', line 39

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|
    response = Faraday.get(url)
    if response.status == 200
      last_config = new(response.body)
    else
      false
    end
  rescue
    false
  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:



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

def search_local_files(domain)
  last_config = false
  match = Dir.glob(File.join(MailAutoconfig.local_ispdb_path, "*")).find do |config_file|
    last_config = 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



62
63
64
# File 'lib/mail_autoconfig/client_config.rb', line 62

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



91
92
93
94
95
# File 'lib/mail_autoconfig/client_config.rb', line 91

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)



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

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



100
101
102
103
104
# File 'lib/mail_autoconfig/client_config.rb', line 100

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)



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

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)



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

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?



69
70
71
# File 'lib/mail_autoconfig/client_config.rb', line 69

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