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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



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

def debug
  @debug
end

#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



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

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

.instanceObject



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

def self.instance
  @@instance
end

Instance Method Details

#[](name) ⇒ Object

Retrieve the Port subclass with this name:

:client, :server, :http, :component


151
152
153
# File 'lib/vines/config.rb', line 151

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)


157
158
159
160
161
162
163
164
165
166
167
168
# 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) ⇒ Object



39
40
41
# File 'lib/vines/config.rb', line 39

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

#cluster(&block) ⇒ Object



62
63
64
65
66
# 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?Boolean

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

Returns:

  • (Boolean)


145
146
147
# File 'lib/vines/config.rb', line 145

def cluster?
  !!@cluster
end

#component?(*jids) ⇒ Boolean

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

Returns:

  • (Boolean)


112
113
114
115
116
# 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) ⇒ Object

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



119
120
121
122
# 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) ⇒ Object



43
44
45
46
47
48
49
50
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


125
126
127
128
129
# 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) ⇒ Object



68
69
70
71
72
73
74
# 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

#portsObject



76
77
78
# File 'lib/vines/config.rb', line 76

def ports
  @ports.values
end

#private_storage?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


132
133
134
135
# File 'lib/vines/config.rb', line 132

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.



100
101
102
103
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/vines/config.rb', line 107

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)


139
140
141
# File 'lib/vines/config.rb', line 139

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.



93
94
95
96
# File 'lib/vines/config.rb', line 93

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.



87
88
89
# File 'lib/vines/config.rb', line 87

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

#vhost?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


81
82
83
# File 'lib/vines/config.rb', line 81

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