class Vines::Config

A Config object is passed to the stream handlers to give them access to server configuration information like virtual host names, storage systems, etc. This class provides the DSL methods used in the conf/config.rb file.

Constants

LOG_LEVELS

Attributes

debug[RW]
router[R]

Public Class Methods

configure(&block) click to toggle source
# File lib/vines/config.rb, line 16
def self.configure(&block)
  @@instance = self.new(&block)
end
instance() click to toggle source
# File lib/vines/config.rb, line 20
def self.instance
  @@instance
end
new(&block) click to toggle source
# File lib/vines/config.rb, line 24
def initialize(&block)
  @certs = File.expand_path('conf/certs')
  @vhosts, @ports, @cluster = {}, {}, nil
  @null = Storage::Null.new
  @router = Router.new(self)
  @debug = nil

  instance_eval(&block)
  raise "must define at least one virtual host" if @vhosts.empty?

  unless @certs && File.directory?(@certs) && File.readable?(@certs)
    raise 'Must provide a readable certs directory'
  end
end

Public Instance Methods

[](name) click to toggle source

Retrieve the Port subclass with this name:

:client, :server, :http, :component
# File lib/vines/config.rb, line 151
def [](name)
  @ports[name] or raise ArgumentError.new("no port named #{name}")
end
allowed?(to, from) click to toggle source

Return true if the two JIDs are allowed to send messages to each other. Both domains must have enabled cross_domain_messages in their config files.

# File lib/vines/config.rb, line 157
def allowed?(to, from)
  to, from = JID.new(to), JID.new(from)
  return false                      if to.empty? || from.empty?
  return true                       if to.domain == from.domain # same domain always allowed
  return cross_domain?(to, from)    if local_jid?(to, from)     # both virtual hosted here
  return check_subdomains(to, from) if subdomain?(to, from)     # component/pubsub to component/pubsub
  return check_subdomain(to, from)  if subdomain?(to)           # to component/pubsub
  return check_subdomain(from, to)  if subdomain?(from)         # from component/pubsub
  return cross_domain?(to)          if local_jid?(to)           # from is remote
  return cross_domain?(from)        if local_jid?(from)         # to is remote
  return false
end
certs(dir=nil) click to toggle source
# File lib/vines/config.rb, line 39
def certs(dir=nil)
  dir ? @certs = File.expand_path(dir) : @certs
end
cluster(&block) click to toggle source
# File lib/vines/config.rb, line 62
def cluster(&block)
  return @cluster unless block
  raise "one cluster definition allowed" if @cluster
  @cluster = Cluster.new(self, &block)
end
cluster?() click to toggle source

Return true if the server is a member of a cluster, serving the same domains from different machines.

# File lib/vines/config.rb, line 145
def cluster?
  !!@cluster
end
component?(*jids) click to toggle source

Return true if all JIDs belong to components hosted by this server.

# File lib/vines/config.rb, line 112
def component?(*jids)
  !jids.flatten.index do |jid|
    !component_password(JID.new(jid).domain)
  end
end
component_password(domain) click to toggle source

Return the password for the component or nil if it's not hosted here.

# File lib/vines/config.rb, line 119
def component_password(domain)
  host = @vhosts.values.find {|host| host.component?(domain) }
  host.password(domain) if host
end
host(*names, &block) click to toggle source
# File lib/vines/config.rb, line 43
def host(*names, &block)
  names = names.flatten.map {|name| name.downcase }
  dupes = names.uniq.size != names.size || (@vhosts.keys & names).any?
  raise "one host definition per domain allowed" if dupes
  names.each do |name|
    @vhosts[name] = Host.new(self, name, &block)
  end
end
local_jid?(*jids) click to toggle source

Return true if all of the JIDs are hosted by this server.

# File lib/vines/config.rb, line 125
def local_jid?(*jids)
  !jids.flatten.index do |jid|
    !vhost?(JID.new(jid).domain)
  end
