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 puts @bot.config.items[key].inspect
\r
55 m.reply "#{key}: #{@bot.config.items[key].desc}"
\r
58 def handle_unset(m, params)
\r
59 key = params[:key].to_s.intern
\r
60 unless @bot.config.items.has_key?(key)
\r
61 m.reply _("no such config key %{key}") % {:key => key}
\r
63 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
64 @bot.config.items[key].unset
\r
65 handle_get(m, params)
\r
66 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
67 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
70 def handle_set(m, params)
\r
71 key = params[:key].to_s.intern
\r
72 value = params[:value].join(" ")
\r
73 unless @bot.config.items.has_key?(key)
\r
74 m.reply _("no such config key %{key}") % {:key => key} unless params[:silent]
\r
77 return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
79 @bot.config.items[key].set_string(value)
\r
80 rescue ArgumentError => e
\r
81 m.reply _("failed to set %{key}: %{error}") % {:key => key, :error => e.message} unless params[:silent]
\r
84 if @bot.config.items[key].requires_restart
\r
85 m.reply _("this config change will take effect on the next restart") unless params[:silent]
\r
87 elsif @bot.config.items[key].requires_rescan
\r
88 m.reply _("this config change will take effect on the next rescan") unless params[:silent]
\r
91 m.okay unless params[:silent]
\r
96 def handle_add(m, params)
\r
97 key = params[:key].to_s.intern
\r
98 value = params[:value]
\r
99 unless @bot.config.items.has_key?(key)
\r
100 m.reply _("no such config key %{key}") % {:key => key}
\r
103 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
104 m.reply _("config key %{key} is not an array") % {:key => key}
\r
107 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
109 @bot.config.items[key].add(value)
\r
110 rescue ArgumentError => e
\r
111 m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
\r
114 handle_get(m,{:key => key})
\r
115 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
116 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
119 def handle_rm(m, params)
\r
120 key = params[:key].to_s.intern
\r
121 value = params[:value]
\r
122 unless @bot.config.items.has_key?(key)
\r
123 m.reply _("no such config key %{key}") % {:key => key}
\r
126 unless @bot.config.items[key].kind_of?(BotConfigArrayValue)
\r
127 m.reply _("config key %{key} is not an array") % {:key => key}
\r
130 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
\r
132 @bot.config.items[key].rm(value)
\r
133 rescue ArgumentError => e
\r
134 m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
\r
137 handle_get(m,{:key => key})
\r
138 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
\r
139 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
\r
142 def bot_save(m, param)
\r
147 def bot_rescan(m, param)
\r
148 m.reply _("saving ...")
\r
150 m.reply _("rescanning ...")
\r
152 m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}
\r
155 def bot_nick(m, param)
\r
156 @bot.nickchg(param[:nick])
\r
159 def bot_status(m, param)
\r
160 m.reply @bot.status
\r
163 # TODO is this one of the methods that disappeared when the bot was moved
\r
164 # from the single-file to the multi-file registry?
\r
166 # def bot_reg_stat(m, param)
\r
167 # m.reply @registry.stat.inspect
\r
170 def bot_version(m, param)
\r
171 m.reply _("I'm a v. %{version} rubybot, (c) Tom Gilbert and the rbot development team - http://linuxbrit.co.uk/rbot/") % {:version => $version}
\r
174 def handle_help(m, params)
\r
175 m.reply help(params[:topic])
\r
178 def help(plugin, topic="")
\r
183 _("config-related tasks: config topics, save, rescan")
\r
185 _("config list => list configuration modules, config list <module> => list configuration keys for module <module>")
\r
187 _("config get <key> => get configuration value for key <key>")
\r
189 _("reset key <key> to the default")
\r
191 _("config set <key> <value> => set configuration value for key <key> to <value>")
\r
193 _("config desc <key> => describe what key <key> configures")
\r
195 _("config add <value> to <key> => add value <value> to key <key> if <key> is an array")
\r
197 _("config rm <value> from <key> => remove value <value> from key <key> if <key> is an array")
\r
199 _("config module - bot configuration. usage: list, desc, get, set, unset, add, rm")
\r
201 # "no help for config #{topic}"
\r
204 _("save => save current dynamic data and configuration")
\r
206 _("rescan => reload modules and static facts")
\r
208 _("config-related tasks: config, save, rescan")
\r
214 conf = ConfigModule.new
\r
216 conf.map 'config list :module',
\r
217 :action => 'handle_list',
\r
218 :defaults => {:module => false},
\r
219 :auth_path => 'show'
\r
220 # TODO this one is presently a security risk, since the bot
\r
221 # stores the master password in the config. Do we need auth levels
\r
222 # on the BotConfig keys too?
\r
223 conf.map 'config get :key',
\r
224 :action => 'handle_get',
\r
225 :auth_path => 'show'
\r
226 conf.map 'config desc :key',
\r
227 :action => 'handle_desc',
\r
228 :auth_path => 'show'
\r
229 conf.map 'config describe :key',
\r
230 :action => 'handle_desc',
\r
231 :auth_path => 'show'
\r
234 :action => 'bot_save'
\r
236 :action => 'bot_rescan'
\r
237 conf.map "nick :nick",
\r
238 :action => 'bot_nick'
\r
240 :action => 'bot_status',
\r
241 :auth_path => 'show::status'
\r
244 # conf.map "registry stats",
\r
245 # :action => 'bot_reg_stat',
\r
246 # :auth_path => '!config::status'
\r
247 conf.map "version",
\r
248 :action => 'bot_version',
\r
249 :auth_path => 'show::status'
\r
251 conf.map 'config set :key *value',
\r
252 :action => 'handle_set',
\r
253 :auth_path => 'edit'
\r
254 conf.map 'config add :value to :key',
\r
255 :action => 'handle_add',
\r
256 :auth_path => 'edit'
\r
257 conf.map 'config rm :value from :key',
\r
258 :action => 'handle_rm',
\r
259 :auth_path => 'edit'
\r
260 conf.map 'config del :value from :key',
\r
261 :action => 'handle_rm',
\r
262 :auth_path => 'edit'
\r
263 conf.map 'config delete :value from :key',
\r
264 :action => 'handle_rm',
\r
265 :auth_path => 'edit'
\r
266 conf.map 'config unset :key',
\r
267 :action => 'handle_unset',
\r
268 :auth_path => 'edit'
\r
269 conf.map 'config reset :key',
\r
270 :action => 'handle_unset',
\r
271 :auth_path => 'edit'
\r
273 conf.map 'config help :topic',
\r
274 :action => 'handle_help',
\r
275 :defaults => {:topic => false},
\r
276 :auth_path => '!help!'
\r
278 conf.default_auth('*', false)
\r
279 conf.default_auth('show::status', true)
\r
280 # TODO these shouldn't be set here, we need a way to let the default
\r
281 # permission be specified together with the BotConfigValue
\r
282 conf.default_auth('key', true)
\r
283 conf.default_auth('key::auth::password', false)
\r