7 unless YAML.respond_to?(:load_file)
8 def YAML.load_file( filepath )
9 File.open( filepath ) do |f|
16 # allow the definition order to be preserved so that sorting by
17 # definition order is possible. The BotConfigWizard does this to allow
18 # the :wizard questions to be in a sensible order.
24 attr_reader :requires_restart
25 attr_reader :requires_rescan
28 def initialize(key, params)
29 @manager = BotConfig::configmanager
30 # Keys must be in the form 'module.name'.
31 # They will be internally passed around as symbols,
32 # but we accept them both in string and symbol form.
33 unless key.to_s =~ /^.+\..+$/
34 raise ArgumentError,"key must be of the form 'module.name'"
39 if params.has_key? :default
40 @default = params[:default]
45 @type = params[:type] || String
46 @on_change = params[:on_change]
47 @validate = params[:validate]
48 @wizard = params[:wizard]
49 @requires_restart = params[:requires_restart]
50 @requires_rescan = params[:requires_rescan]
53 if @default.instance_of?(Proc)
60 return @manager.config[@key] if @manager.config.has_key?(@key)
64 def set(value, on_change = true)
65 @manager.config[@key] = value
66 @on_change.call(@manager.bot, value) if on_change && @on_change
69 @manager.config.delete(@key)
72 # set string will raise ArgumentErrors on failed parse/validate
73 def set_string(string, on_change = true)
78 raise ArgumentError, "invalid value: #{string}"
82 # override this. the default will work for strings only
93 return true unless @validate
94 if @validate.instance_of?(Proc)
95 return @validate.call(value)
96 elsif @validate.instance_of?(Regexp)
97 raise ArgumentError, "validation via Regexp only supported for strings!" unless value.instance_of? String
98 return @validate.match(value)
100 raise ArgumentError, "validation type #{@validate.class} not supported"
105 class BotConfigStringValue < BotConfigValue
108 class BotConfigBooleanValue < BotConfigValue
110 return true if string == "true"
111 return false if string == "false"
112 raise ArgumentError, "#{string} does not match either 'true' or 'false'"
116 class BotConfigIntegerValue < BotConfigValue
118 raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
123 class BotConfigFloatValue < BotConfigValue
125 raise ArgumentError, "not a float #{string}" unless string =~ /^-?[\d.]+$/
130 class BotConfigArrayValue < BotConfigValue
139 set(curval + [val]) unless curval.include?(val)
143 raise ArgumentError, "value #{val} not present" unless curval.include?(val)
148 class BotConfigEnumValue < BotConfigValue
149 def initialize(key, params)
151 @values = params[:values]
154 if @values.instance_of?(Proc)
155 return @values.call(@manager.bot)
161 unless values.include?(string)
162 raise ArgumentError, "invalid value #{string}, allowed values are: " + values.join(", ")
167 "#{@desc} [valid values are: " + values.join(", ") + "]"
171 # container for bot configuration
172 class BotConfigManagerClass
181 bot_associate(nil,true)
186 @config = Hash.new(false)
189 # Associate with bot _bot_
190 def bot_associate(bot, reset=false)
191 reset_config if reset
195 if(File.exist?("#{@bot.botclass}/conf.yaml"))
197 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
198 newconfig.each { |key, val|
199 @config[key.to_sym] = val
203 error "failed to read conf.yaml: #{$!}"
206 # if we got here, we need to run the first-run wizard
207 BotConfigWizard.new(@bot).run
208 # save newly created config
213 unless item.kind_of?(BotConfigValue)
214 raise ArgumentError,"item must be a BotConfigValue"
216 @items[item.key] = item
219 # currently we store values in a hash but this could be changed in the
220 # future. We use hash semantics, however.
221 # components that register their config keys and setup defaults are
224 # return @items[key].value if @items.has_key?(key)
225 return @items[key.to_sym].value if @items.has_key?(key.to_sym)
226 # try to still support unregistered lookups
227 # but warn about them
228 # if @config.has_key?(key)
229 # warning "Unregistered lookup #{key.inspect}"
230 # return @config[key]
232 if @config.has_key?(key.to_sym)
233 warning "Unregistered lookup #{key.to_sym.inspect}"
234 return @config[key.to_sym]
239 # TODO should I implement this via BotConfigValue or leave it direct?
240 # def []=(key, value)
243 # pass everything else through to the hash
244 def method_missing(method, *args, &block)
245 return @config.send(method, *args, &block)
248 # write current configuration to #{botclass}/conf.yaml
251 debug "Writing new conf.yaml ..."
252 File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
254 @config.each { |key, val|
255 savehash[key.to_s] = val
257 file.puts savehash.to_yaml
259 debug "Officializing conf.yaml ..."
260 File.rename("#{@bot.botclass}/conf.yaml.new",
261 "#{@bot.botclass}/conf.yaml")
263 error "failed to write configuration file conf.yaml! #{$!}"
264 error "#{e.class}: #{e}"
265 error e.backtrace.join("\n")
271 # Returns the only BotConfigManagerClass
273 def BotConfig.configmanager
274 return BotConfigManagerClass.instance
277 # Register a config value
278 def BotConfig.register(item)
279 BotConfig.configmanager.register(item)
283 class BotConfigWizard
286 @manager = BotConfig::configmanager
287 @questions = @manager.items.values.find_all {|i| i.wizard }
291 puts "First time rbot configuration wizard"
292 puts "===================================="
293 puts "This is the first time you have run rbot with a config directory of:"
295 puts "This wizard will ask you a few questions to get you started."
296 puts "The rest of rbot's configuration can be manipulated via IRC once"
297 puts "rbot is connected and you are auth'd."
298 puts "-----------------------------------"
300 return unless @questions
301 @questions.sort{|a,b| a.order <=> b.order }.each do |q|
304 print q.key.to_s + " [#{q.to_s}]: "
305 response = STDIN.gets
307 unless response.empty?
308 q.set_string response, false
310 puts "configured #{q.key} => #{q.to_s}"
311 puts "-----------------------------------"
312 rescue ArgumentError => e
313 puts "failed to set #{q.key}: #{e.message}"