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 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
40 value = @bot.config.items[key].to_s
\r
41 m.reply "#{key}: #{value}"
\r
44 def handle_desc(m, params)
\r
45 key = params[:key].to_s.intern
\r
46 unless @bot.config.items.has_key?(key)
\r
47 m.reply "no such config key #{key}"
\r
49 puts @bot.config.items[key].inspect
\r
50 m.reply "#{key}: #{@bot.config.items[key].desc}"
\r
53 def handle_unset(m, params)
\r
54 key = params[:key].to_s.intern
\r
55 unless @bot.config.items.has_key?(key)
\r
56 m.reply "no such config key #{key}"
\r
58 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
59 @bot.config.items[key].unset
\r
60 handle_get(m, params)
\r
61 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
62 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
65 def handle_set(m, params)
\r
66 key = params[:key].to_s.intern
\r
67 value = params[:value].join(" ")
\r
68 unless @bot.config.items.has_key?(key)
\r
69 m.reply "no such config key #{key}" unless params[:silent]
\r
72 return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
74 @bot.config.items[key].set_string(value)
\r
75 rescue ArgumentError => e
\r
76 m.reply "failed to set #{key}: #{e.message}" unless params[:silent]
\r
79 if @bot.config.items[key].requires_restart
\r
80 m.reply "this config change will take effect on the next restart" unless params[:silent]
\r
82 elsif @bot.config.items[key].requires_rescan
\r
83 m.reply "this config change will take effect on the next rescan" unless params[:silent]
\r
86 m.okay unless params[:silent]
\r
91 def handle_add(m, params)
\r
92 key = params[:key].to_s.intern
\r
93 value = params[:value]
\r
94 unless @bot.config.items.has_key?(key)
\r
95 m.reply "no such config key #{key}"
\r
98 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
99 m.reply "config key #{key} is not an array"
\r
102 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
104 @bot.config.items[key].add(value)
\r
105 rescue ArgumentError => e
\r
106 m.reply "failed to add #{value} to #{key}: #{e.message}"
\r
109 handle_get(m,{:key => key})
\r
110 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
111 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
114 def handle_rm(m, params)
\r
115 key = params[:key].to_s.intern
\r
116 value = params[:value]
\r
117 unless @bot.config.items.has_key?(key)
\r
118 m.reply "no such config key #{key}"
\r
121 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
122 m.reply "config key #{key} is not an array"
\r
125 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
127 @bot.config.items[key].rm(value)
\r
128 rescue ArgumentError => e
\r
129 m.reply "failed to remove #{value} from #{key}: #{e.message}"
\r
132 handle_get(m,{:key => key})
\r
133 m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
\r
134 m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
\r
137 def bot_save(m, param)
\r
142 def bot_rescan(m, param)
\r
143 m.reply "saving ..."
\r
145 m.reply "rescanning ..."
\r
147 m.reply "done. #{@bot.plugins.status(true)}"
\r
150 def bot_nick(m, param)
\r
151 @bot.nickchg(param[:nick])
\r
154 def bot_status(m, param)
\r
155 m.reply @bot.status
\r
158 # TODO is this one of the methods that disappeared when the bot was moved
\r
159 # from the single-file to the multi-file registry?
\r
161 # def bot_reg_stat(m, param)
\r
162 # m.reply @registry.stat.inspect
\r
165 def bot_version(m, param)
\r
166 m.reply "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
\r
169 def handle_help(m, params)
\r
170 m.reply help(params[:topic])
\r
173 def help(plugin, topic="")
\r
176 "config list => list configuration modules, config list <module> => list configuration keys for module <module>"
\r
178 "config get <key> => get configuration value for key <key>"
\r
180 "reset key <key> to the default"
\r
182 "config set <key> <value> => set configuration value for key <key> to <value>"
\r
184 "config desc <key> => describe what key <key> configures"
\r
186 "config add <value> to <key> => add value <value> to key <key> if <key> is an array"
\r
188 "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"
\r
190 "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"
\r
192 # "no help for config #{topic}"
\r
198 conf = ConfigModule.new
\r
200 conf.map 'config list :module',
\r
201 :action => 'handle_list',
\r
202 :defaults => {:module => false},
\r
203 :auth_path => 'show'
\r
204 # TODO this one is presently a security risk, since the bot
\r
205 # stores the master password in the config. Do we need auth levels
\r
206 # on the BotConfig keys too?
\r
207 conf.map 'config get :key',
\r
208 :action => 'handle_get',
\r
209 :auth_path => 'show'
\r
210 conf.map 'config desc :key',
\r
211 :action => 'handle_desc',
\r
212 :auth_path => 'show'
\r
213 conf.map 'config describe :key',
\r
214 :action => 'handle_desc',
\r
215 :auth_path => 'show'
\r
218 :action => 'bot_save'
\r
220 :action => 'bot_rescan'
\r
221 conf.map "nick :nick",
\r
222 :action => 'bot_nick'
\r
224 :action => 'bot_status',
\r
225 :auth_path => 'show::status'
\r
228 # conf.map "registry stats",
\r
229 # :action => 'bot_reg_stat',
\r
230 # :auth_path => '!config::status'
\r
231 conf.map "version",
\r
232 :action => 'bot_version',
\r
233 :auth_path => 'show::status'
\r
235 conf.map 'config set :key *value',
\r
236 :action => 'handle_set',
\r
237 :auth_path => 'edit'
\r
238 conf.map 'config add :value to :key',
\r
239 :action => 'handle_add',
\r
240 :auth_path => 'edit'
\r
241 conf.map 'config rm :value from :key',
\r
242 :action => 'handle_rm',
\r
243 :auth_path => 'edit'
\r
244 conf.map 'config del :value from :key',
\r
245 :action => 'handle_rm',
\r
246 :auth_path => 'edit'
\r
247 conf.map 'config delete :value from :key',
\r
248 :action => 'handle_rm',
\r
249 :auth_path => 'edit'
\r
250 conf.map 'config unset :key',
\r
251 :action => 'handle_unset',
\r
252 :auth_path => 'edit'
\r
253 conf.map 'config reset :key',
\r
254 :action => 'handle_unset',
\r
255 :auth_path => 'edit'
\r
257 conf.map 'config help :topic',
\r
258 :action => 'handle_help',
\r
259 :defaults => {:topic => false},
\r
260 :auth_path => '!help!'
\r
262 conf.default_auth('*', false)
\r
263 conf.default_auth('show::status', true)
\r
264 # TODO these shouldn't be set here, we need a way to let the default
\r
265 # permission be specified together with the BotConfigValue
\r
266 conf.default_auth('key', true)
\r
267 conf.default_auth('key::auth::password', false)
\r