]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/config.rb
Plugin header boilerplating.
[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 topic\r
180     when "list"\r
181       "config list => list configuration modules, config list <module> => list configuration keys for module <module>"\r
182     when "get"\r
183       "config get <key> => get configuration value for key <key>"\r
184     when "unset"\r
185       "reset key <key> to the default"\r
186     when "set"\r
187       "config set <key> <value> => set configuration value for key <key> to <value>"\r
188     when "desc"\r
189       "config desc <key> => describe what key <key> configures"\r
190     when "add"\r
191       "config add <value> to <key> => add value <value> to key <key> if <key> is an array"\r
192     when "rm"\r
193       "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"\r
194     else\r
195       "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"\r
196     # else\r
197     #   "no help for config #{topic}"\r
198     end\r
199   end\r
200 \r
201 end\r
202 \r
203 conf = ConfigModule.new\r
204 \r
205 conf.map 'config list :module',\r
206   :action => 'handle_list',\r
207   :defaults => {:module => false},\r
208   :auth_path => 'show'\r
209 # TODO this one is presently a security risk, since the bot\r
210 # stores the master password in the config. Do we need auth levels\r
211 # on the BotConfig keys too?\r
212 conf.map 'config get :key',\r
213   :action => 'handle_get',\r
214   :auth_path => 'show'\r
215 conf.map 'config desc :key',\r
216   :action => 'handle_desc',\r
217   :auth_path => 'show'\r
218 conf.map 'config describe :key',\r
219   :action => 'handle_desc',\r
220   :auth_path => 'show'\r
221 \r
222 conf.map "save",\r
223   :action => 'bot_save'\r
224 conf.map "rescan",\r
225   :action => 'bot_rescan'\r
226 conf.map "nick :nick",\r
227   :action => 'bot_nick'\r
228 conf.map "status",\r
229   :action => 'bot_status',\r
230   :auth_path => 'show::status'\r
231 # TODO see above\r
232 #\r
233 # conf.map "registry stats",\r
234 #   :action => 'bot_reg_stat',\r
235 #   :auth_path => '!config::status'\r
236 conf.map "version",\r
237   :action => 'bot_version',\r
238   :auth_path => 'show::status'\r
239 \r
240 conf.map 'config set :key *value',\r
241   :action => 'handle_set',\r
242   :auth_path => 'edit'\r
243 conf.map 'config add :value to :key',\r
244   :action => 'handle_add',\r
245   :auth_path => 'edit'\r
246 conf.map 'config rm :value from :key',\r
247   :action => 'handle_rm',\r
248   :auth_path => 'edit'\r
249 conf.map 'config del :value from :key',\r
250   :action => 'handle_rm',\r
251   :auth_path => 'edit'\r
252 conf.map 'config delete :value from :key',\r
253   :action => 'handle_rm',\r
254   :auth_path => 'edit'\r
255 conf.map 'config unset :key',\r
256   :action => 'handle_unset',\r
257   :auth_path => 'edit'\r
258 conf.map 'config reset :key',\r
259   :action => 'handle_unset',\r
260   :auth_path => 'edit'\r
261 \r
262 conf.map 'config help :topic',\r
263   :action => 'handle_help',\r
264   :defaults => {:topic => false},\r
265   :auth_path => '!help!'\r
266 \r
267 conf.default_auth('*', false)\r
268 conf.default_auth('show::status', true)\r
269 # TODO these shouldn't be set here, we need a way to let the default\r
270 # permission be specified together with the BotConfigValue\r
271 conf.default_auth('key', true)\r
272 conf.default_auth('key::auth::password', false)\r
273 \r