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