summaryrefslogtreecommitdiff
path: root/lib/rbot/core
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-08-02 20:37:02 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-08-02 20:37:02 +0000
commiteee6a74ea1547b5c0e8757c64cace42fc9aea9bf (patch)
tree0e21f0b1014a8977a08473eba182f11e3515955c /lib/rbot/core
parent3538de46fb71b69e74106f0056ded7bb9beafd89 (diff)
Initial factorization of botconfig into kernel functionality and a coremodule
Diffstat (limited to 'lib/rbot/core')
-rw-r--r--lib/rbot/core/config.rb252
-rw-r--r--lib/rbot/core/core.rb60
2 files changed, 254 insertions, 58 deletions
diff --git a/lib/rbot/core/config.rb b/lib/rbot/core/config.rb
new file mode 100644
index 00000000..47bed108
--- /dev/null
+++ b/lib/rbot/core/config.rb
@@ -0,0 +1,252 @@
+#-- vim:sw=2:et
+#++
+
+
+class ConfigModule < CoreBotModule
+
+ def handle_list(m, params)
+ modules = []
+ if params[:module]
+ @bot.config.items.each_key do |key|
+ mod, name = key.to_s.split('.')
+ next unless mod == params[:module]
+ modules.push key unless modules.include?(name)
+ end
+ if modules.empty?
+ m.reply "no such module #{params[:module]}"
+ else
+ m.reply modules.join(", ")
+ end
+ else
+ @bot.configitems.each_key do |key|
+ name = key.to_s.split('.').first
+ modules.push name unless modules.include?(name)
+ end
+ m.reply "modules: " + modules.join(", ")
+ end
+ end
+
+ def handle_get(m, params)
+ key = params[:key].to_s.intern
+ unless @bot.config.items.has_key?(key)
+ m.reply "no such config key #{key}"
+ return
+ end
+ value = @bot.config.items[key].to_s
+ m.reply "#{key}: #{value}"
+ end
+
+ def handle_desc(m, params)
+ key = params[:key].to_s.intern
+ unless @bot.config.items.has_key?(key)
+ m.reply "no such config key #{key}"
+ end
+ puts @bot.config.items[key].inspect
+ m.reply "#{key}: #{@bot.config.items[key].desc}"
+ end
+
+ def handle_unset(m, params)
+ key = params[:key].to_s.intern
+ unless @bot.config.items.has_key?(key)
+ m.reply "no such config key #{key}"
+ end
+ @bot.config.items[key].unset
+ handle_get(m, params)
+ m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
+ m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
+ end
+
+ def handle_set(m, params)
+ key = params[:key].to_s.intern
+ value = params[:value].join(" ")
+ unless @bot.config.items.has_key?(key)
+ m.reply "no such config key #{key}"
+ return
+ end
+ begin
+ @bot.config.items[key].set_string(value)
+ rescue ArgumentError => e
+ m.reply "failed to set #{key}: #{e.message}"
+ return
+ end
+ if @bot.config.items[key].requires_restart
+ m.reply "this config change will take effect on the next restart"
+ elsif @bot.config.items[key].requires_rescan
+ m.reply "this config change will take effect on the next rescan"
+ else
+ m.okay
+ end
+ end
+
+ def handle_add(m, params)
+ key = params[:key].to_s.intern
+ value = params[:value]
+ unless @bot.config.items.has_key?(key)
+ m.reply "no such config key #{key}"
+ return
+ end
+ unless @bot.config.items[key].class <= BotConfigArrayValue
+ m.reply "config key #{key} is not an array"
+ return
+ end
+ begin
+ @bot.config.items[key].add(value)
+ rescue ArgumentError => e
+ m.reply "failed to add #{value} to #{key}: #{e.message}"
+ return
+ end
+ handle_get(m,{:key => key})
+ m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
+ m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
+ end
+
+ def handle_rm(m, params)
+ key = params[:key].to_s.intern
+ value = params[:value]
+ unless @bot.config.items.has_key?(key)
+ m.reply "no such config key #{key}"
+ return
+ end
+ unless @bot.config.items[key].class <= BotConfigArrayValue
+ m.reply "config key #{key} is not an array"
+ return
+ end
+ begin
+ @bot.config.items[key].rm(value)
+ rescue ArgumentError => e
+ m.reply "failed to remove #{value} from #{key}: #{e.message}"
+ return
+ end
+ handle_get(m,{:key => key})
+ m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart
+ m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan
+ end
+
+ def bot_save(m, param)
+ @bot.save
+ m.okay
+ end
+
+ def bot_rescan(m, param)
+ m.reply "saving ..."
+ @bot.save
+ m.reply "rescanning ..."
+ @bot.rescan
+ m.reply "done. #{@plugins.status(true)}"
+ end
+
+ def bot_nick(m, param)
+ @bot.nickchg(param[:nick])
+ end
+
+ def bot_status(m, param)
+ m.reply @bot.status
+ end
+
+ # TODO is this one of the methods that disappeared when the bot was moved
+ # from the single-file to the multi-file registry?
+ #
+ # def bot_reg_stat(m, param)
+ # m.reply @registry.stat.inspect
+ # end
+
+ def bot_version(m, param)
+ m.reply "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
+ end
+
+ def handle_help(m, params)
+ m.reply help(params[:topic])
+ end
+
+ def help(topic="")
+ case topic
+ when false
+ "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"
+ when "list"
+ "config list => list configuration modules, config list <module> => list configuration keys for module <module>"
+ when "get"
+ "config get <key> => get configuration value for key <key>"
+ when "unset"
+ "reset key <key> to the default"
+ when "set"
+ "config set <key> <value> => set configuration value for key <key> to <value>"
+ when "desc"
+ "config desc <key> => describe what key <key> configures"
+ when "add"
+ "config add <value> to <key> => add value <value> to key <key> if <key> is an array"
+ when "rm"
+ "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"
+ else
+ "no help for config #{topic}"
+ end
+ end
+
+end
+
+conf = ConfigModule.new
+
+conf.map 'config list :module',
+ :action => 'handle_list',
+ :defaults => {:module => false},
+ :auth_path => 'show'
+# TODO this one is presently a security risk, since the bot
+# stores the master password in the config. Do we need auth levels
+# on the BotConfig keys too?
+conf.map 'config get :key',
+ :action => 'handle_get',
+ :auth_path => 'show'
+conf.map 'config desc :key',
+ :action => 'handle_desc',
+ :auth_path => 'show'
+conf.map 'config describe :key',
+ :action => 'handle_desc',
+ :auth_path => 'show'
+
+conf.map "save",
+ :action => 'bot_save'
+conf.map "rescan",
+ :action => 'bot_rescan'
+conf.map "nick :nick",
+ :action => 'bot_nick'
+conf.map "status",
+ :action => 'bot_status',
+ :auth_path => 'show::status'
+# TODO see above
+#
+# conf.map "registry stats",
+# :action => 'bot_reg_stat',
+# :auth_path => '!config::status'
+conf.map "version",
+ :action => 'bot_version',
+ :auth_path => 'show::status'
+
+conf.map 'config set :key *value',
+ :action => 'handle_set',
+ :auth_path => 'edit'
+conf.map 'config add :value to :key',
+ :action => 'handle_add',
+ :auth_path => 'edit'
+conf.map 'config rm :value from :key',
+ :action => 'handle_rm',
+ :auth_path => 'edit'
+conf.map 'config del :value from :key',
+ :action => 'handle_rm',
+ :auth_path => 'edit'
+conf.map 'config delete :value from :key',
+ :action => 'handle_rm',
+ :auth_path => 'edit'
+conf.map 'config unset :key',
+ :action => 'handle_unset',
+ :auth_path => 'edit'
+conf.map 'config reset :key',
+ :action => 'handle_unset',
+ :auth_path => 'edit'
+
+conf.map 'config help :topic',
+ :action => 'handle_help',
+ :defaults => {:topic => false},
+ :auth_path => '!help!'
+
+conf.default_auth('*', false)
+conf.default_auth('show::status', true)
+
diff --git a/lib/rbot/core/core.rb b/lib/rbot/core/core.rb
index 55da1a7d..cb5df226 100644
--- a/lib/rbot/core/core.rb
+++ b/lib/rbot/core/core.rb
@@ -2,7 +2,7 @@
#++
-class Core < CoreBotModule
+class CoreModule < CoreBotModule
def listen(m)
return unless m.class <= PrivMessage
@@ -41,15 +41,6 @@ class Core < CoreBotModule
@bot.join 0
end
- def bot_save(m, param)
- @bot.save
- m.okay
- end
-
- def bot_nick(m, param)
- @bot.nickchg(param[:nick])
- end
-
def bot_say(m, param)
@bot.say param[:where], param[:what].join(" ")
end
@@ -66,14 +57,6 @@ class Core < CoreBotModule
m.reply "pong"
end
- def bot_rescan(m, param)
- m.reply "saving ..."
- @bot.save
- m.reply "rescanning ..."
- @bot.rescan
- m.reply "done. #{@plugins.status(true)}"
- end
-
def bot_quiet(m, param)
if param.has_key?(:where)
@bot.set_quiet param[:where].sub(/^here$/, m.target)
@@ -90,21 +73,6 @@ class Core < CoreBotModule
end
end
- def bot_status(m, param)
- m.reply @bot.status
- end
-
- # TODO is this one of the methods that disappeared when the bot was moved
- # from the single-file to the multi-file registry?
- #
- # def bot_reg_stat(m, param)
- # m.reply @registry.stat.inspect
- # end
-
- def bot_version(m, param)
- m.reply "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
- end
-
def bot_help(m, param)
m.reply @bot.help(param[:topic].join(" "))
end
@@ -160,7 +128,7 @@ class Core < CoreBotModule
end
end
-core = Core.new
+core = CoreModule.new
core.map "quit *msg",
:action => 'bot_quit',
@@ -171,27 +139,6 @@ core.map "restart *msg",
:defaults => { :msg => nil },
:auth_path => 'quit'
-core.map "save",
- :action => 'bot_save',
- :auth_path => 'config'
-core.map "rescan",
- :action => 'bot_rescan',
- :auth_path => 'config'
-core.map "nick :nick",
- :action => 'bot_nick',
- :auth_path => 'config'
-core.map "status",
- :action => 'bot_status',
- :auth_path => 'config::show'
- # TODO see above
- #
- # core.map "registry stats",
- # :action => 'bot_reg_stat',
- # :auth_path => 'config::show'
-core.map "version",
- :action => 'bot_version',
- :auth_path => 'config::show'
-
core.map "quiet",
:action => 'bot_quiet',
:auth_path => 'talk::set'
@@ -235,8 +182,5 @@ core.map "help *topic",
:default => { :topic => [""] },
:auth_path => '!help!'
-# TODO the first line should probably go to the auth module?
-#
core.default_auth('*', false)
-core.default_auth('config::show', true)