5 class ConfigModule < CoreBotModule
\r
11 def handle_list(m, params)
\r
14 @bot.config.items.each_key do |key|
\r
15 mod, name = key.to_s.split('.')
\r
16 next unless mod == params[:module]
\r
17 modules.push key unless modules.include?(name)
\r
20 m.reply "no such module #{params[:module]}"
\r
22 m.reply modules.join(", ")
\r
25 @bot.config.items.each_key do |key|
\r
26 name = key.to_s.split('.').first
\r
27 modules.push name unless modules.include?(name)
\r
29 m.reply "modules: " + modules.join(", ")
\r
33 def handle_get(m, params)
\r
34 key = params[:key].to_s.intern
\r
35 unless @bot.config.items.has_key?(key)
\r
36 m.reply "no such config key #{key}"
\r
39 value = @bot.config.items[key].to_s
\r
40 m.reply "#{key}: #{value}"
\r
43 def handle_desc(m, params)
\r
44 key = params[:key].to_s.intern
\r
45 unless @bot.config.items.has_key?(key)
\r
46 m.reply "no such config key #{key}"
\r
48 puts @bot.config.items[key].inspect
\r
49 m.reply "#{key}: #{@bot.config.items[key].desc}"
\r
52 def handle_unset(m, params)
\r
53 key = params[:key].to_s.intern
\r
54 unless @bot.config.items.has_key?(key)
\r
55 m.reply "no such config key #{key}"
\r
57 @bot.config.items[key].unset
\r
58 handle_get(m, params)
\r
59 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
60 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
63 def handle_set(m, params)
\r
64 key = params[:key].to_s.intern
\r
65 value = params[:value].join(" ")
\r
66 unless @bot.config.items.has_key?(key)
\r
67 m.reply "no such config key #{key}"
\r
71 @bot.config.items[key].set_string(value)
\r
72 rescue ArgumentError => e
\r
73 m.reply "failed to set #{key}: #{e.message}"
\r
76 if @bot.config.items[key].requires_restart
\r
77 m.reply "this config change will take effect on the next restart"
\r
78 elsif @bot.config.items[key].requires_rescan
\r
79 m.reply "this config change will take effect on the next rescan"
\r
85 def handle_add(m, params)
\r
86 key = params[:key].to_s.intern
\r
87 value = params[:value]
\r
88 unless @bot.config.items.has_key?(key)
\r
89 m.reply "no such config key #{key}"
\r
92 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
93 m.reply "config key #{key} is not an array"
\r
97 @bot.config.items[key].add(value)
\r
98 rescue ArgumentError => e
\r
99 m.reply "failed to add #{value} to #{key}: #{e.message}"
\r
102 handle_get(m,{:key => key})
\r
103 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
104 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
107 def handle_rm(m, params)
\r
108 key = params[:key].to_s.intern
\r
109 value = params[:value]
\r
110 unless @bot.config.items.has_key?(key)
\r
111 m.reply "no such config key #{key}"
\r
114 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
115 m.reply "config key #{key} is not an array"
\r
119 @bot.config.items[key].rm(value)
\r
120 rescue ArgumentError => e
\r
121 m.reply "failed to remove #{value} from #{key}: #{e.message}"
\r
124 handle_get(m,{:key => key})
\r
125 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
126 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
129 def bot_save(m, param)
\r
134 def bot_rescan(m, param)
\r
135 m.reply "saving ..."
\r
137 m.reply "rescanning ..."
\r
139 m.reply "done. #{@bot.plugins.status(true)}"
\r
142 def bot_nick(m, param)
\r
143 @bot.nickchg(param[:nick])
\r
146 def bot_status(m, param)
\r
147 m.reply @bot.status
\r
150 # TODO is this one of the methods that disappeared when the bot was moved
\r
151 # from the single-file to the multi-file registry?
\r
153 # def bot_reg_stat(m, param)
\r
154 # m.reply @registry.stat.inspect
\r
157 def bot_version(m, param)
\r
158 m.reply "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
\r
161 def handle_help(m, params)
\r
162 m.reply help(params[:topic])
\r
165 def help(plugin, topic="")
\r
168 "config list => list configuration modules, config list <module> => list configuration keys for module <module>"
\r
170 "config get <key> => get configuration value for key <key>"
\r
172 "reset key <key> to the default"
\r
174 "config set <key> <value> => set configuration value for key <key> to <value>"
\r
176 "config desc <key> => describe what key <key> configures"
\r
178 "config add <value> to <key> => add value <value> to key <key> if <key> is an array"
\r
180 "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"
\r
182 "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"
\r
184 # "no help for config #{topic}"
\r
190 conf = ConfigModule.new
\r
192 conf.map 'config list :module',
\r
193 :action => 'handle_list',
\r
194 :defaults => {:module => false},
\r
195 :auth_path => 'show'
\r
196 # TODO this one is presently a security risk, since the bot
\r
197 # stores the master password in the config. Do we need auth levels
\r
198 # on the BotConfig keys too?
\r
199 conf.map 'config get :key',
\r
200 :action => 'handle_get',
\r
201 :auth_path => 'show'
\r
202 conf.map 'config desc :key',
\r
203 :action => 'handle_desc',
\r
204 :auth_path => 'show'
\r
205 conf.map 'config describe :key',
\r
206 :action => 'handle_desc',
\r
207 :auth_path => 'show'
\r
210 :action => 'bot_save'
\r
212 :action => 'bot_rescan'
\r
213 conf.map "nick :nick",
\r
214 :action => 'bot_nick'
\r
216 :action => 'bot_status',
\r
217 :auth_path => 'show::status'
\r
220 # conf.map "registry stats",
\r
221 # :action => 'bot_reg_stat',
\r
222 # :auth_path => '!config::status'
\r
223 conf.map "version",
\r
224 :action => 'bot_version',
\r
225 :auth_path => 'show::status'
\r
227 conf.map 'config set :key *value',
\r
228 :action => 'handle_set',
\r
229 :auth_path => 'edit'
\r
230 conf.map 'config add :value to :key',
\r
231 :action => 'handle_add',
\r
232 :auth_path => 'edit'
\r
233 conf.map 'config rm :value from :key',
\r
234 :action => 'handle_rm',
\r
235 :auth_path => 'edit'
\r
236 conf.map 'config del :value from :key',
\r
237 :action => 'handle_rm',
\r
238 :auth_path => 'edit'
\r
239 conf.map 'config delete :value from :key',
\r
240 :action => 'handle_rm',
\r
241 :auth_path => 'edit'
\r
242 conf.map 'config unset :key',
\r
243 :action => 'handle_unset',
\r
244 :auth_path => 'edit'
\r
245 conf.map 'config reset :key',
\r
246 :action => 'handle_unset',
\r
247 :auth_path => 'edit'
\r
249 conf.map 'config help :topic',
\r
250 :action => 'handle_help',
\r
251 :defaults => {:topic => false},
\r
252 :auth_path => '!help!'
\r
254 conf.default_auth('*', false)
\r
255 conf.default_auth('show::status', true)
\r