]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/config.rb
core/config: remove leftover heavy-load debug line
[user/henk/code/ruby/rbot.git] / lib / rbot / core / config.rb
1 #-- vim:sw=2:et\r
2 #++\r
3 #\r
4 # :title: rbot config management from IRC\r
5 #\r
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
7 # Copyright:: (C) 2006,2007 Giuseppe Bilotta\r
8 # License:: GPL v2\r
9 \r
10 class ConfigModule < CoreBotModule\r
11 \r
12   def save\r
13     @bot.config.save\r
14   end\r
15 \r
16   def handle_list(m, params)\r
17     modules = []\r
18     if params[:module]\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
23       end\r
24       if modules.empty?\r
25         m.reply _("no such module %{module}") % {:module => params[:module]}\r
26       else\r
27         m.reply modules.join(", ")\r
28       end\r
29     else\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
33       end\r
34       m.reply "modules: " + modules.join(", ")\r
35     end\r
36   end\r
37 \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
42       return\r
43     end\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
47   end\r
48 \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
53     end\r
54     m.reply "#{key}: #{@bot.config.items[key].desc}"\r
55   end\r
56 \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
61     end\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
67   end\r
68 \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
74       return false\r
75     end\r
76     return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
77     begin\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
81       return false\r
82     end\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
85       return :restart\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
88       return :rescan\r
89     else\r
90       m.okay unless params[:silent]\r
91       return true\r
92     end\r
93   end\r
94 \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
100       return\r
101     end\r
102     unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
103       m.reply _("config key %{key} is not an array") % {:key => key}\r
104       return\r
105     end\r
106     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
107     begin\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
111       return\r
112     end\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
116   end\r
117 \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
123       return\r
124     end\r
125     unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
126       m.reply _("config key %{key} is not an array") % {:key => key}\r
127       return\r
128     end\r
129     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
130     begin\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
134       return\r
135     end\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
139   end\r
140 \r
141   def bot_save(m, param)\r
142     @bot.save\r
143     m.okay\r
144   end\r
145 \r
146   def bot_rescan(m, param)\r
147     m.reply _("saving ...")\r
148     @bot.save\r
149     m.reply _("rescanning ...")\r
150     @bot.rescan\r
151     m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}\r
152   end\r
153 \r
154   def bot_nick(m, param)\r
155     @bot.nickchg(param[:nick])\r
156   end\r
157 \r
158   def bot_status(m, param)\r
159     m.reply @bot.status\r
160   end\r
161 \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
164   #\r
165   #  def bot_reg_stat(m, param)\r
166   #    m.reply @registry.stat.inspect\r
167   #  end\r
168 \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
171   end\r
172 \r
173   def handle_help(m, params)\r
174     m.reply help(params[:topic])\r
175   end\r
176 \r
177   def help(plugin, topic="")\r
178     case plugin\r
179     when "config"\r
180       case topic\r
181       when ""\r
182       _("config-related tasks: config topics, save, rescan")\r
183       when "list"\r
184       _("config list => list configuration modules, config list <module> => list configuration keys for module <module>")\r
185       when "get"\r
186       _("config get <key> => get configuration value for key <key>")\r
187       when "unset"\r
188       _("reset key <key> to the default")\r
189       when "set"\r
190       _("config set <key> <value> => set configuration value for key <key> to <value>")\r
191       when "desc"\r
192       _("config desc <key> => describe what key <key> configures")\r
193       when "add"\r
194       _("config add <value> to <key> => add value <value> to key <key> if <key> is an array")\r
195       when "rm"\r
196       _("config rm <value> from <key> => remove value <value> from key <key> if <key> is an array")\r
197       else\r
198       _("config module - bot configuration. usage: list, desc, get, set, unset, add, rm")\r
199       # else\r
200       #   "no help for config #{topic}"\r
201       end\r
202     when "save"\r
203       _("save => save current dynamic data and configuration")\r
204     when "rescan"\r
205       _("rescan => reload modules and static facts")\r
206     else\r
207       _("config-related tasks: config, save, rescan")\r
208     end\r
209   end\r
210 \r
211 end\r
212 \r
213 conf = ConfigModule.new\r
214 \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
231 \r
232 conf.map "save",\r
233   :action => 'bot_save'\r
234 conf.map "rescan",\r
235   :action => 'bot_rescan'\r
236 conf.map "nick :nick",\r
237   :action => 'bot_nick'\r
238 conf.map "status",\r
239   :action => 'bot_status',\r
240   :auth_path => 'show::status'\r
241 # TODO see above\r
242 #\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
249 \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
271 \r
272 conf.map 'config help :topic',\r
273   :action => 'handle_help',\r
274   :defaults => {:topic => false},\r
275   :auth_path => '!help!'\r
276 \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
283 \r