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