]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/config.rb
Fri Jul 29 13:07:56 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / lib / rbot / config.rb
1 module Irc
2
3   require 'yaml'
4
5   # container for bot configuration
6   class BotConfig
7
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)
12     end
13
14     # bot:: parent bot class
15     # create a new config hash from #{botclass}/conf.rbot
16     def initialize(bot)
17       @bot = bot
18       # some defaults
19       @config = Hash.new(false)
20       
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
42
43       # TODO
44       # have this class persist key/values in hash using yaml as it kinda
45       # already does.
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.
67       
68       if(File.exist?("#{@bot.botclass}/conf.yaml"))
69         newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
70         @config.update(newconfig)
71       else
72         # first-run wizard!
73         wiz = BotConfigWizard.new(@bot)
74         newconfig = wiz.run(@config)
75         @config.update(newconfig)
76       end
77     end
78
79     # write current configuration to #{botclass}/conf.rbot
80     def save
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
84       end
85     end
86   end
87
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
92   # online modification
93   class BotConfigWizard
94
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.
101     #
102     def initialize(bot)
103       @bot = bot
104       @questions = [
105         {
106           :question => "What server should the bot connect to?",
107           :prompt => "Hostname",
108           :key => "server.name",
109           :type => :string,
110         },
111         {
112           :question => "What port should the bot connect to?",
113           :prompt => "Port",
114           :key => "server.port",
115           :type => :number,
116         },
117         {
118           :question => "Does this IRC server require a password for access? Leave blank if not.",
119           :prompt => "Password",
120           :key => "server.password",
121           :type => :password,
122         },
123         {
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",
127           :type => :string,
128         },
129         {
130           :question => "What IRC nickname should the bot attempt to use?",
131           :prompt => "Nick",
132           :key => "irc.nick",
133           :type => :string,
134         },
135         {
136           :question => "What local user should the bot appear to be?",
137           :prompt => "User",
138           :key => "irc.user",
139           :type => :string,
140         },
141         {
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",
145           :type => :string,
146         },
147         {
148           :question => "Which language file should the bot use?",
149           :prompt => "Language",
150           :key => "core.language",
151           :type => :enum,
152           :items => Dir.new(Config::DATADIR + "/languages").collect {|f|
153             f =~ /\.lang$/ ? f.gsub(/\.lang$/, "") : nil
154           }.compact
155         },
156         {
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",
160           :type => :password,
161         },
162       ]
163     end
164     
165     def run(defaults)
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:"
170       puts @bot.botclass
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 "-----------------------------------"
175
176       @questions.each do |q|
177         puts q[:question]
178         begin
179           key = q[:key]
180           if q[:type] == :enum
181             puts "valid values are: " + q[:items].join(", ")
182           end
183           if (defaults.has_key?(key))
184             print q[:prompt] + " [#{defaults[key]}]: "
185           else
186             print q[:prompt] + " []: "
187           end
188           response = STDIN.gets
189           response.chop!
190           response = defaults[key] if response == "" && defaults.has_key?(key)
191           case q[:type]
192             when :string
193             when :number
194               raise "value '#{response}' is not a number" unless (response.class == Fixnum || response =~ /^\d+$/)
195               response = response.to_i
196             when :password
197             when :enum
198               raise "selected value '#{response}' is not one of the valid values" unless q[:items].include?(response)
199           end
200           config[key] = response
201           puts "configured #{key} => #{config[key]}"
202           puts "-----------------------------------"
203         rescue RuntimeError => e
204           puts e.message
205           retry
206         end
207       end
208       return config
209     end
210   end
211 end