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