end
log(level) click to toggle source
# File lib/vines/config.rb, line 68
def log(level)
  const = Logger.const_get(level.to_s.upcase) rescue nil
  unless LOG_LEVELS.include?(level.to_s) && const
    raise "log level must be one of: #{LOG_LEVELS.join(', ')}"
  end
  Class.new.extend(Vines::Log).log.level = const
end
ports() click to toggle source
# File lib/vines/config.rb, line 76
def ports
  @ports.values
end
private_storage?(domain) click to toggle source

Return true if private XML fragment storage is enabled for this domain.

# File lib/vines/config.rb, line 132
def private_storage?(domain)
  host = vhost(domain)
  host.private_storage? if host
end
pubsub(domain) click to toggle source

Returns the PubSub system for the domain or nil if pubsub is not enabled for this domain.

# File lib/vines/config.rb, line 100
def pubsub(domain)
  host = @vhosts.values.find {|host| host.pubsub?(domain) }
  host.pubsubs[domain.to_s] if host
end
pubsub?(domain) click to toggle source

Return true if the domain is a pubsub service hosted at a virtual host at this server.

# File lib/vines/config.rb, line 107
def pubsub?(domain)
  @vhosts.values.any? {|host| host.pubsub?(domain) }
end
s2s?(domain) click to toggle source

Returns true if server-to-server connections are allowed with the given domain.

# File lib/vines/config.rb, line 139
def s2s?(domain)
  @ports[:server] && @ports[:server].hosts.include?(domain.to_s)
end
storage(domain) click to toggle source

Returns the storage system for the domain or a Storage::Null instance if the domain is not hosted at this server.

# File lib/vines/config.rb, line 93
def storage(domain)
  host = vhost(domain)
  host ? host.storage : @null
end
vhost(domain) click to toggle source

Return the Host config object for this domain if it's hosted by this server.

# File lib/vines/config.rb, line 87
def vhost(domain)
  @vhosts[domain.to_s]
end
vhost?(domain) click to toggle source

Return true if the domain is virtual hosted by this server.

# File lib/vines/config.rb, line 81
def vhost?(domain)
  !!vhost(domain)
end

Private Instance Methods

check_subdomain(subdomain, jid) click to toggle source

Return true if the third-level subdomain JID (component or pubsub) is allowed to communicate with the other JID. For example, pubsub.wonderland.lit should be allowed to send messages to alice@wonderland.lit because they share the second-level wonderland.lit domain.

# File lib/vines/config.rb, line 195
def check_subdomain(subdomain, jid)
  comp = strip_domain(subdomain)
  return true if comp.domain == jid.domain
  local_jid?(jid) ? cross_domain?(comp, jid) : cross_domain?(comp)
end
check_subdomains(to, from) click to toggle source

Return true if the third-level subdomain JIDs (components and pubsubs) are allowed to communicate with each other. For example, a tea.wonderland.lit component should be allowed to send messages to pubsub.wonderland.lit because they share the second-level wonderland.lit domain.

# File lib/vines/config.rb, line 185
def check_subdomains(to, from)
  sub1, sub2 = strip_domain(to), strip_domain(from)
  (sub1 == sub2) || cross_domain?(sub1, sub2)
end
cross_domain?(*jids) click to toggle source

Return true if all JIDs are allowed to exchange cross domain messages.

# File lib/vines/config.rb, line 210
def cross_domain?(*jids)
  !jids.flatten.index do |jid|
    !vhost(jid.domain).cross_domain_messages?
  end
end
strip_domain(jid) click to toggle source

Return the third-level JID's domain with the first subdomain stripped off to create a second-level domain. For example, alice@tea.wonderland.lit returns wonderland.lit.

# File lib/vines/config.rb, line 204
def strip_domain(jid)
  domain = jid.domain.split('.').drop(1).join('.')
  JID.new(domain)
end
subdomain?(*jids) click to toggle source

Return true if all of the JIDs are some kind of subdomain resource hosted here (either a component or a pubsub domain).

# File lib/vines/config.rb, line 174
def subdomain?(*jids)
  !jids.flatten.index do |jid|
    !component?(jid) && !pubsub?(jid)
  end
end