4 # :title: rbot config management from IRC
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 class ConfigModule < CoreBotModule
11 if $version_timestamp.to_i > 0
12 ago = _(" [%{secs} ago]") % {
13 :secs => Utils.secs_to_string(Time.now.to_i - $version_timestamp.to_i)
18 _("I'm a v. %{version}%{ago} rubybot%{copyright}%{url}") % {
21 :copyright => ", #{Irc::Bot::COPYRIGHT_NOTICE}",
22 :url => " - #{Irc::Bot::SOURCE_URL}"
30 def handle_list(m, params)
33 @bot.config.items.each_key do |key|
34 mod, name = key.to_s.split('.')
35 next unless mod == params[:module]
36 modules.push key unless modules.include?(name)
39 m.reply _("no such module %{module}") % {:module => params[:module]}
41 m.reply modules.join(", ")
44 @bot.config.items.each_key do |key|
45 name = key.to_s.split('.').first
46 modules.push name unless modules.include?(name)
48 m.reply "modules: " + modules.join(", ")
52 def handle_get(m, params)
53 key = params[:key].to_s.intern
54 unless @bot.config.items.has_key?(key)
55 m.reply _("no such config key %{key}") % {:key => key}
58 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
59 value = @bot.config.items[key].to_s
60 m.reply "#{key}: #{value}"
63 def handle_desc(m, params)
64 key = params[:key].to_s.intern
65 unless @bot.config.items.has_key?(key)
66 m.reply _("no such config key %{key}") % {:key => key}
68 m.reply "#{key}: #{@bot.config.items[key].desc}"
71 def handle_search(m, params)
72 rx = Regexp.new(params[:rx].to_s, true)
74 @bot.config.items.each do |k, v|
75 cfs << [Bold + k.to_s + Bold, v.desc] if k.to_s.match(rx) or (v.desc.match(rx) rescue false)
78 m.reply _("no config key found matching %{r}") % { :r => params[:rx].to_s}
80 m.reply _("possible keys: %{kl}") % { :kl => cfs.map { |c| c.first}.sort.join(', ') } if cfs.length > 1
81 m.reply cfs.map { |c| c.join(': ') }.join("\n")
85 def handle_unset(m, params)
86 key = params[:key].to_s.intern
87 unless @bot.config.items.has_key?(key)
88 m.reply _("no such config key %{key}") % {:key => key}
90 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
91 @bot.config.items[key].unset
93 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
94 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
97 def handle_set(m, params)
98 key = params[:key].to_s.intern
99 value = params[:value].join(" ")
100 unless @bot.config.items.has_key?(key)
101 m.reply _("no such config key %{key}") % {:key => key} unless params[:silent]
104 return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
106 @bot.config.items[key].set_string(value)
107 rescue ArgumentError => e
108 m.reply _("failed to set %{key}: %{error}") % {:key => key, :error => e.message} unless params[:silent]
111 if @bot.config.items[key].requires_restart
112 m.reply _("this config change will take effect on the next restart") unless params[:silent]
114 elsif @bot.config.items[key].requires_rescan
115 m.reply _("this config change will take effect on the next rescan") unless params[:silent]
118 m.okay unless params[:silent]
123 def handle_add(m, params)
124 key = params[:key].to_s.intern
125 values = params[:value].to_s.split(/,\s+/)
126 unless @bot.config.items.has_key?(key)
127 m.reply _("no such config key %{key}") % {:key => key}
130 unless @bot.config.items[key].kind_of?(Config::ArrayValue)
131 m.reply _("config key %{key} is not an array") % {:key => key}
134 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
135 values.each do |value|
137 @bot.config.items[key].add(value)
138 rescue ArgumentError => e
139 m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
143 handle_get(m,{:key => key})
144 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
145 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
148 def handle_rm(m, params)
149 key = params[:key].to_s.intern
150 values = params[:value].to_s.split(/,\s+/)
151 unless @bot.config.items.has_key?(key)
152 m.reply _("no such config key %{key}") % {:key => key}
155 unless @bot.config.items[key].kind_of?(Config::ArrayValue)
156 m.reply _("config key %{key} is not an array") % {:key => key}
159 return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
160 values.each do |value|
162 @bot.config.items[key].rm(value)
163 rescue ArgumentError => e
164 m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
168 handle_get(m,{:key => key})
169 m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
170 m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
173 def bot_save(m, param)
178 def bot_rescan(m, param)
179 m.reply _("saving ...")
181 m.reply _("rescanning ...")
183 m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}
186 def bot_nick(m, param)
187 @bot.nickchg(param[:nick])
188 @bot.wanted_nick = param[:nick]
191 def bot_status(m, param)
195 # TODO is this one of the methods that disappeared when the bot was moved
196 # from the single-file to the multi-file registry?
198 # def bot_reg_stat(m, param)
199 # m.reply @registry.stat.inspect
202 def bot_version(m, param)
203 m.reply version_string
207 who = m.private? ? "me" : m.target
210 m.ctcp_reply version_string
212 m.ctcp_reply Irc::Bot::SOURCE_URL
216 def handle_help(m, params)
217 m.reply help(params[:topic])
220 def help(plugin, topic="")
225 _("config list => list configuration modules, config list <module> => list configuration keys for module <module>")
227 _("config get <key> => get configuration value for key <key>")
229 _("reset key <key> to the default")
231 _("config set <key> <value> => set configuration value for key <key> to <value>")
233 _("config desc <key> => describe what key <key> configures")
235 _("config add <values> to <key> => add values <values> to key <key> if <key> is an array")
237 _("config rm <value> from <key> => remove value <value> from key <key> if <key> is an array")
239 _("config module - bot configuration. usage: list, desc, get, set, unset, add, rm")
241 # "no help for config #{topic}"
244 _("nick <newnick> => change the bot nick to <newnick>, if possible")
246 _("status => display some information on the bot's status")
248 _("save => save current dynamic data and configuration")
250 _("rescan => reload modules and static facts")
252 _("version => describes software version")
254 _("config-related tasks: config, save, rescan, version, nick, status")
260 conf = ConfigModule.new
262 conf.map 'config list :module',
263 :action => 'handle_list',
264 :defaults => {:module => false},
266 # TODO this one is presently a security risk, since the bot
267 # stores the master password in the config. Do we need auth levels
268 # on the Bot::Config keys too?
269 conf.map 'config get :key',
270 :action => 'handle_get',
272 conf.map 'config desc :key',
273 :action => 'handle_desc',
275 conf.map 'config describe :key',
276 :action => 'handle_desc',
277 :auth_path => 'show::desc!'
278 conf.map 'config search *rx',
279 :action => 'handle_search',
283 :action => 'bot_save'
285 :action => 'bot_rescan'
286 conf.map "nick :nick",
287 :action => 'bot_nick'
289 :action => 'bot_status',
290 :auth_path => 'show::status'
293 # conf.map "registry stats",
294 # :action => 'bot_reg_stat',
295 # :auth_path => '!config::status'
297 :action => 'bot_version',
298 :auth_path => 'show::status'
300 conf.map 'config set :key *value',
301 :action => 'handle_set',
303 conf.map 'config add *value to :key',
304 :action => 'handle_add',
306 conf.map 'config rm *value from :key',
307 :action => 'handle_rm',
309 conf.map 'config remove *value from :key',
310 :action => 'handle_rm',
312 conf.map 'config del *value from :key',
313 :action => 'handle_rm',
315 conf.map 'config delete *value from :key',
316 :action => 'handle_rm',
318 conf.map 'config unset :key',
319 :action => 'handle_unset',
321 conf.map 'config reset :key',
322 :action => 'handle_unset',
325 conf.map 'config help :topic',
326 :action => 'handle_help',
327 :defaults => {:topic => false},
328 :auth_path => '!help!'
330 conf.default_auth('*', false)
331 conf.default_auth('show', true)
332 conf.default_auth('show::get', false)
333 # TODO these shouldn't be set here, we need a way to let the default
334 # permission be specified together with the ConfigValue
335 conf.default_auth('key', true)
336 conf.default_auth('key::auth::password', false)