5 unless YAML.respond_to?(:load_file)
6 def YAML.load_file( filepath )
7 File.open( filepath ) do |f|
19 # allow the definition order to be preserved so that sorting by
20 # definition order is possible. The Wizard does this to allow
21 # the :wizard questions to be in a sensible order.
27 attr_reader :requires_restart
28 attr_reader :requires_rescan
31 attr_reader :auth_path
32 def initialize(key, params)
33 @manager = Config.manager
34 # Keys must be in the form 'module.name'.
35 # They will be internally passed around as symbols,
36 # but we accept them both in string and symbol form.
37 unless key.to_s =~ /^.+\..+$/
38 raise ArgumentError,"key must be of the form 'module.name'"
43 if params.has_key? :default
44 @default = params[:default]
49 @type = params[:type] || String
50 @on_change = params[:on_change]
51 @validate = params[:validate]
52 @wizard = params[:wizard]
53 @requires_restart = params[:requires_restart]
54 @requires_rescan = params[:requires_rescan]
55 @auth_path = "config::key::#{key.sub('.','::')}"
58 if @default.instance_of?(Proc)
65 return @manager.config[@key] if @manager.config.has_key?(@key)
69 def set(value, on_change = true)
70 @manager.config[@key] = value
71 @manager.changed = true
72 @on_change.call(@manager.bot, value) if on_change && @on_change
76 @manager.config.delete(@key)
77 @manager.changed = true
78 @on_change.call(@manager.bot, value) if @on_change
82 # set string will raise ArgumentErrors on failed parse/validate
83 def set_string(string, on_change = true)
88 raise ArgumentError, "invalid value: #{string}"
92 # override this. the default will work for strings only
102 def validate(val, validator = @validate)
107 return validator.call(val)
109 raise ArgumentError, "validation via Regexp only supported for strings!" unless String === val
110 return validator.match(val)
112 raise ArgumentError, "validation type #{validator.class} not supported"
117 class StringValue < Value
120 class BooleanValue < Value
122 return true if string == "true"
123 return false if string == "false"
124 if string =~ /^-?\d+$/
125 return string.to_i != 0
127 raise ArgumentError, "#{string} does not match either 'true' or 'false', and it's not an integer either"
131 if r.kind_of?(Integer)
139 class IntegerValue < Value
141 return 1 if string == "true"
142 return 0 if string == "false"
143 raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
148 if r.kind_of?(Integer)
156 class FloatValue < Value
158 raise ArgumentError, "not a float #{string}" unless string =~ /^-?[\d.]+$/
163 class ArrayValue < Value
164 def initialize(key, params)
166 @validate_item = params[:validate_item]
167 @validate ||= Proc.new do |v|
168 !v.find { |i| !validate_item(i) }
172 def validate_item(item)
173 validate(item, @validate_item)
183 newval = self.get.dup
184 unless newval.include? val
186 validate_item(val) or raise ArgumentError, "invalid item: #{val}"
187 validate(newval) or raise ArgumentError, "invalid value: #{newval.to_s}"
193 raise ArgumentError, "value #{val} not present" unless curval.include?(val)
198 class EnumValue < Value
199 def initialize(key, params)
201 @values = params[:values]
204 if @values.instance_of?(Proc)
205 return @values.call(@manager.bot)
211 unless values.include?(string)
212 raise ArgumentError, "invalid value #{string}, allowed values are: " + values.join(", ")
217 _("%{desc} [valid values are: %{values}]") % {:desc => @desc, :values => values.join(', ')}
221 # container for bot configuration
229 attr_accessor :changed
232 bot_associate(nil,true)
237 @config = Hash.new(false)
240 # Associate with bot _bot_
241 def bot_associate(bot, reset=false)
242 reset_config if reset
247 if(File.exist?("#{@bot.botclass}/conf.yaml"))
249 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
250 newconfig.each { |key, val|
251 @config[key.to_sym] = val
255 error "failed to read conf.yaml: #{$!}"
258 # if we got here, we need to run the first-run wizard
260 # save newly created config
266 unless item.kind_of?(Value)
267 raise ArgumentError,"item must be an Irc::Bot::Config::Value"
269 @items[item.key] = item
272 # currently we store values in a hash but this could be changed in the
273 # future. We use hash semantics, however.
274 # components that register their config keys and setup defaults are
277 # return @items[key].value if @items.has_key?(key)
278 return @items[key.to_sym].value if @items.has_key?(key.to_sym)
279 # try to still support unregistered lookups
280 # but warn about them
281 # if @config.has_key?(key)
282 # warning "Unregistered lookup #{key.inspect}"
283 # return @config[key]
285 if @config.has_key?(key.to_sym)
286 warning _("Unregistered lookup #{key.to_sym.inspect}")
287 return @config[key.to_sym]
293 return @items[key.to_sym].set(value) if @items.has_key?(key.to_sym)
294 if @config.has_key?(key.to_sym)
295 warning _("Unregistered lookup #{key.to_sym.inspect}")
296 return @config[key.to_sym] = value
300 # pass everything else through to the hash
301 def method_missing(method, *args, &block)
302 return @config.send(method, *args, &block)
305 # write current configuration to #{botclass}/conf.yaml
308 debug "Not writing conf.yaml (unchanged)"
312 debug "Writing new conf.yaml ..."
313 File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
315 @config.each { |key, val|
316 savehash[key.to_s] = val
318 file.puts savehash.to_yaml
320 debug "Officializing conf.yaml ..."
321 File.rename("#{@bot.botclass}/conf.yaml.new",
322 "#{@bot.botclass}/conf.yaml")
325 error "failed to write configuration file conf.yaml! #{$!}"
326 error "#{e.class}: #{e}"
327 error e.backtrace.join("\n")
332 # Returns the only Irc::Bot::Config::ManagerClass
335 return ManagerClass.instance
338 # Register a config value
339 def Config.register(item)
340 Config.manager.register(item)
346 @manager = Config.manager
347 @questions = @manager.items.values.find_all {|i| i.wizard }
351 puts _("First time rbot configuration wizard")
352 puts "===================================="
353 puts _("This is the first time you have run rbot with a config directory of: #{@bot.botclass}")
354 puts _("This wizard will ask you a few questions to get you started.")
355 puts _("The rest of rbot's configuration can be manipulated via IRC once rbot is connected and you are auth'd.")
356 puts "-----------------------------------"
358 return unless @questions
359 @questions.sort{|a,b| a.order <=> b.order }.each do |q|
362 print q.key.to_s + " [#{q.to_s}]: "
363 response = STDIN.gets
365 unless response.empty?
366 q.set_string response, false
368 puts _("configured #{q.key} => #{q.to_s}")
369 puts "-----------------------------------"
370 rescue ArgumentError => e
371 puts _("failed to set #{q.key}: #{e.message}")