Class: Irc::Bot::Config::ManagerClass

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/config.rb

Overview

container for bot configuration

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Singleton

#_dump

Constructor Details

- (ManagerClass) initialize

Returns a new instance of ManagerClass



236
237
238
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 236

def initialize
  bot_associate(nil,true)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &block)

pass everything else through to the hash



330
331
332
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 330

def method_missing(method, *args, &block)
  return @config.send(method, *args, &block)
end

Instance Attribute Details

- (Object) bot (readonly)

Returns the value of attribute bot



230
231
232
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 230

def bot
  @bot
end

- (Object) changed

Returns the value of attribute changed



234
235
236
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 234

def changed
  @changed
end

- (Object) config (readonly)

Returns the value of attribute config



232
233
234
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 232

def config
  @config
end

- (Object) items (readonly)

Returns the value of attribute items



231
232
233
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 231

def items
  @items
end

- (Object) overrides (readonly)

Returns the value of attribute overrides



233
234
235
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 233

def overrides
  @overrides
end

Instance Method Details

- (Object) [](key)

currently we store values in a hash but this could be changed in the future. We use hash semantics, however. components that register their config keys and setup defaults are supported via []



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 305

def [](key)
  # return @items[key].value if @items.has_key?(key)
  return @items[key.to_sym].value if @items.has_key?(key.to_sym)
  # try to still support unregistered lookups
  # but warn about them
  #      if @config.has_key?(key)
  #        warning "Unregistered lookup #{key.inspect}"
  #        return @config[key]
  #      end
  if @config.has_key?(key.to_sym)
    warning _("Unregistered lookup #{key.to_sym.inspect}")
    return @config[key.to_sym]
  end
  return false
end

- (Object) []=(key, value)



321
322
323
324
325
326
327
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 321

def []=(key, value)
  return @items[key.to_sym].set(value) if @items.has_key?(key.to_sym)
  if @config.has_key?(key.to_sym)
    warning _("Unregistered lookup #{key.to_sym.inspect}")
    return @config[key.to_sym] = value
  end
end

- (Object) bot_associate(bot, reset = false)

Associate with bot bot



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 261

def bot_associate(bot, reset=false)
  reset_config if reset
  @bot = bot
  return unless @bot

  @changed = false
  conf = @bot.path 'conf.yaml'
  if File.exist? conf
    begin
      newconfig = YAML::load_file conf
      newconfig.each { |key, val|
        @config[key.to_sym] = val
      }
      return
    rescue
      error "failed to read conf.yaml: #{$!}"
    end
  end
  # config options with :store_default to true should store
  # their default value at first run.
  # Some defaults might change anytime the bot starts
  #  for instance core.db or authpw
  @items.values.find_all {|i| i.store_default }.each do |value|
    @config[value.key] = value.default
  end

  # if we got here, we need to run the first-run wizard
  Wizard.new(@bot).run
  # save newly created config
  @changed = true
  save
end

- (Object) register(item)



294
295
296
297
298
299
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 294

def register(item)
  unless item.kind_of?(Value)
    raise ArgumentError,"item must be an Irc::Bot::Config::Value"
  end
  @items[item.key] = item
end

- (Object) reset_config



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 240

def reset_config
  @items = Hash.new
  @config = Hash.new(false)

  # We allow default values for config keys to be overridden by
  # the config file /etc/rbot.conf
  # The main purpose for this is to allow distro or system-wide
  # settings such as external program paths (figlet, toilet, ispell)
  # to be set once for all the bots.
  @overrides = Hash.new
  etcfile = "/etc/rbot.conf"
  if File.exist?(etcfile)
    log "Loading defaults from #{etcfile}"
    etcconf = YAML::load_file(etcfile)
    etcconf.each { |k, v|
      @overrides[k.to_sym] = v
    }
  end
end

- (Object) save

write current configuration to #botclass/conf.yaml



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File '/home/apoc/projects/ruby/rbot/lib/rbot/config.rb', line 335

def save
  if not @changed
    debug "Not writing conf.yaml (unchanged)"
    return
  end
  begin
	conf = @bot.path 'conf.yaml'
	fnew = conf + '.new'
    debug "Writing new conf.yaml ..."
    File.open(fnew, "w") do |file|
      savehash = {}
      @config.each { |key, val|
        savehash[key.to_s] = val
      }
      file.puts savehash.to_yaml
    end
    debug "Officializing conf.yaml ..."
    File.rename(fnew, conf)
    @changed = false
  rescue => e
    error "failed to write configuration file conf.yaml! #{$!}"
    error "#{e.class}: #{e}"
    error e.backtrace.join("\n")
  end
end