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['irc.nick'] = "rbot"
26 @config['irc.user'] = "rbot"
27 @config['irc.join_channels'] = ""
28 @config['core.language'] = "english"
29 @config['core.save_every'] = 60
30 @config['keyword.listen'] = false
31 @config['auth.password'] = ""
32 @config['server.sendq_delay'] = 2.0
33 @config['server.sendq_burst'] = 4
34 @config['keyword.address'] = true
35 @config['keyword.listen'] = false
38 # have this class persist key/values in hash using yaml as it kinda
40 # have other users of the class describe config to it on init, like:
41 # @config.add(:key => 'server.name', :type => 'string',
42 # :default => 'localhost', :restart => true,
43 # :help => 'irc server to connect to')
44 # that way the config module doesn't have to know about all the other
45 # classes but can still provide help and defaults.
46 # Classes don't have to add keys, they can just use config as a
47 # persistent hash, but then they won't be presented by the config
48 # module for runtime display/changes.
49 # (:restart, if true, makes the bot reply to changes with "this change
50 # will take effect after the next restart)
51 # :proc => Proc.new {|newvalue| ...}
52 # (:proc, proc to run on change of setting)
53 # or maybe, @config.add_key(...) do |newvalue| .... end
54 # :validate => /regex/
55 # (operates on received string before conversion)
56 # Special handling for arrays so the config module can be used to
57 # add/remove elements as well as changing the whole thing
58 # Allow config options to list possible valid values (if type is enum,
59 # for example). Then things like the language module can list the
60 # available languages for choosing.
62 if(File.exist?("#{@bot.botclass}/conf.yaml"))
63 newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
64 @config.update(newconfig)
67 wiz = BotConfigWizard.new(@bot)
68 newconfig = wiz.run(@config)
69 @config.update(newconfig)
73 # write current configuration to #{botclass}/conf.rbot
75 Dir.mkdir("#{@bot.botclass}") if(!File.exist?("#{@bot.botclass}"))
76 File.open("#{@bot.botclass}/conf.yaml", "w") do |file|
77 file.puts @config.to_yaml
82 # I don't see a nice way to avoid the first start wizard knowing way too
83 # much about other modules etc, because it runs early and stuff it
84 # configures is used to initialise the other modules...
85 # To minimise this we'll do as little as possible and leave the rest to
89 # TODO things to configure..
90 # config directory (botclass) - people don't realise they should set
91 # this. The default... isn't good.
92 # users? - default *!*@* to 10
93 # levels? - need a way to specify a default level, methinks, for
100 :question => "What server should the bot connect to?",
101 :prompt => "Hostname",
102 :key => "server.name",
106 :question => "What port should the bot connect to?",
108 :key => "server.port",
112 :question => "Does this IRC server require a password for access? Leave blank if not.",
113 :prompt => "Password",
114 :key => "server.password",
118 :question => "Would you like rbot to bind to a specific local host or IP? Leave blank if not.",
119 :prompt => "Local bind",
120 :key => "server.bindhost",
124 :question => "What IRC nickname should the bot attempt to use?",
130 :question => "What local user should the bot appear to be?",
136 :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'",
137 :prompt => "Channels",
138 :key => "irc.join_channels",
142 :question => "Which language file should the bot use?",
143 :prompt => "Language",
144 :key => "core.language",
146 :items => Dir.new(@bot.datadir + "/languages").collect {|f|
147 f =~ /\.lang$/ ? f.gsub(/\.lang$/, "") : nil
151 :question => "Enter your password for maxing your auth with the bot (used to associate new hostmasks with your owner-status etc)",
152 :prompt => "Password",
153 :key => "auth.password",
160 config = defaults.clone
161 puts "First time rbot configuration wizard"
162 puts "===================================="
163 puts "This is the first time you have run rbot with a config directory of:"
165 puts "This wizard will ask you a few questions to get you started."
166 puts "The rest of rbot's configuration can be manipulated via IRC once"
167 puts "rbot is connected and you are auth'd."
168 puts "-----------------------------------"
170 @questions.each do |q|
175 puts "valid values are: " + q[:items].join(", ")
177 if (defaults.has_key?(key))
178 print q[:prompt] + " [#{defaults[key]}]: "
180 print q[:prompt] + " []: "
182 response = STDIN.gets
184 response = defaults[key] if response == "" && defaults.has_key?(key)
188 raise "value '#{response}' is not a number" unless (response.class == Fixnum || response =~ /^\d+$/)
189 response = response.to_i
192 raise "selected value '#{response}' is not one of the valid values" unless q[:items].include?(response)
194 config[key] = response
195 puts "configured #{key} => #{config[key]}"
196 puts "-----------------------------------"
197 rescue RuntimeError => e