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

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

Overview

container for bot configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManagerClass

Returns a new instance of ManagerClass.



234
235
236
# File 'lib/rbot/config.rb', line 234

def initialize
  bot_associate(nil,true)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

pass everything else through to the hash



320
321
322
# File 'lib/rbot/config.rb', line 320

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

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



228
229
230
# File 'lib/rbot/config.rb', line 228

def bot
  @bot
end

#changedObject

Returns the value of attribute changed.



232
233
234
# File 'lib/rbot/config.rb', line 232

def changed
  @changed
end

#configObject (readonly)

Returns the value of attribute config.



230
231
232
# File 'lib/rbot/config.rb', line 230

def config
  @config
end

#itemsObject (readonly)

Returns the value of attribute items.



229
230
231
# File 'lib/rbot/config.rb', line 229

def items
  @items
end

#overridesObject (readonly)

Returns the value of attribute overrides.



231
232
233
# File 'lib/rbot/config.rb', line 231

def overrides
  @overrides
end

Instance Method Details

#[](key) ⇒ Object

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 []



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/rbot/config.rb', line 295

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

#[]=(key, value) ⇒ Object



311
312
313
314
315
316
317
# File 'lib/rbot/config.rb', line 311

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

#bot_associate(bot, reset = false) ⇒ Object

Associate with bot bot



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rbot/config.rb', line 259

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

#register(item) ⇒ Object



284
285
286
287
288
289
# File 'lib/rbot/config.rb', line 284

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

#reset_configObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rbot/config.rb', line 238

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

#saveObject

write current configuration to #botclass/conf.yaml



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/rbot/config.rb', line 325

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