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