5 # container for bot configuration
8 # currently we store values in a hash but this could be changed in the
9 # future. We use hash semantics, however.
10 def method_missing(method, *args, &block)
11 return @config.send(method, *args, &block)
14 # bot:: parent bot class
15 # create a new config hash from #{botclass}/conf.rbot
19 @config = Hash.new(false)
21 @config['server.name'] = "localhost"
22 @config['server.port'] = 6667
23 @config['server.password'] = false
24 @config['server.bindhost'] = false
25 @config['server.reconnect_wait'] = 5
26 @config['irc.nick'] = "rbot"
27 @config['irc.user'] = "rbot"
28 @config['irc.join_channels'] = ""
29 @config['core.language'] = "english"
30 @config['core.save_every'] = 60
31 @config['keyword.listen'] = false
32 @config['auth.password'] = ""
33 @config['server.sendq_delay'] = 2.0
34 @config['server.sendq_burst'] = 4
35 @config['keyword.address'] = true
36 @config['keyword.listen'] = false
37 @config['http.proxy'] = false
38 @config['http.proxy_include'] = false
39 @config['http.proxy_exclude'] = false
40 @config['http.proxy_user'] = false
41 @config['http.proxy_pass'] = false
44 # have this class persist key/values in hash using yaml as it kinda
46 # have other users of the class describe config to it on init, like:
47 # @config.add(:key => 'server.name', :type => 'string',
48 # :default => 'localhost', :restart => true,
49 # :help => 'irc server to connect to')
50 # that way the config module doesn't have to know about all the other
51 # classes but can still provide help and defaults.
52 # Classes don't have to add keys, they can just use config as a
53 # persistent hash, but then they won't be presented by the config
54 # module for runtime display/changes.
55 # (:restart, if true, makes the bot reply to changes with "this change
56 # will take effect after the next restart)
57 # :proc => Proc.new {|newvalue| ...}
58 # (:proc, proc to run on change of setting)
59 # or maybe, @config.add_key(...) do |newvalue| .... end
60 # :validate => /regex/
61 # (operates on received string before conversion)
62 # Special handling for arrays so the config module can be used to
63 # add/remove elements as well as changing the whole thing
64 # Allow config options to list possible valid values (if type is enum,
65 # for example). Then things like the language module can list the
66 # available languages for choosing.
68 if(File.exist?("#{@bot.botclass}/conf.yaml"))
69 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
70 @config.update(newconfig)
73 wiz = BotConfigWizard.new(@bot)
74 newconfig = wiz.run(@config)
75 @config.update(newconfig)
79 # write current configuration to #{botclass}/conf.rbot
81 Dir.mkdir("#{@bot.botclass}") if(!File.exist?("#{@bot.botclass}"))
82 File.open("#{@bot.botclass}/conf.yaml", "w") do |file|
83 file.puts @config.to_yaml
88 # I don't see a nice way to avoid the first start wizard knowing way too
89 # much about other modules etc, because it runs early and stuff it
90 # configures is used to initialise the other modules...
91 # To minimise this we'll do as little as possible and leave the rest to
95 # TODO things to configure..
96 # config directory (botclass) - people don't realise they should set
97 # this. The default... isn't good.
98 # users? - default *!*@* to 10
99 # levels? - need a way to specify a default level, methinks, for
100 # unconfigured items.
106 :question => "What server should the bot connect to?",
107 :prompt => "Hostname",
108 :key => "server.name",
112 :question => "What port should the bot connect to?",
114 :key => "server.port",
118 :question => "Does this IRC server require a password for access? Leave blank if not.",
119 :prompt => "Password",
120 :key => "server.password",
124 :question => "Would you like rbot to bind to a specific local host or IP? Leave blank if not.",
125 :prompt => "Local bind",
126 :key => "server.bindhost",
130 :question => "What IRC nickname should the bot attempt to use?",
136 :question => "What local user should the bot appear to be?",
142 :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'",
143 :prompt => "Channels",
144 :key => "irc.join_channels",
148 :question => "Which language file should the bot use?",
149 :prompt => "Language",
150 :key => "core.language",
152 :items => Dir.new(Config::DATADIR + "/languages").collect {|f|
153 f =~ /\.lang$/ ? f.gsub(/\.lang$/, "") : nil
157 :question => "Enter your password for maxing your auth with the bot (used to associate new hostmasks with your owner-status etc)",
158 :prompt => "Password",
159 :key => "auth.password",
166 config = defaults.clone
167 puts "First time rbot configuration wizard"
168 puts "===================================="
169 puts "This is the first time you have run rbot with a config directory of:"
171 puts "This wizard will ask you a few questions to get you started."
172 puts "The rest of rbot's configuration can be manipulated via IRC once"
173 puts "rbot is connected and you are auth'd."
174 puts "-----------------------------------"
176 @questions.each do |q|
181 puts "valid values are: " + q[:items].join(", ")
183 if (defaults.has_key?(key))
184 print q[:prompt] + " [#{defaults[key]}]: "
186 print q[:prompt] + " []: "
188 response = STDIN.gets
190 response = defaults[key] if response == "" && defaults.has_key?(key)
194 raise "value '#{response}' is not a number" unless (response.class == Fixnum || response =~ /^\d+$/)
195 response = response.to_i
198 raise "selected value '#{response}' is not one of the valid values" unless q[:items].include?(response)
200 config[key] = response
201 puts "configured #{key} => #{config[key]}"
202 puts "-----------------------------------"
203 rescue RuntimeError => e