]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/config.rb
Relocate help strings to proper locations, add help for 'mode' command
[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 #{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}"\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}"\r
53     end\r
54     puts @bot.config.items[key].inspect\r
55     m.reply "#{key}: #{@bot.config.items[key].desc}"\r
56   end\r
57 \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}"\r
62     end\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
68   end\r
69 \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}" unless params[:silent]\r
75       return false\r
76     end\r
77     return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
78     begin\r
79       @bot.config.items[key].set_string(value)\r
80     rescue ArgumentError => e\r
81       m.reply "failed to set #{key}: #{e.message}" unless params[:silent]\r
82       return false\r
83     end\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
86       return :restart\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
89       return :rescan\r
90     else\r
91       m.okay unless params[:silent]\r
92       return true\r
93     end\r
94   end\r
95 \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}"\r
101       return\r
102     end\r
103     unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
104       m.reply "config key #{key} is not an array"\r
105       return\r
106     end\r
107     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
108     begin\r
109       @bot.config.items[key].add(value)\r
110     rescue ArgumentError => e\r
111       m.reply "failed to add #{value} to #{key}: #{e.message}"\r
112       return\r
113     end\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
117   end\r
118 \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}"\r
124       return\r
125     end\r
126     unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
127       m.reply "config key #{key} is not an array"\r
128       return\r
129     end\r
130     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
131     begin\r
132       @bot.config.items[key].rm(value)\r
133     rescue ArgumentError => e\r
134       m.reply "failed to remove #{value} from #{key}: #{e.message}"\r
135       return\r
136     end\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
140   end\r
141 \r
142   def bot_save(m, param)\r
143     @bot.save\r
144     m.okay\r
145   end\r
146 \r
147   def bot_rescan(m, param)\r
148     m.reply "saving ..."\r
149     @bot.save\r
150     m.reply "rescanning ..."\r
151     @bot.rescan\r
152     m.reply "done. #{@bot.plugins.status(true)}"\r
153   end\r
154 \r
155   def bot_nick(m, param)\r
156     @bot.nickchg(param[:nick])\r
157   end\r
158 \r
159   def bot_status(m, param)\r
160     m.reply @bot.status\r
161   end\r
162 \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
165   #\r
166   #  def bot_reg_stat(m, param)\r
167   #    m.reply @registry.stat.inspect\r
168   #  end\r
169 \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/"\r
172   end\r
173 \r
174   def handle_help(m, params)\r
175     m.reply help(params[:topic])\r
176   end\r
177 \r
178   def help(plugin, topic="")\r
179     case plugin\r
180     when "config"\r
181       case topic\r
182       when ""\r
183       "config-related tasks: config topics, save, rescan"\r
184       when "list"\r
185       "config list => list configuration modules, config list <module> => list configuration keys for module <module>"\r
186       when "get"\r
187       "config get <key> => get configuration value for key <key>"\r
188       when "unset"\r
189       "reset key <key> to the default"\r
190       when "set"\r
191       "config set <key> <value> => set configuration value for key <key> to <value>"\r
192       when "desc"\r
193       "config desc <key> => describe what key <key> configures"\r
194       when "add"\r
195       "config add <value> to <key> => add value <value> to key <key> if <key> is an array"\r
196       when "rm"\r
197       "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"\r
198       else\r
199       "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"\r
200       # else\r
201       #   "no help for config #{topic}"\r
202       end\r
203     when "save"\r
204       "save => save current dynamic data and configuration"\r
205     when "rescan"\r
206       "rescan => reload modules and static facts"\r
207     else\r
208       "config-related tasks: config, save, rescan"\r
209     end\r
210   end\r
211 \r
212 end\r
213 \r
214 conf = ConfigModule.new\r
215 \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
232 \r
233 conf.map "save",\r
234   :action => 'bot_save'\r
235 conf.map "rescan",\r
236   :action => 'bot_rescan'\r
237 conf.map "nick :nick",\r
238   :action => 'bot_nick'\r
239 conf.map "status",\r
240   :action => 'bot_status',\r
241   :auth_path => 'show::status'\r
242 # TODO see above\r
243 #\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
250 \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
272 \r
273 conf.map 'config help :topic',\r
274   :action => 'handle_help',\r
275   :defaults => {:topic => false},\r
276   :auth_path => '!help!'\r
277 \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
284 \r