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
# 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?
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


158
159
160
# File 'lib/vines/config.rb', line 158

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)


164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vines/config.rb', line 164

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



32
33
34
# File 'lib/vines/config.rb', line 32

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

#cluster(&block) ⇒ Object



69
70
71
72
73
# File 'lib/vines/config.rb', line 69

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)


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

def cluster?
  !!@cluster
end

#component?(*jids) ⇒ Boolean

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

Returns:

  • (Boolean)


119
120
121
122
123
# File 'lib/vines/config.rb', line 119

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.



126
127
128
129
# File 'lib/vines/config.rb', line 126

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

#domain_nameObject



53
54
55
56
57
# File 'lib/vines/config.rb', line 53

def domain_name
  AppConfig.environment.url
    .gsub(/^http(s){0,1}:\/\/|\/$/, '')
    .to_s rescue "localhost"
end

#host(*names, &block) ⇒ Object



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

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|
    if name.eql? "lygneo"
      @vhosts[domain_name] = Host.new(self, domain_name, &block)
    else
      @vhosts[name] = Host.new(self, name, &block)
    end
  end
end

#local_jid?(*jids) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

#log(level) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/vines/config.rb', line 75

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

#pepper(pepper = nil) ⇒ Object



49
50
51
# File 'lib/vines/config.rb', line 49

def pepper(pepper=nil)
  pepper ? @pepper = pepper : @pepper = ""
end

#portsObject



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

def ports
  @ports.values
end

#private_storage?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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.



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

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)


114
115
116
# File 'lib/vines/config.rb', line 114

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)


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

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.



100
101
102
103
# File 'lib/vines/config.rb', line 100

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.



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

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

#vhost?(domain) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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