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
16 def handle_list(m, params)
\r
19 @bot.config.items.each_key do |key|
\r
20 mod, name = key.to_s.split('.')
\r
21 next unless mod == params[:module]
\r
22 modules.push key unless modules.include?(name)
\r
25 m.reply _("no such module %{module}") % {:module => params[:module]}
\r
27 m.reply modules.join(", ")
\r
30 @bot.config.items.each_key do |key|
\r
31 name = key.to_s.split('.').first
\r
32 modules.push name unless modules.include?(name)
\r
34 m.reply "modules: " + modules.join(", ")
\r
38 def handle_get(m, params)
\r
39 key = params[:key].to_s.intern
\r
40 unless @bot.config.items.has_key?(key)
\r
41 m.reply _("no such config key %{key}") % {:key => key}
\r
44 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
45 value = @bot.config.items[key].to_s
\r
46 m.reply "#{key}: #{value}"
\r
49 def handle_desc(m, params)
\r
50 key = params[:key].to_s.intern
\r
51 unless @bot.config.items.has_key?(key)
\r
52 m.reply _("no such config key %{key}") % {:key => key}
\r
54 m.reply "#{key}: #{@bot.config.items[key].desc}"
\r
57 def handle_unset(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 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
63 @bot.config.items[key].unset
\r
64 handle_get(m, params)
\r
65 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
66 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
69 def handle_set(m, params)
\r
70 key = params[:key].to_s.intern
\r
71 value = params[:value].join(" ")
\r
72 unless @bot.config.items.has_key?(key)
\r
73 m.reply _("no such config key %{key}") % {:key => key} unless params[:silent]
\r
76 return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
78 @bot.config.items[key].set_string(value)
\r
79 rescue ArgumentError => e
\r
80 m.reply _("failed to set %{key}: %{error}") % {:key => key, :error => e.message} unless params[:silent]
\r
83 if @bot.config.items[key].requires_restart
\r
84 m.reply _("this config change will take effect on the next restart") unless params[:silent]
\r
86 elsif @bot.config.items[key].requires_rescan
\r
87 m.reply _("this config change will take effect on the next rescan") unless params[:silent]
\r
90 m.okay unless params[:silent]
\r
95 def handle_add(m, params)
\r
96 key = params[:key].to_s.intern
\r
97 value = params[:value]
\r
98 unless @bot.config.items.has_key?(key)
\r
99 m.reply _("no such config key %{key}") % {:key => key}
\r
102 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
103 m.reply _("config key %{key} is not an array") % {:key => key}
\r
106 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
108 @bot.config.items[key].add(value)
\r
109 rescue ArgumentError => e
\r
110 m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
\r
113 handle_get(m,{:key => key})
\r
114 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
115 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
118 def handle_rm(m, params)
\r
119 key = params[:key].to_s.intern
\r
120 value = params[:value]
\r
121 unless @bot.config.items.has_key?(key)
\r
122 m.reply _("no such config key %{key}") % {:key => key}
\r
125 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
126 m.reply _("config key %{key} is not an array") % {:key => key}
\r
129 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
131 @bot.config.items[key].rm(value)
\r
132 rescue ArgumentError => e
\r
133 m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
\r
136 handle_get(m,{:key => key})
\r
137 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
138 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
141 def bot_save(m, param)
\r
146 def bot_rescan(m, param)
\r
147 m.reply _("saving ...")
\r
149 m.reply _("rescanning ...")
\r
151 m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}
\r
154 def bot_nick(m, param)
\r
155 @bot.nickchg(param[:nick])
\r
158 def bot_status(m, param)
\r
159 m.reply @bot.status
\r
162 # TODO is this one of the methods that disappeared when the bot was moved
\r
163 # from the single-file to the multi-file registry?
\r
165 # def bot_reg_stat(m, param)
\r
166 # m.reply @registry.stat.inspect
\r
169 def bot_version(m, param)
\r
170 m.reply _("I'm a v. %{version} rubybot, (c) Tom Gilbert and the rbot development team - http://linuxbrit.co.uk/rbot/") % {:version => $version}
\r
173 def handle_help(m, params)
\r
174 m.reply help(params[:topic])
\r
177 def help(plugin, topic="")
\r
182 _("config-related tasks: config topics, save, rescan")
\r
184 _("config list => list configuration modules, config list <module> => list configuration keys for module <module>")
\r
186 _("config get <key> => get configuration value for key <key>")
\r
188 _("reset key <key> to the default")
\r
190 _("config set <key> <value> => set configuration value for key <key> to <value>")
\r
192 _("config desc <key> => describe what key <key> configures")
\r
194 _("config add <value> to <key> => add value <value> to key <key> if <key> is an array")
\r
196 _("config rm <value> from <key> => remove value <value> from key <key> if <key> is an array")
\r
198 _("config module - bot configuration. usage: list, desc, get, set, unset, add, rm")
\r
200 # "no help for config #{topic}"
\r
203 _("save => save current dynamic data and configuration")
\r
205 _("rescan => reload modules and static facts")
\r
207 _("config-related tasks: config, save, rescan")
\r
213 conf = ConfigModule.new
\r
215 conf.map 'config list :module',
\r
216 :action => 'handle_list',
\r
217 :defaults => {:module => false},
\r
218 :auth_path => 'show'
\r
219 # TODO this one is presently a security risk, since the bot
\r
220 # stores the master password in the config. Do we need auth levels
\r
221 # on the BotConfig keys too?
\r
222 conf.map 'config get :key',
\r
223 :action => 'handle_get',
\r
224 :auth_path => 'show'
\r
225 conf.map 'config desc :key',
\r
226 :action => 'handle_desc',
\r
227 :auth_path => 'show'
\r
228 conf.map 'config describe :key',
\r
229 :action => 'handle_desc',
\r
230 :auth_path => 'show'
\r
233 :action => 'bot_save'
\r
235 :action => 'bot_rescan'
\r
236 conf.map "nick :nick",
\r
237 :action => 'bot_nick'
\r
239 :action => 'bot_status',
\r
240 :auth_path => 'show::status'
\r
243 # conf.map "registry stats",
\r
244 # :action => 'bot_reg_stat',
\r
245 # :auth_path => '!config::status'
\r
246 conf.map "version",
\r
247 :action => 'bot_version',
\r
248 :auth_path => 'show::status'
\r
250 conf.map 'config set :key *value',
\r
251 :action => 'handle_set',
\r
252 :auth_path => 'edit'
\r
253 conf.map 'config add :value to :key',
\r
254 :action => 'handle_add',
\r
255 :auth_path => 'edit'
\r
256 conf.map 'config rm :value from :key',
\r
257 :action => 'handle_rm',
\r
258 :auth_path => 'edit'
\r
259 conf.map 'config del :value from :key',
\r
260 :action => 'handle_rm',
\r
261 :auth_path => 'edit'
\r
262 conf.map 'config delete :value from :key',
\r
263 :action => 'handle_rm',
\r
264 :auth_path => 'edit'
\r
265 conf.map 'config unset :key',
\r
266 :action => 'handle_unset',
\r
267 :auth_path => 'edit'
\r
268 conf.map 'config reset :key',
\r
269 :action => 'handle_unset',
\r
270 :auth_path => 'edit'
\r
272 conf.map 'config help :topic',
\r
273 :action => 'handle_help',
\r
274 :defaults => {:topic => false},
\r
275 :auth_path => '!help!'
\r
277 conf.default_auth('*', false)
\r
278 conf.default_auth('show::status', true)
\r
279 # TODO these shouldn't be set here, we need a way to let the default
\r
280 # permission be specified together with the BotConfigValue
\r
281 conf.default_auth('key', true)
\r
282 conf.default_auth('key::auth::password', false)
\r