4 require 'rbot/messagemapper'
11 def initialize(key, params)
13 if params.has_key? :default
14 @default = params[:default]
19 @type = params[:type] || String
20 @values = params[:values]
21 @on_change = params[:on_change]
24 if @default.class == Proc
30 def on_change(newvalue)
31 return unless @on_change
32 @on_change.call(newvalue)
36 # container for bot configuration
48 def BotConfig.register(key, params)
49 unless params.nil? || params.instance_of?(Hash)
50 raise ArgumentError,"params must be a hash"
52 raise ArgumentError,"params must contain a period" unless key =~ /^.+\..+$/
53 @@items[key] = BotConfigItem.new(key, params)
56 # currently we store values in a hash but this could be changed in the
57 # future. We use hash semantics, however.
58 # components that register their config keys and setup defaults are
61 return @config[key] if @config.has_key?(key)
62 return @@items[key].default if @@items.has_key?(key)
66 # pass everything through to the hash
67 def method_missing(method, *args, &block)
68 return @config.send(method, *args, &block)
71 def handle_list(m, params)
74 @@items.each_key do |key|
75 mod, name = key.split('.')
76 next unless mod == params[:module]
77 modules.push name unless modules.include?(name)
80 m.reply "no such module #{params[:module]}"
82 m.reply "module #{params[:module]} contains: " + modules.join(", ")
85 @@items.each_key do |key|
86 name = key.split('.').first
87 modules.push name unless modules.include?(name)
89 m.reply "modules: " + modules.join(", ")
93 def handle_get(m, params)
95 unless @@items.has_key?(key)
96 m.reply "no such config key #{key}"
99 if @@items[key].type == :array
100 value = self[key].join(", ")
101 elsif @@items[key].type == :password && !m.private
104 m.reply "#{key}: #{value}"
107 def handle_desc(m, params)
109 unless @@items.has_key?(key)
110 m.reply "no such config key #{key}"
112 m.reply "#{key}: #{@@items[key].desc}"
115 def handle_unset(m, params)
117 unless @@items.has_key?(key)
118 m.reply "no such config key #{key}"
121 handle_get(m, params)
124 def handle_set(m, params)
126 value = params[:value].to_s
127 unless @@items.has_key?(key)
128 m.reply "no such config key #{key}"
131 puts "item type is #{item.type}"
138 @config[key] = value.to_i
140 @config[key] = value.to_f
142 @config[key] = value.split(/,\s*/)
150 unless item.values.include?(value)
151 m.reply "invalid value #{value}, allowed values are: " + item.values.join(", ")
156 puts "ACK, unsupported type #{item.type}"
159 item.on_change(@config[key])
163 # bot:: parent bot class
164 # create a new config hash from #{botclass}/conf.rbot
167 @config = Hash.new(false)
169 # respond to config messages, to provide runtime configuration
178 @handler = MessageMapper.new(self)
179 @handler.map 'config list :module', :action => 'handle_list',
180 :defaults => {:module => false}
181 @handler.map 'config get :key', :action => 'handle_get'
182 @handler.map 'config desc :key', :action => 'handle_desc'
183 @handler.map 'config describe :key', :action => 'handle_desc'
184 @handler.map 'config set :key *value', :action => 'handle_set'
185 @handler.map 'config unset :key', :action => 'handle_unset'
188 # have this class persist key/values in hash using yaml as it kinda
190 # have other users of the class describe config to it on init, like:
191 # @config.add(:key => 'server.name', :type => 'string',
192 # :default => 'localhost', :restart => true,
193 # :help => 'irc server to connect to')
194 # that way the config module doesn't have to know about all the other
195 # classes but can still provide help and defaults.
196 # Classes don't have to add keys, they can just use config as a
197 # persistent hash, but then they won't be presented by the config
198 # module for runtime display/changes.
199 # (:restart, if true, makes the bot reply to changes with "this change
200 # will take effect after the next restart)
201 # :proc => Proc.new {|newvalue| ...}
202 # (:proc, proc to run on change of setting)
203 # or maybe, @config.add_key(...) do |newvalue| .... end
204 # :validate => /regex/
205 # (operates on received string before conversion)
206 # Special handling for arrays so the config module can be used to
207 # add/remove elements as well as changing the whole thing
208 # Allow config options to list possible valid values (if type is enum,
209 # for example). Then things like the language module can list the
210 # available languages for choosing.
212 if(File.exist?("#{@bot.botclass}/conf.yaml"))
213 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
214 @config.update(newconfig)
217 wiz = BotConfigWizard.new(@bot)
218 newconfig = wiz.run(@config)
219 @config.update(newconfig)
223 # write current configuration to #{botclass}/conf.rbot
225 Dir.mkdir("#{@bot.botclass}") if(!File.exist?("#{@bot.botclass}"))
226 File.open("#{@bot.botclass}/conf.yaml", "w") do |file|
227 file.puts @config.to_yaml
236 # I don't see a nice way to avoid the first start wizard knowing way too
237 # much about other modules etc, because it runs early and stuff it
238 # configures is used to initialise the other modules...
239 # To minimise this we'll do as little as possible and leave the rest to
240 # online modification
241 class BotConfigWizard
243 # TODO things to configure..
244 # config directory (botclass) - people don't realise they should set
245 # this. The default... isn't good.
246 # users? - default *!*@* to 10
247 # levels? - need a way to specify a default level, methinks, for
248 # unconfigured items.
254 :question => "What server should the bot connect to?",
255 :key => "server.name",
259 :question => "What port should the bot connect to?",
260 :key => "server.port",
264 :question => "Does this IRC server require a password for access? Leave blank if not.",
265 :key => "server.password",
269 :question => "Would you like rbot to bind to a specific local host or IP? Leave blank if not.",
270 :key => "server.bindhost",
274 :question => "What IRC nickname should the bot attempt to use?",
279 :question => "What local user should the bot appear to be?",
284 :question => "What channels should the bot always join at startup? List multiple channels using commas to separate. If a channel requires a password, use a space after the channel name. e.g: '#chan1, #chan2, #secretchan secritpass, #chan3'",
285 :prompt => "Channels",
286 :key => "irc.join_channels",
290 :question => "Which language file should the bot use?",
291 :key => "core.language",
293 :items => Dir.new(Config::DATADIR + "/languages").collect {|f|
294 f =~ /\.lang$/ ? f.gsub(/\.lang$/, "") : nil
298 :question => "Enter your password for maxing your auth with the bot (used to associate new hostmasks with your owner-status etc)",
299 :key => "auth.password",
306 config = defaults.clone
307 puts "First time rbot configuration wizard"
308 puts "===================================="
309 puts "This is the first time you have run rbot with a config directory of:"
311 puts "This wizard will ask you a few questions to get you started."
312 puts "The rest of rbot's configuration can be manipulated via IRC once"
313 puts "rbot is connected and you are auth'd."
314 puts "-----------------------------------"
316 @questions.each do |q|
321 puts "valid values are: " + q[:items].join(", ")
323 if (defaults.has_key?(key))
324 print q[:key] + " [#{defaults[key]}]: "
326 print q[:key] + " []: "
328 response = STDIN.gets
330 response = defaults[key] if response == "" && defaults.has_key?(key)
334 raise "value '#{response}' is not a number" unless (response.class == Fixnum || response =~ /^\d+$/)
335 response = response.to_i
338 raise "selected value '#{response}' is not one of the valid values" unless q[:items].include?(response)
340 config[key] = response
341 puts "configured #{key} => #{config[key]}"
342 puts "-----------------------------------"
343 rescue RuntimeError => e