5 class ConfigModule < CoreBotModule
\r
7 def handle_list(m, params)
\r
10 @bot.config.items.each_key do |key|
\r
11 mod, name = key.to_s.split('.')
\r
12 next unless mod == params[:module]
\r
13 modules.push key unless modules.include?(name)
\r
16 m.reply "no such module #{params[:module]}"
\r
18 m.reply modules.join(", ")
\r
21 @bot.configitems.each_key do |key|
\r
22 name = key.to_s.split('.').first
\r
23 modules.push name unless modules.include?(name)
\r
25 m.reply "modules: " + modules.join(", ")
\r
29 def handle_get(m, params)
\r
30 key = params[:key].to_s.intern
\r
31 unless @bot.config.items.has_key?(key)
\r
32 m.reply "no such config key #{key}"
\r
35 value = @bot.config.items[key].to_s
\r
36 m.reply "#{key}: #{value}"
\r
39 def handle_desc(m, params)
\r
40 key = params[:key].to_s.intern
\r
41 unless @bot.config.items.has_key?(key)
\r
42 m.reply "no such config key #{key}"
\r
44 puts @bot.config.items[key].inspect
\r
45 m.reply "#{key}: #{@bot.config.items[key].desc}"
\r
48 def handle_unset(m, params)
\r
49 key = params[:key].to_s.intern
\r
50 unless @bot.config.items.has_key?(key)
\r
51 m.reply "no such config key #{key}"
\r
53 @bot.config.items[key].unset
\r
54 handle_get(m, params)
\r
55 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
56 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
59 def handle_set(m, params)
\r
60 key = params[:key].to_s.intern
\r
61 value = params[:value].join(" ")
\r
62 unless @bot.config.items.has_key?(key)
\r
63 m.reply "no such config key #{key}"
\r
67 @bot.config.items[key].set_string(value)
\r
68 rescue ArgumentError => e
\r
69 m.reply "failed to set #{key}: #{e.message}"
\r
72 if @bot.config.items[key].requires_restart
\r
73 m.reply "this config change will take effect on the next restart"
\r
74 elsif @bot.config.items[key].requires_rescan
\r
75 m.reply "this config change will take effect on the next rescan"
\r
81 def handle_add(m, params)
\r
82 key = params[:key].to_s.intern
\r
83 value = params[:value]
\r
84 unless @bot.config.items.has_key?(key)
\r
85 m.reply "no such config key #{key}"
\r
88 unless @bot.config.items[key].class <= BotConfigArrayValue
\r
89 m.reply "config key #{key} is not an array"
\r
93 @bot.config.items[key].add(value)
\r
94 rescue ArgumentError => e
\r
95 m.reply "failed to add #{value} to #{key}: #{e.message}"
\r
98 handle_get(m,{:key => key})
\r
99 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
100 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
103 def handle_rm(m, params)
\r
104 key = params[:key].to_s.intern
\r
105 value = params[:value]
\r
106 unless @bot.config.items.has_key?(key)
\r
107 m.reply "no such config key #{key}"
\r
110 unless @bot.config.items[key].class <= BotConfigArrayValue
\r
111 m.reply "config key #{key} is not an array"
\r
115 @bot.config.items[key].rm(value)
\r
116 rescue ArgumentError => e
\r
117 m.reply "failed to remove #{value} from #{key}: #{e.message}"
\r
120 handle_get(m,{:key => key})
\r
121 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
122 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
125 def bot_save(m, param)
\r
130 def bot_rescan(m, param)
\r
131 m.reply "saving ..."
\r
133 m.reply "rescanning ..."
\r
135 m.reply "done. #{@plugins.status(true)}"
\r
138 def bot_nick(m, param)
\r
139 @bot.nickchg(param[:nick])
\r
142 def bot_status(m, param)
\r
143 m.reply @bot.status
\r
146 # TODO is this one of the methods that disappeared when the bot was moved
\r
147 # from the single-file to the multi-file registry?
\r
149 # def bot_reg_stat(m, param)
\r
150 # m.reply @registry.stat.inspect
\r
153 def bot_version(m, param)
\r
154 m.reply "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
\r
157 def handle_help(m, params)
\r
158 m.reply help(params[:topic])
\r
164 "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"
\r
166 "config list => list configuration modules, config list <module> => list configuration keys for module <module>"
\r
168 "config get <key> => get configuration value for key <key>"
\r
170 "reset key <key> to the default"
\r
172 "config set <key> <value> => set configuration value for key <key> to <value>"
\r
174 "config desc <key> => describe what key <key> configures"
\r
176 "config add <value> to <key> => add value <value> to key <key> if <key> is an array"
\r
178 "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"
\r
180 "no help for config #{topic}"
\r
186 conf = ConfigModule.new
\r
188 conf.map 'config list :module',
\r
189 :action => 'handle_list',
\r
190 :defaults => {:module => false},
\r
191 :auth_path => 'show'
\r
192 # TODO this one is presently a security risk, since the bot
\r
193 # stores the master password in the config. Do we need auth levels
\r
194 # on the BotConfig keys too?
\r
195 conf.map 'config get :key',
\r
196 :action => 'handle_get',
\r
197 :auth_path => 'show'
\r
198 conf.map 'config desc :key',
\r
199 :action => 'handle_desc',
\r
200 :auth_path => 'show'
\r
201 conf.map 'config describe :key',
\r
202 :action => 'handle_desc',
\r
203 :auth_path => 'show'
\r
206 :action => 'bot_save'
\r
208 :action => 'bot_rescan'
\r
209 conf.map "nick :nick",
\r
210 :action => 'bot_nick'
\r
212 :action => 'bot_status',
\r
213 :auth_path => 'show::status'
\r
216 # conf.map "registry stats",
\r
217 # :action => 'bot_reg_stat',
\r
218 # :auth_path => '!config::status'
\r
219 conf.map "version",
\r
220 :action => 'bot_version',
\r
221 :auth_path => 'show::status'
\r
223 conf.map 'config set :key *value',
\r
224 :action => 'handle_set',
\r
225 :auth_path => 'edit'
\r
226 conf.map 'config add :value to :key',
\r
227 :action => 'handle_add',
\r
228 :auth_path => 'edit'
\r
229 conf.map 'config rm :value from :key',
\r
230 :action => 'handle_rm',
\r
231 :auth_path => 'edit'
\r
232 conf.map 'config del :value from :key',
\r
233 :action => 'handle_rm',
\r
234 :auth_path => 'edit'
\r
235 conf.map 'config delete :value from :key',
\r
236 :action => 'handle_rm',
\r
237 :auth_path => 'edit'
\r
238 conf.map 'config unset :key',
\r
239 :action => 'handle_unset',
\r
240 :auth_path => 'edit'
\r
241 conf.map 'config reset :key',
\r
242 :action => 'handle_unset',
\r
243 :auth_path => 'edit'
\r
245 conf.map 'config help :topic',
\r
246 :action => 'handle_help',
\r
247 :defaults => {:topic => false},
\r
248 :auth_path => '!help!'
\r
250 conf.default_auth('*', false)
\r
251 conf.default_auth('show::status', true)
\r