]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/config.rb
introduce reload alias for rescan
[user/henk/code/ruby/rbot.git] / lib / rbot / core / config.rb
index 38140f18b6599979abb2a72a78c099387553eb10..43d08829884a5606c64206129f5b36494f7c6061 100644 (file)
@@ -4,16 +4,14 @@
 # :title: rbot config management from IRC
 #
 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-# Copyright:: (C) 2006,2007 Giuseppe Bilotta
-# License:: GPL v2
 
 class ConfigModule < CoreBotModule
 
   def version_string
     if $version_timestamp.to_i > 0
-      ago = String.new ' ['
-      ago << Utils.secs_to_string(Time.now.to_i - $version_timestamp.to_i)
-      ago << ' ago]'
+      ago = _(" [%{secs} ago]") % {
+        :secs => Utils.secs_to_string(Time.now.to_i - $version_timestamp.to_i)
+      }
     else
       ago = ''
     end
@@ -74,13 +72,13 @@ class ConfigModule < CoreBotModule
     rx = Regexp.new(params[:rx].to_s, true)
     cfs = []
     @bot.config.items.each do |k, v|
-      cfs << v if k.to_s.match(rx) or (v.desc.match(rx) rescue false)
+      cfs << [Bold + k.to_s + Bold, v.desc] if k.to_s.match(rx) or (v.desc.match(rx) rescue false)
     end
     if cfs.empty?
       m.reply _("no config key found matching %{r}") % { :r => params[:rx].to_s}
     else
-      m.reply _("possible keys: %{kl}") % { :kl => cfs.map { |c| c.key}.join(', ') }
-      m.reply cfs.map { |c| [c.key, c.desc].join(': ') }.join("\n")
+      m.reply _("possible keys: %{kl}") % { :kl => cfs.map { |c| c.first}.sort.join(', ') } if cfs.length > 1
+      m.reply cfs.map { |c| c.join(': ') }.join("\n")
     end
   end
 
@@ -124,7 +122,7 @@ class ConfigModule < CoreBotModule
 
   def handle_add(m, params)
     key = params[:key].to_s.intern
-    value = params[:value]
+    values = params[:value].to_s.split(/,\s+/)
     unless @bot.config.items.has_key?(key)
       m.reply _("no such config key %{key}") % {:key => key}
       return
@@ -134,11 +132,13 @@ class ConfigModule < CoreBotModule
       return
     end
     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
-    begin
-      @bot.config.items[key].add(value)
-    rescue ArgumentError => e
-      m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
-      return
+    values.each do |value|
+      begin
+        @bot.config.items[key].add(value)
+      rescue ArgumentError => e
+        m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
+        next
+      end
     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
@@ -147,7 +147,7 @@ class ConfigModule < CoreBotModule
 
   def handle_rm(m, params)
     key = params[:key].to_s.intern
-    value = params[:value]
+    values = params[:value].to_s.split(/,\s+/)
     unless @bot.config.items.has_key?(key)
       m.reply _("no such config key %{key}") % {:key => key}
       return
@@ -157,11 +157,13 @@ class ConfigModule < CoreBotModule
       return
     end
     return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)
-    begin
-      @bot.config.items[key].rm(value)
-    rescue ArgumentError => e
-      m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
-      return
+    values.each do |value|
+      begin
+        @bot.config.items[key].rm(value)
+      rescue ArgumentError => e
+        m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}
+        next
+      end
     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
@@ -174,15 +176,32 @@ class ConfigModule < CoreBotModule
   end
 
   def bot_rescan(m, param)
-    m.reply _("saving ...")
-    @bot.save
-    m.reply _("rescanning ...")
-    @bot.rescan
-    m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}
+    if param[:botmodule]
+      name = param[:botmodule]
+      if not @bot.plugins.has_key? name
+        m.reply _("botmodule not found")
+        return # error
+      else
+        botmodule = @bot.plugins[name]
+        m.reply _("botmodule %s... saving... rescanning...") % [name]
+      end
+    else
+      m.reply _("saving... rescanning...")
+    end
+
+    @bot.rescan(botmodule)
+    m.reply _("done. %{plugin_status}") % {
+      :plugin_status => @bot.plugins.status(true)}
+    failure = @bot.plugins.botmodule_failure(name) if botmodule
+    if failure
+      m.reply _("plugin failed to load, %{failure}") % {
+        :failure => failure}
+    end
   end
 
   def bot_nick(m, param)
     @bot.nickchg(param[:nick])
+    @bot.wanted_nick = param[:nick]
   end
 
   def bot_status(m, param)
@@ -229,7 +248,7 @@ class ConfigModule < CoreBotModule
       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")
+      _("config add <values> to <key> => add values <values> 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
@@ -244,11 +263,13 @@ class ConfigModule < CoreBotModule
     when "save"
       _("save => save current dynamic data and configuration")
     when "rescan"
-      _("rescan => reload modules and static facts")
+      _("rescan [<botmodule>] => reload specified or all botmodules and static facts")
+    when "reload"
+      _("reload [<botmodule>] => reload specified or all botmodules and static facts")
     when "version"
       _("version => describes software version")
     else
-      _("config-related tasks: config, save, rescan, version, nick, status")
+      _("config-related tasks: config, save, rescan(/reload), version, nick, status")
     end
   end
 
@@ -278,7 +299,9 @@ conf.map 'config search *rx',
 
 conf.map "save",
   :action => 'bot_save'
-conf.map "rescan",
+conf.map "rescan [:botmodule]",
+  :action => 'bot_rescan'
+conf.map "reload [:botmodule]",
   :action => 'bot_rescan'
 conf.map "nick :nick",
   :action => 'bot_nick'
@@ -297,16 +320,19 @@ conf.map "version",
 conf.map 'config set :key *value',
   :action => 'handle_set',
   :auth_path => 'edit'
-conf.map 'config add :value to :key',
+conf.map 'config add *value to :key',
   :action => 'handle_add',
   :auth_path => 'edit'
-conf.map 'config rm :value from :key',
+conf.map 'config rm *value from :key',
+  :action => 'handle_rm',
+  :auth_path => 'edit'
+conf.map 'config remove *value from :key',
   :action => 'handle_rm',
   :auth_path => 'edit'
-conf.map 'config del :value from :key',
+conf.map 'config del *value from :key',
   :action => 'handle_rm',
   :auth_path => 'edit'
-conf.map 'config delete :value from :key',
+conf.map 'config delete *value from :key',
   :action => 'handle_rm',
   :auth_path => 'edit'
 conf.map 'config unset :key',