]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/config.rb
e93af811e51b00129db9bc9862bf0b58d09ed155
[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
38       # TODO
39       # have this class persist key/values in hash using yaml as it kinda
40       # already does.
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.
62       
63       if(File.exist?("#{@bot.botclass}/conf.yaml"))
64         newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
65         @config.update(newconfig)
66       else
67         # first-run wizard!
68         wiz = BotConfigWizard.new(@bot)
69         newconfig = wiz.run(@config)
70         @config.update(newconfig)
71       end
72     end
73
74     # write current configuration to #{botclass}/conf.rbot
75     def save
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
79       end
80     end
81   end
82
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
87   # online modification
88   class BotConfigWizard
89
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
95     # unconfigured items.
96     #
97     def initialize(bot)
98       @bot = bot
99       @questions = [
100         {
101           :question => "What server should the bot connect to?",
102           :prompt => "Hostname",
103           :key => "server.name",
104           :type => :string,
105         },
106         {
107           :question => "What port should the bot connect to?",
108           :prompt => "Port",
109           :key => "server.port",
110           :type => :number,
111         },
112         {
113           :question => "Does this IRC server require a password for access? Leave blank if not.",
114           :prompt => "Password",
115           :key => "server.password",
116           :type => :password,
117         },
118         {
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",
122           :type => :string,
123         },
124         {
125           :question => "What IRC nickname should the bot attempt to use?",
126           :prompt => "Nick",
127           :key => "irc.nick",
128           :type => :string,
129         },
130         {
131           :question => "What local user should the bot appear to be?",
132           :prompt => "User",
133           :key => "irc.user",
134           :type => :string,
135         },
136         {
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",
140           :type => :string,
141         },
142         {
143           :question => "Which language file should the bot use?",
144           :prompt => "Language",
145           :key => "core.language",
146           :type => :enum,
147           :items => Dir.new(Config::DATADIR + "/languages").collect {|f|
148             f =~ /\.lang$/ ? f.gsub(/\.lang$/, "") : nil
149           }.compact
150         },
151         {
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",
155           :type => :password,
156         },
157       ]
158     end
159     
160     def run(defaults)
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:"
165       puts @bot.botclass
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 "-----------------------------------"
170
171       @questions.each do |q|
172         puts q[:question]
173         begin
174           key = q[:key]
175           if q[:type] == :enum
176             puts "valid values are: " + q[:items].join(", ")
177           end
178           if (defaults.has_key?(key))
179             print q[:prompt] + " [#{defaults[key]}]: "
180           else
181             print q[:prompt] + " []: "
182           end
183           response = STDIN.gets
184           response.chop!
185           response = defaults[key] if response == "" && defaults.has_key?(key)
186           case q[:type]
187             when :string
188             when :number
189               raise "value '#{response}' is not a number" unless (response.class == Fixnum || response =~ /^\d+$/)
190               response = response.to_i
191             when :password
192             when :enum
193               raise "selected value '#{response}' is not one of the valid values" unless q[:items].include?(response)
194           end
195           config[key] = response
196           puts "configured #{key} => #{config[key]}"
197           puts "-----------------------------------"
198         rescue RuntimeError => e
199           puts e.message
200           retry
201         end
202       end
203       return config
204     end
205   end
206 end