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)
73 @manager.changed = true
74 @on_change.call(@manager.bot, value) if @on_change
77 # set string will raise ArgumentErrors on failed parse/validate
78 def set_string(string, on_change = true)
83 raise ArgumentError, "invalid value: #{string}"
87 # override this. the default will work for strings only
98 return true unless @validate
99 if @validate.instance_of?(Proc)
100 return @validate.call(value)
101 elsif @validate.instance_of?(Regexp)
102 raise ArgumentError, "validation via Regexp only supported for strings!" unless value.instance_of? String
103 return @validate.match(value)
105 raise ArgumentError, "validation type #{@validate.class} not supported"
110 class BotConfigStringValue < BotConfigValue
113 class BotConfigBooleanValue < BotConfigValue
115 return true if string == "true"
116 return false if string == "false"
117 raise ArgumentError, "#{string} does not match either 'true' or 'false'"
121 class BotConfigIntegerValue < BotConfigValue
123 raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
128 class BotConfigFloatValue < BotConfigValue
130 raise ArgumentError, "not a float #{string}" unless string =~ /^-?[\d.]+$/
135 class BotConfigArrayValue < BotConfigValue
144 set(curval + [val]) unless curval.include?(val)
148 raise ArgumentError, "value #{val} not present" unless curval.include?(val)
153 class BotConfigEnumValue < BotConfigValue
154 def initialize(key, params)
156 @values = params[:values]
159 if @values.instance_of?(Proc)
160 return @values.call(@manager.bot)
166 unless values.include?(string)
167 raise ArgumentError, "invalid value #{string}, allowed values are: " + values.join(", ")
172 "#{@desc} [valid values are: " + values.join(", ") + "]"
176 # container for bot configuration
177 class BotConfigManagerClass
184 attr_accessor :changed
187 bot_associate(nil,true)
192 @config = Hash.new(false)
195 # Associate with bot _bot_
196 def bot_associate(bot, reset=false)
197 reset_config if reset
202 if(File.exist?("#{@bot.botclass}/conf.yaml"))
204 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
205 newconfig.each { |key, val|
206 @config[key.to_sym] = val
210 error "failed to read conf.yaml: #{$!}"
213 # if we got here, we need to run the first-run wizard
214 BotConfigWizard.new(@bot).run
215 # save newly created config
221 unless item.kind_of?(BotConfigValue)
222 raise ArgumentError,"item must be a BotConfigValue"
224 @items[item.key] = item
227 # currently we store values in a hash but this could be changed in the
228 # future. We use hash semantics, however.
229 # components that register their config keys and setup defaults are
232 # return @items[key].value if @items.has_key?(key)
233 return @items[key.to_sym].value if @items.has_key?(key.to_sym)
234 # try to still support unregistered lookups
235 # but warn about them
236 # if @config.has_key?(key)
237 # warning "Unregistered lookup #{key.inspect}"
238 # return @config[key]
240 if @config.has_key?(key.to_sym)
241 warning "Unregistered lookup #{key.to_sym.inspect}"
242 return @config[key.to_sym]
247 # TODO should I implement this via BotConfigValue or leave it direct?
248 # def []=(key, value)
251 # pass everything else through to the hash
252 def method_missing(method, *args, &block)
253 return @config.send(method, *args, &block)
256 # write current configuration to #{botclass}/conf.yaml
259 debug "Not writing conf.yaml (unchanged)"
263 debug "Writing new conf.yaml ..."
264 File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
266 @config.each { |key, val|
267 savehash[key.to_s] = val
269 file.puts savehash.to_yaml
271 debug "Officializing conf.yaml ..."
272 File.rename("#{@bot.botclass}/conf.yaml.new",
273 "#{@bot.botclass}/conf.yaml")
276 error "failed to write configuration file conf.yaml! #{$!}"
277 error "#{e.class}: #{e}"
278 error e.backtrace.join("\n")
284 # Returns the only BotConfigManagerClass
286 def BotConfig.configmanager
287 return BotConfigManagerClass.instance
290 # Register a config value
291 def BotConfig.register(item)
292 BotConfig.configmanager.register(item)
296 class BotConfigWizard
299 @manager = BotConfig::configmanager
300 @questions = @manager.items.values.find_all {|i| i.wizard }
304 puts "First time rbot configuration wizard"
305 puts "===================================="
306 puts "This is the first time you have run rbot with a config directory of:"
308 puts "This wizard will ask you a few questions to get you started."
309 puts "The rest of rbot's configuration can be manipulated via IRC once"
310 puts "rbot is connected and you are auth'd."
311 puts "-----------------------------------"
313 return unless @questions
314 @questions.sort{|a,b| a.order <=> b.order }.each do |q|
317 print q.key.to_s + " [#{q.to_s}]: "
318 response = STDIN.gets
320 unless response.empty?
321 q.set_string response, false
323 puts "configured #{q.key} => #{q.to_s}"
324 puts "-----------------------------------"
325 rescue ArgumentError => e
326 puts "failed to set #{q.key}: #{e.message}"