4 # :title: rbot config management from IRC
\r
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
\r
7 # Copyright:: (C) 2006,2007 Giuseppe Bilotta
\r
10 class ConfigModule < CoreBotModule
\r
13 _("I'm a v. %{version} rubybot%{copyright}%{url}") % {
\r
14 :version => $version,
\r
15 :copyright => ", #{Irc::Bot::COPYRIGHT_NOTICE}",
\r
16 :url => " - #{Irc::Bot::SOURCE_URL}"
\r
24 def handle_list(m, params)
\r
27 @bot.config.items.each_key do |key|
\r
28 mod, name = key.to_s.split('.')
\r
29 next unless mod == params[:module]
\r
30 modules.push key unless modules.include?(name)
\r
33 m.reply _("no such module %{module}") % {:module => params[:module]}
\r
35 m.reply modules.join(", ")
\r
38 @bot.config.items.each_key do |key|
\r
39 name = key.to_s.split('.').first
\r
40 modules.push name unless modules.include?(name)
\r
42 m.reply "modules: " + modules.join(", ")
\r
46 def handle_get(m, params)
\r
47 key = params[:key].to_s.intern
\r
48 unless @bot.config.items.has_key?(key)
\r
49 m.reply _("no such config key %{key}") % {:key => key}
\r
52 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
53 value = @bot.config.items[key].to_s
\r
54 m.reply "#{key}: #{value}"
\r
57 def handle_desc(m, params)
\r
58 key = params[:key].to_s.intern
\r
59 unless @bot.config.items.has_key?(key)
\r
60 m.reply _("no such config key %{key}") % {:key => key}
\r
62 m.reply "#{key}: #{@bot.config.items[key].desc}"
\r
65 def handle_unset(m, params)
\r
66 key = params[:key].to_s.intern
\r
67 unless @bot.config.items.has_key?(key)
\r
68 m.reply _("no such config key %{key}") % {:key => key}
\r
70 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
71 @bot.config.items[key].unset
\r
72 handle_get(m, params)
\r
73 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
74 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
77 def handle_set(m, params)
\r
78 key = params[:key].to_s.intern
\r
79 value = params[:value].join(" ")
\r
80 unless @bot.config.items.has_key?(key)
\r
81 m.reply _("no such config key %{key}") % {:key => key} unless params[:silent]
\r
84 return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
86 @bot.config.items[key].set_string(value)
\r
87 rescue ArgumentError => e
\r
88 m.reply _("failed to set %{key}: %{error}") % {:key => key, :error => e.message} unless params[:silent]
\r
91 if @bot.config.items[key].requires_restart
\r
92 m.reply _("this config change will take effect on the next restart") unless params[:silent]
\r
94 elsif @bot.config.items[key].requires_rescan
\r
95 m.reply _("this config change will take effect on the next rescan") unless params[:silent]
\r
98 m.okay unless params[:silent]
\r
103 def handle_add(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}") % {:key => key}
\r
110 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
111 m.reply _("config key %{key} is not an array") % {:key => key}
\r
114 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
116 @bot.config.items[key].add(value)
\r
117 rescue ArgumentError => e
\r
118 m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
\r
121 handle_get(m,{:key => key})
\r
122 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
123 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
126 def handle_rm(m, params)
\r
127 key = params[:key].to_s.intern
\r
128 value = params[:value]
\r
129 unless @bot.config.items.has_key?(key)
\r
130 m.reply _("no such config key %{key}") % {:key => key}
\r
133 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
134 m.reply _("config key %{key} is not an array") % {:key => key}
\r
137 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
139 @bot.config.items[key].rm(value)
\r
140 rescue ArgumentError => e
\r
141 m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
\r
144 handle_get(m,{:key => key})
\r
145 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
146 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
149 def bot_save(m, param)
\r
154 def bot_rescan(m, param)
\r
155 m.reply _("saving ...")
\r
157 m.reply _("rescanning ...")
\r
159 m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}
\r
162 def bot_nick(m, param)
\r
163 @bot.nickchg(param[:nick])
\r
166 def bot_status(m, param)
\r
167 m.reply @bot.status
\r
170 # TODO is this one of the methods that disappeared when the bot was moved
\r
171 # from the single-file to the multi-file registry?
\r
173 # def bot_reg_stat(m, param)
\r
174 # m.reply @registry.stat.inspect
\r
177 def bot_version(m, param)
\r
178 m.reply version_string
\r
181 def handle_help(m, params)
\r
182 m.reply help(params[:topic])
\r
185 def help(plugin, topic="")
\r
190 _("config-related tasks: config topics, save, rescan")
\r
192 _("config list => list configuration modules, config list <module> => list configuration keys for module <module>")
\r
194 _("config get <key> => get configuration value for key <key>")
\r
196 _("reset key <key> to the default")
\r
198 _("config set <key> <value> => set configuration value for key <key> to <value>")
\r
200 _("config desc <key> => describe what key <key> configures")
\r
202 _("config add <value> to <key> => add value <value> to key <key> if <key> is an array")
\r
204 _("config rm <value> from <key> => remove value <value> from key <key> if <key> is an array")
\r
206 _("config module - bot configuration. usage: list, desc, get, set, unset, add, rm")
\r
208 # "no help for config #{topic}"
\r
211 _("save => save current dynamic data and configuration")
\r
213 _("rescan => reload modules and static facts")
\r
215 _("version => describes software version")
\r
217 _("config-related tasks: config, save, rescan, version")
\r
223 conf = ConfigModule.new
\r
225 conf.map 'config list :module',
\r
226 :action => 'handle_list',
\r
227 :defaults => {:module => false},
\r
228 :auth_path => 'show'
\r
229 # TODO this one is presently a security risk, since the bot
\r
230 # stores the master password in the config. Do we need auth levels
\r
231 # on the BotConfig keys too?
\r
232 conf.map 'config get :key',
\r
233 :action => 'handle_get',
\r
234 :auth_path => 'show'
\r
235 conf.map 'config desc :key',
\r
236 :action => 'handle_desc',
\r
237 :auth_path => 'show'
\r
238 conf.map 'config describe :key',
\r
239 :action => 'handle_desc',
\r
240 :auth_path => 'show'
\r
243 :action => 'bot_save'
\r
245 :action => 'bot_rescan'
\r
246 conf.map "nick :nick",
\r
247 :action => 'bot_nick'
\r
249 :action => 'bot_status',
\r
250 :auth_path => 'show::status'
\r
253 # conf.map "registry stats",
\r
254 # :action => 'bot_reg_stat',
\r
255 # :auth_path => '!config::status'
\r
256 conf.map "version",
\r
257 :action => 'bot_version',
\r
258 :auth_path => 'show::status'
\r
260 conf.map 'config set :key *value',
\r
261 :action => 'handle_set',
\r
262 :auth_path => 'edit'
\r
263 conf.map 'config add :value to :key',
\r
264 :action => 'handle_add',
\r
265 :auth_path => 'edit'
\r
266 conf.map 'config rm :value from :key',
\r
267 :action => 'handle_rm',
\r
268 :auth_path => 'edit'
\r
269 conf.map 'config del :value from :key',
\r
270 :action => 'handle_rm',
\r
271 :auth_path => 'edit'
\r
272 conf.map 'config delete :value from :key',
\r
273 :action => 'handle_rm',
\r
274 :auth_path => 'edit'
\r
275 conf.map 'config unset :key',
\r
276 :action => 'handle_unset',
\r
277 :auth_path => 'edit'
\r
278 conf.map 'config reset :key',
\r
279 :action => 'handle_unset',
\r
280 :auth_path => 'edit'
\r
282 conf.map 'config help :topic',
\r
283 :action => 'handle_help',
\r
284 :defaults => {:topic => false},
\r
285 :auth_path => '!help!'
\r
287 conf.default_auth('*', false)
\r
288 conf.default_auth('show::status', true)
\r
289 # TODO these shouldn't be set here, we need a way to let the default
\r
290 # permission be specified together with the BotConfigValue
\r
291 conf.default_auth('key', true)
\r
292 conf.default_auth('key::auth::password', false)
\r