]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/config.rb
9ef7ebeef6008bad1ea1239cf9e1de956fce875f
[user/henk/code/ruby/rbot.git] / lib / rbot / core / config.rb
1 #-- vim:sw=2:et\r
2 #++\r
3 \r
4 \r
5 class ConfigModule < CoreBotModule\r
6 \r
7   def save\r
8     @bot.config.save\r
9   end\r
10 \r
11   def handle_list(m, params)\r
12     modules = []\r
13     if params[:module]\r
14       @bot.config.items.each_key do |key|\r
15         mod, name = key.to_s.split('.')\r
16         next unless mod == params[:module]\r
17         modules.push key unless modules.include?(name)\r
18       end\r
19       if modules.empty?\r
20         m.reply "no such module #{params[:module]}"\r
21       else\r
22         m.reply modules.join(", ")\r
23       end\r
24     else\r
25       @bot.config.items.each_key do |key|\r
26         name = key.to_s.split('.').first\r
27         modules.push name unless modules.include?(name)\r
28       end\r
29       m.reply "modules: " + modules.join(", ")\r
30     end\r
31   end\r
32 \r
33   def handle_get(m, params)\r
34     key = params[:key].to_s.intern\r
35     unless @bot.config.items.has_key?(key)\r
36       m.reply "no such config key #{key}"\r
37       return\r
38     end\r
39     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
40     value = @bot.config.items[key].to_s\r
41     m.reply "#{key}: #{value}"\r
42   end\r
43 \r
44   def handle_desc(m, params)\r
45     key = params[:key].to_s.intern\r
46     unless @bot.config.items.has_key?(key)\r
47       m.reply "no such config key #{key}"\r
48     end\r
49     puts @bot.config.items[key].inspect\r
50     m.reply "#{key}: #{@bot.config.items[key].desc}"\r
51   end\r
52 \r
53   def handle_unset(m, params)\r
54     key = params[:key].to_s.intern\r
55     unless @bot.config.items.has_key?(key)\r
56       m.reply "no such config key #{key}"\r
57     end\r
58     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
59     @bot.config.items[key].unset\r
60     handle_get(m, params)\r
61     m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart\r
62     m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan\r
63   end\r
64 \r
65   def handle_set(m, params)\r
66     key = params[:key].to_s.intern\r
67     value = params[:value].join(" ")\r
68     unless @bot.config.items.has_key?(key)\r
69       m.reply "no such config key #{key}" unless params[:silent]\r
70       return false\r
71     end\r
72     return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
73     begin\r
74       @bot.config.items[key].set_string(value)\r
75     rescue ArgumentError => e\r
76       m.reply "failed to set #{key}: #{e.message}" unless params[:silent]\r
77       return false\r
78     end\r
79     if @bot.config.items[key].requires_restart\r
80       m.reply "this config change will take effect on the next restart" unless params[:silent]\r
81       return :restart\r
82     elsif @bot.config.items[key].requires_rescan\r
83       m.reply "this config change will take effect on the next rescan" unless params[:silent]\r
84       return :rescan\r
85     else\r
86       m.okay unless params[:silent]\r
87       return true\r
88     end\r
89   end\r
90 \r
91   def handle_add(m, params)\r
92     key = params[:key].to_s.intern\r
93     value = params[:value]\r
94     unless @bot.config.items.has_key?(key)\r
95       m.reply "no such config key #{key}"\r
96       return\r
97     end\r
98     unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
99       m.reply "config key #{key} is not an array"\r
100       return\r
101     end\r
102     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
103     begin\r
104       @bot.config.items[key].add(value)\r
105     rescue ArgumentError => e\r
106       m.reply "failed to add #{value} to #{key}: #{e.message}"\r
107       return\r
108     end\r
109     handle_get(m,{:key => key})\r
110     m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart\r
111     m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan\r
112   end\r
113 \r
114   def handle_rm(m, params)\r
115     key = params[:key].to_s.intern\r
116     value = params[:value]\r
117     unless @bot.config.items.has_key?(key)\r
118       m.reply "no such config key #{key}"\r
119       return\r
120     end\r
121     unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
122       m.reply "config key #{key} is not an array"\r
123       return\r
124     end\r
125     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
126     begin\r
127       @bot.config.items[key].rm(value)\r
128     rescue ArgumentError => e\r
129       m.reply "failed to remove #{value} from #{key}: #{e.message}"\r
130       return\r
131     end\r
132     handle_get(m,{:key => key})\r
133     m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart\r
134     m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan\r
135   end\r
136 \r
137   def bot_save(m, param)\r
138     @bot.save\r
139     m.okay\r
140   end\r
141 \r
142   def bot_rescan(m, param)\r
143     m.reply "saving ..."\r
144     @bot.save\r
145     m.reply "rescanning ..."\r
146     @bot.rescan\r
147     m.reply "done. #{@bot.plugins.status(true)}"\r
148   end\r
149 \r
150   def bot_nick(m, param)\r
151     @bot.nickchg(param[:nick])\r
152   end\r
153 \r
154   def bot_status(m, param)\r
155     m.reply @bot.status\r
156   end\r
157 \r
158   # TODO is this one of the methods that disappeared when the bot was moved\r
159   # from the single-file to the multi-file registry?\r
160   #\r
161   #  def bot_reg_stat(m, param)\r
162   #    m.reply @registry.stat.inspect\r
163   #  end\r
164 \r
165   def bot_version(m, param)\r
166     m.reply  "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"\r
167   end\r
168 \r
169   def handle_help(m, params)\r
170     m.reply help(params[:topic])\r
171   end\r
172 \r
173   def help(plugin, topic="")\r
174     case topic\r
175     when "list"\r
176       "config list => list configuration modules, config list <module> => list configuration keys for module <module>"\r
177     when "get"\r
178       "config get <key> => get configuration value for key <key>"\r
179     when "unset"\r
180       "reset key <key> to the default"\r
181     when "set"\r
182       "config set <key> <value> => set configuration value for key <key> to <value>"\r
183     when "desc"\r
184       "config desc <key> => describe what key <key> configures"\r
185     when "add"\r
186       "config add <value> to <key> => add value <value> to key <key> if <key> is an array"\r
187     when "rm"\r
188       "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"\r
189     else\r
190       "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"\r
191     # else\r
192     #   "no help for config #{topic}"\r
193     end\r
194   end\r
195 \r
196 end\r
197 \r
198 conf = ConfigModule.new\r
199 \r
200 conf.map 'config list :module',\r
201   :action => 'handle_list',\r
202   :defaults => {:module => false},\r
203   :auth_path => 'show'\r
204 # TODO this one is presently a security risk, since the bot\r
205 # stores the master password in the config. Do we need auth levels\r
206 # on the BotConfig keys too?\r
207 conf.map 'config get :key',\r
208   :action => 'handle_get',\r
209   :auth_path => 'show'\r
210 conf.map 'config desc :key',\r
211   :action => 'handle_desc',\r
212   :auth_path => 'show'\r
213 conf.map 'config describe :key',\r
214   :action => 'handle_desc',\r
215   :auth_path => 'show'\r
216 \r
217 conf.map "save",\r
218   :action => 'bot_save'\r
219 conf.map "rescan",\r
220   :action => 'bot_rescan'\r
221 conf.map "nick :nick",\r
222   :action => 'bot_nick'\r
223 conf.map "status",\r
224   :action => 'bot_status',\r
225   :auth_path => 'show::status'\r
226 # TODO see above\r
227 #\r
228 # conf.map "registry stats",\r
229 #   :action => 'bot_reg_stat',\r
230 #   :auth_path => '!config::status'\r
231 conf.map "version",\r
232   :action => 'bot_version',\r
233   :auth_path => 'show::status'\r
234 \r
235 conf.map 'config set :key *value',\r
236   :action => 'handle_set',\r
237   :auth_path => 'edit'\r
238 conf.map 'config add :value to :key',\r
239   :action => 'handle_add',\r
240   :auth_path => 'edit'\r
241 conf.map 'config rm :value from :key',\r
242   :action => 'handle_rm',\r
243   :auth_path => 'edit'\r
244 conf.map 'config del :value from :key',\r
245   :action => 'handle_rm',\r
246   :auth_path => 'edit'\r
247 conf.map 'config delete :value from :key',\r
248   :action => 'handle_rm',\r
249   :auth_path => 'edit'\r
250 conf.map 'config unset :key',\r
251   :action => 'handle_unset',\r
252   :auth_path => 'edit'\r
253 conf.map 'config reset :key',\r
254   :action => 'handle_unset',\r
255   :auth_path => 'edit'\r
256 \r
257 conf.map 'config help :topic',\r
258   :action => 'handle_help',\r
259   :defaults => {:topic => false},\r
260   :auth_path => '!help!'\r
261 \r
262 conf.default_auth('*', false)\r
263 conf.default_auth('show::status', true)\r
264 # TODO these shouldn't be set here, we need a way to let the default\r
265 # permission be specified together with the BotConfigValue\r
266 conf.default_auth('key', true)\r
267 conf.default_auth('key::auth::password', false)\r
268 \r