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
76 # set string will raise ArgumentErrors on failed parse/validate
77 def set_string(string, on_change = true)
82 raise ArgumentError, "invalid value: #{string}"
86 # override this. the default will work for strings only
97 return true unless @validate
98 if @validate.instance_of?(Proc)
99 return @validate.call(value)
100 elsif @validate.instance_of?(Regexp)
101 raise ArgumentError, "validation via Regexp only supported for strings!" unless value.instance_of? String
102 return @validate.match(value)
104 raise ArgumentError, "validation type #{@validate.class} not supported"
109 class BotConfigStringValue < BotConfigValue
112 class BotConfigBooleanValue < BotConfigValue
114 return true if string == "true"
115 return false if string == "false"
116 raise ArgumentError, "#{string} does not match either 'true' or 'false'"
120 class BotConfigIntegerValue < BotConfigValue
122 raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
127 class BotConfigFloatValue < BotConfigValue
129 raise ArgumentError, "not a float #{string}" unless string =~ /^-?[\d.]+$/
134 class BotConfigArrayValue < BotConfigValue
143 set(curval + [val]) unless curval.include?(val)
147 raise ArgumentError, "value #{val} not present" unless curval.include?(val)
152 class BotConfigEnumValue < BotConfigValue
153 def initialize(key, params)
155 @values = params[:values]
158 if @values.instance_of?(Proc)
159 return @values.call(@manager.bot)
165 unless values.include?(string)
166 raise ArgumentError, "invalid value #{string}, allowed values are: " + values.join(", ")
171 "#{@desc} [valid values are: " + values.join(", ") + "]"
175 # container for bot configuration
176 class BotConfigManagerClass
183 attr_accessor :changed
186 bot_associate(nil,true)
191 @config = Hash.new(false)
194 # Associate with bot _bot_
195 def bot_associate(bot, reset=false)
196 reset_config if reset
201 if(File.exist?("#{@bot.botclass}/conf.yaml"))
203 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
204 newconfig.each { |key, val|
205 @config[key.to_sym] = val
209 error "failed to read conf.yaml: #{$!}"
212 # if we got here, we need to run the first-run wizard
213 BotConfigWizard.new(@bot).run
214 # save newly created config
220 unless item.kind_of?(BotConfigValue)
221 raise ArgumentError,"item must be a BotConfigValue"
223 @items[item.key] = item
226 # currently we store values in a hash but this could be changed in the
227 # future. We use hash semantics, however.
228 # components that register their config keys and setup defaults are
231 # return @items[key].value if @items.has_key?(key)
232 return @items[key.to_sym].value if @items.has_key?(key.to_sym)
233 # try to still support unregistered lookups
234 # but warn about them
235 # if @config.has_key?(key)
236 # warning "Unregistered lookup #{key.inspect}"
237 # return @config[key]
239 if @config.has_key?(key.to_sym)
240 warning "Unregistered lookup #{key.to_sym.inspect}"
241 return @config[key.to_sym]
246 # TODO should I implement this via BotConfigValue or leave it direct?
247 # def []=(key, value)
250 # pass everything else through to the hash
251 def method_missing(method, *args, &block)
252 return @config.send(method, *args, &block)
255 # write current configuration to #{botclass}/conf.yaml
258 debug "Not writing conf.yaml (unchanged)"
262 debug "Writing new conf.yaml ..."
263 File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
265 @config.each { |key, val|
266 savehash[key.to_s] = val
268 file.puts savehash.to_yaml
270 debug "Officializing conf.yaml ..."
271 File.rename("#{@bot.botclass}/conf.yaml.new",
272 "#{@bot.botclass}/conf.yaml")
275 error "failed to write configuration file conf.yaml! #{$!}"
276 error "#{e.class}: #{e}"
277 error e.backtrace.join("\n")
283 # Returns the only BotConfigManagerClass
285 def BotConfig.configmanager
286 return BotConfigManagerClass.instance
289 # Register a config value
290 def BotConfig.register(item)
291 BotConfig.configmanager.register(item)
295 class BotConfigWizard
298 @manager = BotConfig::configmanager
299 @questions = @manager.items.values.find_all {|i| i.wizard }
303 puts "First time rbot configuration wizard"
304 puts "===================================="
305 puts "This is the first time you have run rbot with a config directory of:"
307 puts "This wizard will ask you a few questions to get you started."
308 puts "The rest of rbot's configuration can be manipulated via IRC once"
309 puts "rbot is connected and you are auth'd."
310 puts "-----------------------------------"
312 return unless @questions
313 @questions.sort{|a,b| a.order <=> b.order }.each do |q|
316 print q.key.to_s + " [#{q.to_s}]: "
317 response = STDIN.gets
319 unless response.empty?
320 q.set_string response, false
322 puts "configured #{q.key} => #{q.to_s}"
323 puts "-----------------------------------"
324 rescue ArgumentError => e
325 puts "failed to set #{q.key}: #{e.message}"