]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/config.rb
+ @bot.wanted_nick stores the nick wanted by the bot
[user/henk/code/ruby/rbot.git] / lib / rbot / core / config.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: rbot config management from IRC
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7
8 class ConfigModule < CoreBotModule
9
10   def version_string
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)
14       }
15     else
16       ago = ''
17     end
18     _("I'm a v. %{version}%{ago} rubybot%{copyright}%{url}") % {
19       :version => $version,
20       :ago => ago,
21       :copyright => ", #{Irc::Bot::COPYRIGHT_NOTICE}",
22       :url => " - #{Irc::Bot::SOURCE_URL}"
23     }
24   end
25
26   def save
27     @bot.config.save
28   end
29
30   def handle_list(m, params)
31     modules = []
32     if params[:module]
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)
37       end
38       if modules.empty?
39         m.reply _("no such module %{module}") % {:module => params[:module]}
40       else
41         m.reply modules.join(", ")
42       end
43     else
44       @bot.config.items.each_key do |key|
45         name = key.to_s.split('.').first
46         modules.push name unless modules.include?(name)
47       end
48       m.reply "modules: " + modules.join(", ")
49     end
50   end
51
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}
56       return
57     end
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}"
61   end
62
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}
67     end
68     m.reply "#{key}: #{@bot.config.items[key].desc}"
69   end
70
71   def handle_search(m, params)
72     rx = Regexp.new(params[:rx].to_s, true)
73     cfs = []
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)
76     end
77     if cfs.empty?
78       m.reply _("no config key found matching %{r}") % { :r => params[:rx].to_s}
79     else
80       m.reply _("possible keys: %{kl}") % { :kl => cfs.map { |c| c.first}.join(', ') } if cfs.length > 1
81       m.reply cfs.map { |c| c.join(': ') }.join("\n")
82     end
83   end
84
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}
89     end
90     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
91     @bot.config.items[key].unset
92     handle_get(m, params)
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
95   end
96
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]
102       return false
103     end
104     return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
105     begin
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]
109       return false
110     end
111     if @bot.config.items[key].requires_restart
112       m.reply _("this config change will take effect on the next restart") unless params[:silent]
113       return :restart
114     elsif @bot.config.items[key].requires_rescan
115       m.reply _("this config change will take effect on the next rescan") unless params[:silent]
116       return :rescan
117     else
118       m.okay unless params[:silent]
119       return true
120     end
121   end
122
123   def handle_add(m, params)
124     key = params[:key].to_s.intern
125     value = params[:value]
126     unless @bot.config.items.has_key?(key)
127       m.reply _("no such config key %{key}") % {:key => key}
128       return
129     end
130     unless @bot.config.items[key].kind_of?(Config::ArrayValue)
131       m.reply _("config key %{key} is not an array") % {:key => key}
132       return
133     end
134     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
135     begin
136       @bot.config.items[key].add(value)
137     rescue ArgumentError => e
138       m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
139       return
140     end
141     handle_get(m,{:key => key})
142     m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
143     m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
144   end
145
146   def handle_rm(m, params)
147     key = params[:key].to_s.intern
148     value = params[:value]
149     unless @bot.config.items.has_key?(key)
150       m.reply _("no such config key %{key}") % {:key => key}
151       return
152     end
153     unless @bot.config.items[key].kind_of?(Config::ArrayValue)
154       m.reply _("config key %{key} is not an array") % {:key => key}
155       return
156     end
157     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
158     begin
159       @bot.config.items[key].rm(value)
160     rescue ArgumentError => e
161       m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
162       return
163     end
164     handle_get(m,{:key => key})
165     m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart
166     m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan
167   end
168
169   def bot_save(m, param)
170     @bot.save
171     m.okay
172   end
173
174   def bot_rescan(m, param)
175     m.reply _("saving ...")
176     @bot.save
177     m.reply _("rescanning ...")
178     @bot.rescan
179     m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}
180   end
181
182   def bot_nick(m, param)
183     @bot.nickchg(param[:nick])
184     @bot.wanted_nick = param[:nick]
185   end
186
187   def bot_status(m, param)
188     m.reply @bot.status
189   end
190
191   # TODO is this one of the methods that disappeared when the bot was moved
192   # from the single-file to the multi-file registry?
193   #
194   #  def bot_reg_stat(m, param)
195   #    m.reply @registry.stat.inspect
196   #  end
197
198   def bot_version(m, param)
199     m.reply version_string
200   end
201
202   def ctcp_listen(m)
203     who = m.private? ? "me" : m.target
204     case m.ctcp.intern
205     when :VERSION
206       m.ctcp_reply version_string
207     when :SOURCE
208       m.ctcp_reply Irc::Bot::SOURCE_URL
209     end
210   end
211
212   def handle_help(m, params)
213     m.reply help(params[:topic])
214   end
215
216   def help(plugin, topic="")
217     case plugin
218     when "config"
219       case topic
220       when "list"
221       _("config list => list configuration modules, config list <module> => list configuration keys for module <module>")
222       when "get"
223       _("config get <key> => get configuration value for key <key>")
224       when "unset"
225       _("reset key <key> to the default")
226       when "set"
227       _("config set <key> <value> => set configuration value for key <key> to <value>")
228       when "desc"
229       _("config desc <key> => describe what key <key> configures")
230       when "add"
231       _("config add <value> to <key> => add value <value> to key <key> if <key> is an array")
232       when "rm"
233       _("config rm <value> from <key> => remove value <value> from key <key> if <key> is an array")
234       else
235       _("config module - bot configuration. usage: list, desc, get, set, unset, add, rm")
236       # else
237       #   "no help for config #{topic}"
238       end
239     when "nick"
240       _("nick <newnick> => change the bot nick to <newnick>, if possible")
241     when "status"
242       _("status => display some information on the bot's status")
243     when "save"
244       _("save => save current dynamic data and configuration")
245     when "rescan"
246       _("rescan => reload modules and static facts")
247     when "version"
248       _("version => describes software version")
249     else
250       _("config-related tasks: config, save, rescan, version, nick, status")
251     end
252   end
253
254 end
255
256 conf = ConfigModule.new
257
258 conf.map 'config list :module',
259   :action => 'handle_list',
260   :defaults => {:module => false},
261   :auth_path => 'show'
262 # TODO this one is presently a security risk, since the bot
263 # stores the master password in the config. Do we need auth levels
264 # on the Bot::Config keys too?
265 conf.map 'config get :key',
266   :action => 'handle_get',
267   :auth_path => 'show'
268 conf.map 'config desc :key',
269   :action => 'handle_desc',
270   :auth_path => 'show'
271 conf.map 'config describe :key',
272   :action => 'handle_desc',
273   :auth_path => 'show::desc!'
274 conf.map 'config search *rx',
275   :action => 'handle_search',
276   :auth_path => 'show'
277
278 conf.map "save",
279   :action => 'bot_save'
280 conf.map "rescan",
281   :action => 'bot_rescan'
282 conf.map "nick :nick",
283   :action => 'bot_nick'
284 conf.map "status",
285   :action => 'bot_status',
286   :auth_path => 'show::status'
287 # TODO see above
288 #
289 # conf.map "registry stats",
290 #   :action => 'bot_reg_stat',
291 #   :auth_path => '!config::status'
292 conf.map "version",
293   :action => 'bot_version',
294   :auth_path => 'show::status'
295
296 conf.map 'config set :key *value',
297   :action => 'handle_set',
298   :auth_path => 'edit'
299 conf.map 'config add :value to :key',
300   :action => 'handle_add',
301   :auth_path => 'edit'
302 conf.map 'config rm :value from :key',
303   :action => 'handle_rm',
304   :auth_path => 'edit'
305 conf.map 'config del :value from :key',
306   :action => 'handle_rm',
307   :auth_path => 'edit'
308 conf.map 'config delete :value from :key',
309   :action => 'handle_rm',
310   :auth_path => 'edit'
311 conf.map 'config unset :key',
312   :action => 'handle_unset',
313   :auth_path => 'edit'
314 conf.map 'config reset :key',
315   :action => 'handle_unset',
316   :auth_path => 'edit'
317
318 conf.map 'config help :topic',
319   :action => 'handle_help',
320   :defaults => {:topic => false},
321   :auth_path => '!help!'
322
323 conf.default_auth('*', false)
324 conf.default_auth('show', true)
325 conf.default_auth('show::get', false)
326 # TODO these shouldn't be set here, we need a way to let the default
327 # permission be specified together with the ConfigValue
328 conf.default_auth('key', true)
329 conf.default_auth('key::auth::password', false)
330