4 require 'rbot/messagemapper'
7 # allow the definition order to be preserved so that sorting by
8 # definition order is possible. The BotConfigWizard does this to allow
9 # the :wizard questions to be in a sensible order.
15 attr_reader :requires_restart
17 def initialize(key, params)
18 unless key =~ /^.+\..+$/
19 raise ArgumentError,"key must be of the form 'module.name'"
24 if params.has_key? :default
25 @default = params[:default]
30 @type = params[:type] || String
31 @on_change = params[:on_change]
32 @validate = params[:validate]
33 @wizard = params[:wizard]
34 @requires_restart = params[:requires_restart]
37 if @default.instance_of?(Proc)
44 return BotConfig.config[@key] if BotConfig.config.has_key?(@key)
48 def set(value, on_change = true)
49 BotConfig.config[@key] = value
50 @on_change.call(BotConfig.bot, value) if on_change && @on_change
53 BotConfig.config.delete(@key)
56 # set string will raise ArgumentErrors on failed parse/validate
57 def set_string(string, on_change = true)
62 raise ArgumentError, "invalid value: #{string}"
66 # override this. the default will work for strings only
77 return true unless @validate
78 if @validate.instance_of?(Proc)
79 return @validate.call(value)
80 elsif @validate.instance_of?(Regexp)
81 raise ArgumentError, "validation via Regexp only supported for strings!" unless value.instance_of? String
82 return @validate.match(value)
84 raise ArgumentError, "validation type #{@validate.class} not supported"
89 class BotConfigStringValue < BotConfigValue
91 class BotConfigBooleanValue < BotConfigValue
93 return true if string == "true"
94 return false if string == "false"
95 raise ArgumentError, "#{string} does not match either 'true' or 'false'"
98 class BotConfigIntegerValue < BotConfigValue
100 raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
104 class BotConfigFloatValue < BotConfigValue
106 raise ArgumentError, "not a float #{string}" unless string =~ /^-?[\d.]+$/
110 class BotConfigArrayValue < BotConfigValue
118 class BotConfigEnumValue < BotConfigValue
119 def initialize(key, params)
121 @values = params[:values]
124 if @values.instance_of?(Proc)
125 return @values.call(BotConfig.bot)
131 unless @values.include?(string)
132 raise ArgumentError, "invalid value #{string}, allowed values are: " + @values.join(", ")
137 "#{@desc} [valid values are: " + values.join(", ") + "]"
141 # container for bot configuration
143 # Array of registered BotConfigValues for defaults, types and help
148 # Hash containing key => value pairs for lookup and serialisation
149 @@config = Hash.new(false)
157 def BotConfig.register(item)
158 unless item.kind_of?(BotConfigValue)
159 raise ArgumentError,"item must be a BotConfigValue"
161 @@items[item.key] = item
164 # currently we store values in a hash but this could be changed in the
165 # future. We use hash semantics, however.
166 # components that register their config keys and setup defaults are
169 return @@items[key].value if @@items.has_key?(key)
170 # try to still support unregistered lookups
171 return @@config[key] if @@config.has_key?(key)
175 # TODO should I implement this via BotConfigValue or leave it direct?
176 # def []=(key, value)
179 # pass everything else through to the hash
180 def method_missing(method, *args, &block)
181 return @@config.send(method, *args, &block)
184 def handle_list(m, params)
187 @@items.each_key do |key|
188 mod, name = key.split('.')
189 next unless mod == params[:module]
190 modules.push key unless modules.include?(name)
193 m.reply "no such module #{params[:module]}"
195 m.reply modules.join(", ")
198 @@items.each_key do |key|
199 name = key.split('.').first
200 modules.push name unless modules.include?(name)
202 m.reply "modules: " + modules.join(", ")
206 def handle_get(m, params)
208 unless @@items.has_key?(key)
209 m.reply "no such config key #{key}"
212 value = @@items[key].to_s
213 m.reply "#{key}: #{value}"
216 def handle_desc(m, params)
218 unless @@items.has_key?(key)
219 m.reply "no such config key #{key}"
221 puts @@items[key].inspect
222 m.reply "#{key}: #{@@items[key].desc}"
225 def handle_unset(m, params)
227 unless @@items.has_key?(key)
228 m.reply "no such config key #{key}"
231 handle_get(m, params)
234 def handle_set(m, params)
236 value = params[:value].to_s
237 unless @@items.has_key?(key)
238 m.reply "no such config key #{key}"
242 @@items[key].set_string(value)
243 rescue ArgumentError => e
244 m.reply "failed to set #{key}: #{e.message}"
247 if @@items[key].requires_restart
248 m.reply "this config change will take effect on the next restart"
254 def handle_help(m, params)
255 topic = params[:topic]
258 m.reply "config module - bot configuration. usage: list, desc, get, set, unset"
260 m.reply "config list => list configuration modules, config list <module> => list configuration keys for module <module>"
262 m.reply "config get <key> => get configuration value for key <key>"
264 m.reply "reset key <key> to the default"
266 m.reply "config set <key> <value> => set configuration value for key <key> to <value>"
268 m.reply "config desc <key> => describe what key <key> configures"
270 m.reply "no help for config #{topic}"
274 m.reply "incorrect usage, try '#{@@bot.nick}: help config'"
277 # bot:: parent bot class
278 # create a new config hash from #{botclass}/conf.rbot
282 # respond to config messages, to provide runtime configuration
292 @handler = MessageMapper.new(self)
293 @handler.map 'config list :module', :action => 'handle_list',
294 :defaults => {:module => false}
295 @handler.map 'config get :key', :action => 'handle_get'
296 @handler.map 'config desc :key', :action => 'handle_desc'
297 @handler.map 'config describe :key', :action => 'handle_desc'
298 @handler.map 'config set :key *value', :action => 'handle_set'
299 @handler.map 'config unset :key', :action => 'handle_unset'
300 @handler.map 'config help :topic', :action => 'handle_help',
301 :defaults => {:topic => false}
302 @handler.map 'help config :topic', :action => 'handle_help',
303 :defaults => {:topic => false}
305 if(File.exist?("#{@@bot.botclass}/conf.yaml"))
306 newconfig = YAML::load_file("#{@@bot.botclass}/conf.yaml")
307 @@config.update newconfig
310 BotConfigWizard.new(@@bot).run
311 # save newly created config
316 # write current configuration to #{botclass}/conf.rbot
318 File.open("#{@@bot.botclass}/conf.yaml", "w") do |file|
319 file.puts @@config.to_yaml
328 class BotConfigWizard
331 @questions = BotConfig.items.values.find_all {|i| i.wizard }
335 puts "First time rbot configuration wizard"
336 puts "===================================="
337 puts "This is the first time you have run rbot with a config directory of:"
339 puts "This wizard will ask you a few questions to get you started."
340 puts "The rest of rbot's configuration can be manipulated via IRC once"
341 puts "rbot is connected and you are auth'd."
342 puts "-----------------------------------"
344 return unless @questions
345 @questions.sort{|a,b| a.order <=> b.order }.each do |q|
348 print q.key + " [#{q.to_s}]: "
349 response = STDIN.gets
351 unless response.empty?
352 q.set_string response, false
354 puts "configured #{q.key} => #{q.to_s}"
355 puts "-----------------------------------"
356 rescue ArgumentError => e
357 puts "failed to set #{q.key}: #{e.message}"