@order = @@order
@@order += 1
@key = key.to_sym
- if params.has_key? :default
+ if @manager.overrides.key?(@key)
+ @default = @manager.overrides[@key]
+ elsif params.has_key? :default
@default = params[:default]
else
@default = false
unless newval.include? val
newval << val
validate_item(val) or raise ArgumentError, "invalid item: #{val}"
- validate(newval) or raise ArgumentError, "invalid value: #{newval.to_s}"
+ validate(newval) or raise ArgumentError, "invalid value: #{newval.inspect}"
set(newval)
end
end
attr_reader :bot
attr_reader :items
attr_reader :config
+ attr_reader :overrides
attr_accessor :changed
def initialize
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
# Associate with bot _bot_
return unless @bot
@changed = false
- if(File.exist?("#{@bot.botclass}/conf.yaml"))
+ conf = @bot.path 'conf.yaml'
+ if File.exist? conf
begin
- newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
+ newconfig = YAML::load_file conf
newconfig.each { |key, val|
@config[key.to_sym] = val
}
return
end
begin
+ conf = @bot.path 'conf.yaml'
+ fnew = conf + '.new'
debug "Writing new conf.yaml ..."
- File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
+ 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("#{@bot.botclass}/conf.yaml.new",
- "#{@bot.botclass}/conf.yaml")
+ File.rename(fnew, conf)
@changed = false
rescue => e
error "failed to write configuration file conf.yaml! #{$!}"