Class: Vines::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vines/config.rb,
lib/vines/config/host.rb,
lib/vines/config/port.rb,
lib/vines/config/pubsub.rb

Overview

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.

Defined Under Namespace

Classes: ClientPort, ComponentPort, Host, HttpPort, Port, PubSub, ServerPort

Constant Summary collapse

LOG_LEVELS =
%w[debug info warn error fatal].freeze
@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Config

Returns a new instance of Config.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vines/config.rb', line 23

def initialize(&block)
  @certs = File.expand_path('conf/certs')
  @vhosts, @ports, @cluster = {}, {}, nil
  @null = Storage::Null.new
  @router = Router.new(self)
  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

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.



12
13
14
# File 'lib/vines/config.rb', line 12

def router
  @router
end

Class Method Details

.configure(&block) ⇒ Object



15
16
17
# File 'lib/vines/config.rb', line 15

def self.configure(&block)
  @@instance = self.new(&block)
end

.instanceObject



19
20
21
# File 'lib/vines/config.rb', line 19

def self.instance
  @@instance
end

Instance Method Details

#[](name) ⇒ Object

Retrieve the Port subclass with this name:

:client, :server, :http, :component


148
149
150
# File 'lib/vines/config.rb', line 148

def [](name)
  @ports[name] or raise ArgumentError.new("no port named #{name}")
end

#allowed?(to, from) ⇒ Boolean

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.

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/vines/config.rb', line 154

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) ⇒ Object



36
37
38
# File 'lib/vines/config.rb', line 36

def certs(dir=nil)
  dir ? @certs = File.expand_path(dir) : @certs
end

#cluster(&block) ⇒ Object



59
60
61
62
63
# File 'lib/vines/config.rb', line 59

def cluster(&block)
  return @cluster unless block
  raise "one cluster definition allowed" if @cluster
  @cluster = Cluster.new(self, &block)
end

#cluster?Boolean

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

Returns:

  • (Boolean)


142
143
144
# File 'lib/vines/config.rb', line 142

def cluster?
  !!@cluster
end

#component?(*jids) ⇒ Boolean

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

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/vines/config.rb', line 109

def component?(*jids)
  !jids.flatten.index do |jid|
    !component_password(JID.new(jid).domain)
  end
end

#component_password(domain) ⇒ Object

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



116
117
118
119
# File 'lib/vines/config.rb', line 116

def component_password(domain)
  host = @vhosts.values.find {|host| host.component?(domain) }
  host.password(domain) if host
end

#host(*names, &block) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/vines/config.rb', line 40

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) ⇒ Boolean

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

Returns:

  • (Boolean)


122
123
124
125
126
# File 'lib/vines/config.rb', line 122

def local_jid?(*jids)
  !jids.flatten.index do |jid|
    !vhost?(JID.new(jid).domain)
  end
end

#log(level) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/vines/config.rb', line 65

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

#portsObject



73
74
75
# File 'lib/vines/config.rb', line 73

def ports
  @ports.values
end

#private_storage?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/vines/config.rb', line 129

def private_storage?(domain)
  host = vhost(domain)
  host.private_storage? if host
end

#pubsub(domain) ⇒ Object

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



97
98
99
100
# File 'lib/vines/config.rb', line 97

def pubsub(domain)
  host = @vhosts.values.find {|host| host.pubsub?(domain) }
  host.pubsubs[domain.to_s] if host
end

#pubsub?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


104
105
106
# File 'lib/vines/config.rb', line 104

def pubsub?(domain)
  @vhosts.values.any? {|host| host.pubsub?(domain) }
end

#s2s?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


136
137
138
# File 'lib/vines/config.rb', line 136

def s2s?(domain)
  @ports[:server] && @ports[:server].hosts.include?(domain.to_s)
end

#storage(domain) ⇒ Object

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



90
91
92
93
# File 'lib/vines/config.rb', line 90

def storage(domain)
  host = vhost(domain)
  host ? host.storage : @null
end

#vhost(domain) ⇒ Object

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



84
85
86
# File 'lib/vines/config.rb', line 84

def vhost(domain)
  @vhosts[domain.to_s]
end

#vhost?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


78
79
80
# File 'lib/vines/config.rb', line 78

def vhost?(domain)
  !!vhost(domain)
end