Class: Vines::Config
- Inherits:
-
Object
- Object
- Vines::Config
- 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
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Object
Retrieve the Port subclass with this name: [:client, :server, :http, :component].
-
#allowed?(to, from) ⇒ Boolean
Return true if the two JIDs are allowed to send messages to each other.
- #certs(dir = nil) ⇒ Object
- #cluster(&block) ⇒ Object
-
#cluster? ⇒ Boolean
Return true if the server is a member of a cluster, serving the same domains from different machines.
-
#component?(*jids) ⇒ Boolean
Return true if all JIDs belong to components hosted by this server.
-
#component_password(domain) ⇒ Object
Return the password for the component or nil if it’s not hosted here.
- #host(*names, &block) ⇒ Object
-
#initialize(&block) ⇒ Config
constructor
A new instance of Config.
-
#local_jid?(*jids) ⇒ Boolean
Return true if all of the JIDs are hosted by this server.
- #log(level) ⇒ Object
- #ports ⇒ Object
-
#private_storage?(domain) ⇒ Boolean
Return true if private XML fragment storage is enabled for this domain.
-
#pubsub(domain) ⇒ Object
Returns the PubSub system for the domain or nil if pubsub is not enabled for this domain.
-
#pubsub?(domain) ⇒ Boolean
Return true if the domain is a pubsub service hosted at a virtual host at this server.
-
#s2s?(domain) ⇒ Boolean
Returns true if server-to-server connections are allowed with the given domain.
-
#storage(domain) ⇒ Object
Returns the storage system for the domain or a Storage::Null instance if the domain is not hosted at this server.
-
#vhost(domain) ⇒ Object
Return the Host config object for this domain if it’s hosted by this server.
-
#vhost?(domain) ⇒ Boolean
Return true if the domain is virtual hosted by this server.
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.('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
#debug ⇒ Object
Returns the value of attribute debug.
13 14 15 |
# File 'lib/vines/config.rb', line 13 def debug @debug end |
#router ⇒ Object (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 |
.instance ⇒ Object
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.
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.(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.
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.
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.
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 |
#ports ⇒ Object
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.
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.
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.
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.
81 82 83 |
# File 'lib/vines/config.rb', line 81 def vhost?(domain) !!vhost(domain) end |