4 require 'rbot/messagemapper'
6 unless YAML.respond_to?(:load_file)
8 def YAML.load_file( filepath )
9 File.open( filepath ) do |f|
17 # allow the definition order to be preserved so that sorting by
18 # definition order is possible. The BotConfigWizard does this to allow
19 # the :wizard questions to be in a sensible order.
25 attr_reader :requires_restart
27 def initialize(key, params)
28 unless key =~ /^.+\..+$/
29 raise ArgumentError,"key must be of the form 'module.name'"
34 if params.has_key? :default
35 @default = params[:default]
40 @type = params[:type] || String
41 @on_change = params[:on_change]
42 @validate = params[:validate]
43 @wizard = params[:wizard]
44 @requires_restart = params[:requires_restart]
47 if @default.instance_of?(Proc)
54 return BotConfig.config[@key] if BotConfig.config.has_key?(@key)
58 def set(value, on_change = true)
59 BotConfig.config[@key] = value
60 @on_change.call(BotConfig.bot, value) if on_change && @on_change
63 BotConfig.config.delete(@key)
66 # set string will raise ArgumentErrors on failed parse/validate
67 def set_string(string, on_change = true)
72 raise ArgumentError, "invalid value: #{string}"
76 # override this. the default will work for strings only
87 return true unless @validate
88 if @validate.instance_of?(Proc)
89 return @validate.call(value)
90 elsif @validate.instance_of?(Regexp)
91 raise ArgumentError, "validation via Regexp only supported for strings!" unless value.instance_of? String
92 return @validate.match(value)
94 raise ArgumentError, "validation type #{@validate.class} not supported"
99 class BotConfigStringValue < BotConfigValue
101 class BotConfigBooleanValue < BotConfigValue
103 return true if string == "true"
104 return false if string == "false"
105 raise ArgumentError, "#{string} does not match either 'true' or 'false'"
108 class BotConfigIntegerValue < BotConfigValue
110 raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
114 class BotConfigFloatValue < BotConfigValue
116 raise ArgumentError, "not a float #{string}" unless string =~ /^-?[\d.]+$/
120 class BotConfigArrayValue < BotConfigValue
128 class BotConfigEnumValue < BotConfigValue
129 def initialize(key, params)
131 @values = params[:values]
134 if @values.instance_of?(Proc)
135 return @values.call(BotConfig.bot)
141 unless @values.include?(string)
142 raise ArgumentError, "invalid value #{string}, allowed values are: " + @values.join(", ")
147 "#{@desc} [valid values are: " + values.join(", ") + "]"
151 # container for bot configuration
153 # Array of registered BotConfigValues for defaults, types and help
158 # Hash containing key => value pairs for lookup and serialisation
159 @@config = Hash.new(false)
167 def BotConfig.register(item)
168 unless item.kind_of?(BotConfigValue)
169 raise ArgumentError,"item must be a BotConfigValue"
171 @@items[item.key] = item
174 # currently we store values in a hash but this could be changed in the
175 # future. We use hash semantics, however.
176 # components that register their config keys and setup defaults are
179 return @@items[key].value if @@items.has_key?(key)
180 # try to still support unregistered lookups
181 return @@config[key] if @@config.has_key?(key)
185 # TODO should I implement this via BotConfigValue or leave it direct?
186 # def []=(key, value)
189 # pass everything else through to the hash
190 def method_missing(method, *args, &block)
191 return @@config.send(method, *args, &block)
194 def handle_list(m, params)
197 @@items.each_key do |key|
198 mod, name = key.split('.')
199 next unless mod == params[:module]
200 modules.push key unless modules.include?(name)
203 m.reply "no such module #{params[:module]}"
205 m.reply modules.join(", ")
208 @@items.each_key do |key|
209 name = key.split('.').first
210 modules.push name unless modules.include?(name)
212 m.reply "modules: " + modules.join(", ")
216 def handle_get(m, params)
218 unless @@items.has_key?(key)
219 m.reply "no such config key #{key}"
222 value = @@items[key].to_s
223 m.reply "#{key}: #{value}"
226 def handle_desc(m, params)
228 unless @@items.has_key?(key)
229 m.reply "no such config key #{key}"
231 puts @@items[key].inspect
232 m.reply "#{key}: #{@@items[key].desc}"
235 def handle_unset(m, params)
237 unless @@items.has_key?(key)
238 m.reply "no such config key #{key}"
241 handle_get(m, params)
244 def handle_set(m, params)
246 value = params[:value].to_s
247 unless @@items.has_key?(key)
248 m.reply "no such config key #{key}"
252 @@items[key].set_string(value)
253 rescue ArgumentError => e
254 m.reply "failed to set #{key}: #{e.message}"
257 if @@items[key].requires_restart
258 m.reply "this config change will take effect on the next restart"
264 def handle_help(m, params)
265 topic = params[:topic]
268 m.reply "config module - bot configuration. usage: list, desc, get, set, unset"
270 m.reply "config list => list configuration modules, config list <module> => list configuration keys for module <module>"
272 m.reply "config get <key> => get configuration value for key <key>"
274 m.reply "reset key <key> to the default"
276 m.reply "config set <key> <value> => set configuration value for key <key> to <value>"
278 m.reply "config desc <key> => describe what key <key> configures"
280 m.reply "no help for config #{topic}"
284 m.reply "incorrect usage, try '#{@@bot.nick}: help config'"
287 # bot:: parent bot class
288 # create a new config hash from #{botclass}/conf.rbot
292 # respond to config messages, to provide runtime configuration
302 @handler = MessageMapper.new(self)
303 @handler.map 'config list :module', :action => 'handle_list',
304 :defaults => {:module => false}
305 @handler.map 'config get :key', :action => 'handle_get'
306 @handler.map 'config desc :key', :action => 'handle_desc'
307 @handler.map 'config describe :key', :action => 'handle_desc'
308 @handler.map 'config set :key *value', :action => 'handle_set'
309 @handler.map 'config unset :key', :action => 'handle_unset'
310 @handler.map 'config help :topic', :action => 'handle_help',
311 :defaults => {:topic => false}
312 @handler.map 'help config :topic', :action => 'handle_help',
313 :defaults => {:topic => false}
315 if(File.exist?("#{@@bot.botclass}/conf.yaml"))
316 newconfig = YAML::load_file("#{@@bot.botclass}/conf.yaml")
317 @@config.update newconfig
320 BotConfigWizard.new(@@bot).run
321 # save newly created config
326 # write current configuration to #{botclass}/conf.rbot
328 File.open("#{@@bot.botclass}/conf.yaml", "w") do |file|
329 file.puts @@config.to_yaml
338 class BotConfigWizard
341 @questions = BotConfig.items.values.find_all {|i| i.wizard }
345 puts "First time rbot configuration wizard"
346 puts "===================================="
347 puts "This is the first time you have run rbot with a config directory of:"
349 puts "This wizard will ask you a few questions to get you started."
350 puts "The rest of rbot's configuration can be manipulated via IRC once"
351 puts "rbot is connected and you are auth'd."
352 puts "-----------------------------------"
354 return unless @questions
355 @questions.sort{|a,b| a.order <=> b.order }.each do |q|
358 print q.key + " [#{q.to_s}]: "
359 response = STDIN.gets
361 unless response.empty?
362 q.set_string response, false
364 puts "configured #{q.key} => #{q.to_s}"
365 puts "-----------------------------------"
366 rescue ArgumentError => e
367 puts "failed to set #{q.key}: #{e.message}"