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 attr_reader :auth_path
29 def initialize(key, params)
30 @manager = BotConfig::configmanager
31 # Keys must be in the form 'module.name'.
32 # They will be internally passed around as symbols,
33 # but we accept them both in string and symbol form.
34 unless key.to_s =~ /^.+\..+$/
35 raise ArgumentError,"key must be of the form 'module.name'"
40 if params.has_key? :default
41 @default = params[:default]
46 @type = params[:type] || String
47 @on_change = params[:on_change]
48 @validate = params[:validate]
49 @wizard = params[:wizard]
50 @requires_restart = params[:requires_restart]
51 @requires_rescan = params[:requires_rescan]
52 @auth_path = "config::key::#{key.sub('.','::')}"
55 if @default.instance_of?(Proc)
62 return @manager.config[@key] if @manager.config.has_key?(@key)
66 def set(value, on_change = true)
67 @manager.config[@key] = value
68 @manager.changed = true
69 @on_change.call(@manager.bot, value) if on_change && @on_change
72 @manager.config.delete(@key)
75 # set string will raise ArgumentErrors on failed parse/validate
76 def set_string(string, on_change = true)
81 raise ArgumentError, "invalid value: #{string}"
85 # override this. the default will work for strings only
96 return true unless @validate
97 if @validate.instance_of?(Proc)
98 return @validate.call(value)
99 elsif @validate.instance_of?(Regexp)
100 raise ArgumentError, "validation via Regexp only supported for strings!" unless value.instance_of? String
101 return @validate.match(value)
103 raise ArgumentError, "validation type #{@validate.class} not supported"
108 class BotConfigStringValue < BotConfigValue
111 class BotConfigBooleanValue < BotConfigValue
113 return true if string == "true"
114 return false if string == "false"
115 raise ArgumentError, "#{string} does not match either 'true' or 'false'"
119 class BotConfigIntegerValue < BotConfigValue
121 raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
126 class BotConfigFloatValue < BotConfigValue
128 raise ArgumentError, "not a float #{string}" unless string =~ /^-?[\d.]+$/
133 class BotConfigArrayValue < BotConfigValue
142 set(curval + [val]) unless curval.include?(val)
146 raise ArgumentError, "value #{val} not present" unless curval.include?(val)
151 class BotConfigEnumValue < BotConfigValue
152 def initialize(key, params)
154 @values = params[:values]
157 if @values.instance_of?(Proc)
158 return @values.call(@manager.bot)
164 unless values.include?(string)
165 raise ArgumentError, "invalid value #{string}, allowed values are: " + values.join(", ")
170 "#{@desc} [valid values are: " + values.join(", ") + "]"
174 # container for bot configuration
175 class BotConfigManagerClass
182 attr_accessor :changed
185 bot_associate(nil,true)
190 @config = Hash.new(false)
193 # Associate with bot _bot_
194 def bot_associate(bot, reset=false)
195 reset_config if reset
200 if(File.exist?("#{@bot.botclass}/conf.yaml"))
202 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
203 newconfig.each { |key, val|
204 @config[key.to_sym] = val
208 error "failed to read conf.yaml: #{$!}"
211 # if we got here, we need to run the first-run wizard
212 BotConfigWizard.new(@bot).run
213 # save newly created config
219 unless item.kind_of?(BotConfigValue)
220 raise ArgumentError,"item must be a BotConfigValue"
222 @items[item.key] = item
225 # currently we store values in a hash but this could be changed in the
226 # future. We use hash semantics, however.
227 # components that register their config keys and setup defaults are
230 # return @items[key].value if @items.has_key?(key)
231 return @items[key.to_sym].value if @items.has_key?(key.to_sym)
232 # try to still support unregistered lookups
233 # but warn about them
234 # if @config.has_key?(key)
235 # warning "Unregistered lookup #{key.inspect}"
236 # return @config[key]
238 if @config.has_key?(key.to_sym)
239 warning "Unregistered lookup #{key.to_sym.inspect}"
240 return @config[key.to_sym]
245 # TODO should I implement this via BotConfigValue or leave it direct?
246 # def []=(key, value)
249 # pass everything else through to the hash
250 def method_missing(method, *args, &block)
251 return @config.send(method, *args, &block)
254 # write current configuration to #{botclass}/conf.yaml
257 debug "Not writing conf.yaml (unchanged)"
261 debug "Writing new conf.yaml ..."
262 File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
264 @config.each { |key, val|
265 savehash[key.to_s] = val
267 file.puts savehash.to_yaml
269 debug "Officializing conf.yaml ..."
270 File.rename("#{@bot.botclass}/conf.yaml.new",
271 "#{@bot.botclass}/conf.yaml")
273 error "failed to write configuration file conf.yaml! #{$!}"
274 error "#{e.class}: #{e}"
275 error e.backtrace.join("\n")
281 # Returns the only BotConfigManagerClass
283 def BotConfig.configmanager
284 return BotConfigManagerClass.instance
287 # Register a config value
288 def BotConfig.register(item)
289 BotConfig.configmanager.register(item)
293 class BotConfigWizard
296 @manager = BotConfig::configmanager
297 @questions = @manager.items.values.find_all {|i| i.wizard }
301 puts "First time rbot configuration wizard"
302 puts "===================================="
303 puts "This is the first time you have run rbot with a config directory of:"
305 puts "This wizard will ask you a few questions to get you started."
306 puts "The rest of rbot's configuration can be manipulated via IRC once"
307 puts "rbot is connected and you are auth'd."
308 puts "-----------------------------------"
310 return unless @questions
311 @questions.sort{|a,b| a.order <=> b.order }.each do |q|
314 print q.key.to_s + " [#{q.to_s}]: "
315 response = STDIN.gets
317 unless response.empty?
318 q.set_string response, false
320 puts "configured #{q.key} => #{q.to_s}"
321 puts "-----------------------------------"
322 rescue ArgumentError => e
323 puts "failed to set #{q.key}: #{e.message}"