]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/config.rb
config botmodule: support CTCP commands VERSION and SOURCE
[user/henk/code/ruby/rbot.git] / lib / rbot / core / config.rb
index 47bed108a37449181486d356cb76f38e3130410b..717408a23e286d215f88ee91672467a165468186 100644 (file)
@@ -1,9 +1,26 @@
 #-- vim:sw=2:et\r
 #++\r
-\r
+#\r
+# :title: rbot config management from IRC\r
+#\r
+# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
+# Copyright:: (C) 2006,2007 Giuseppe Bilotta\r
+# License:: GPL v2\r
 \r
 class ConfigModule < CoreBotModule\r
 \r
+  def version_string\r
+    _("I'm a v. %{version} rubybot%{copyright}%{url}") % {\r
+      :version => $version,\r
+      :copyright => ", #{Irc::Bot::COPYRIGHT_NOTICE}",\r
+      :url => " - #{Irc::Bot::SOURCE_URL}"\r
+    }\r
+  end\r
+\r
+  def save\r
+    @bot.config.save\r
+  end\r
+\r
   def handle_list(m, params)\r
     modules = []\r
     if params[:module]\r
@@ -13,12 +30,12 @@ class ConfigModule < CoreBotModule
         modules.push key unless modules.include?(name)\r
       end\r
       if modules.empty?\r
-        m.reply "no such module #{params[:module]}"\r
+        m.reply _("no such module %{module}") % {:module => params[:module]}\r
       else\r
         m.reply modules.join(", ")\r
       end\r
     else\r
-      @bot.configitems.each_key do |key|\r
+      @bot.config.items.each_key do |key|\r
         name = key.to_s.split('.').first\r
         modules.push name unless modules.include?(name)\r
       end\r
@@ -29,9 +46,10 @@ class ConfigModule < CoreBotModule
   def handle_get(m, params)\r
     key = params[:key].to_s.intern\r
     unless @bot.config.items.has_key?(key)\r
-      m.reply "no such config key #{key}"\r
+      m.reply _("no such config key %{key}") % {:key => key}\r
       return\r
     end\r
+    return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
     value = @bot.config.items[key].to_s\r
     m.reply "#{key}: #{value}"\r
   end\r
@@ -39,42 +57,46 @@ class ConfigModule < CoreBotModule
   def handle_desc(m, params)\r
     key = params[:key].to_s.intern\r
     unless @bot.config.items.has_key?(key)\r
-      m.reply "no such config key #{key}"\r
+      m.reply _("no such config key %{key}") % {:key => key}\r
     end\r
-    puts @bot.config.items[key].inspect\r
     m.reply "#{key}: #{@bot.config.items[key].desc}"\r
   end\r
 \r
   def handle_unset(m, params)\r
     key = params[:key].to_s.intern\r
     unless @bot.config.items.has_key?(key)\r
-      m.reply "no such config key #{key}"\r
+      m.reply _("no such config key %{key}") % {:key => key}\r
     end\r
+    return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
     @bot.config.items[key].unset\r
     handle_get(m, params)\r
-    m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart\r
-    m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan\r
+    m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart\r
+    m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan\r
   end\r
 \r
   def handle_set(m, params)\r
     key = params[:key].to_s.intern\r
     value = params[:value].join(" ")\r
     unless @bot.config.items.has_key?(key)\r
-      m.reply "no such config key #{key}"\r
-      return\r
+      m.reply _("no such config key %{key}") % {:key => key} unless params[:silent]\r
+      return false\r
     end\r
+    return false if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
     begin\r
       @bot.config.items[key].set_string(value)\r
     rescue ArgumentError => e\r
-      m.reply "failed to set #{key}: #{e.message}"\r
-      return\r
+      m.reply _("failed to set %{key}: %{error}") % {:key => key, :error => e.message} unless params[:silent]\r
+      return false\r
     end\r
     if @bot.config.items[key].requires_restart\r
-      m.reply "this config change will take effect on the next restart"\r
+      m.reply _("this config change will take effect on the next restart") unless params[:silent]\r
+      return :restart\r
     elsif @bot.config.items[key].requires_rescan\r
-      m.reply "this config change will take effect on the next rescan"\r
+      m.reply _("this config change will take effect on the next rescan") unless params[:silent]\r
+      return :rescan\r
     else\r
-      m.okay\r
+      m.okay unless params[:silent]\r
+      return true\r
     end\r
   end\r
 \r
@@ -82,44 +104,46 @@ class ConfigModule < CoreBotModule
     key = params[:key].to_s.intern\r
     value = params[:value]\r
     unless @bot.config.items.has_key?(key)\r
-      m.reply "no such config key #{key}"\r
+      m.reply _("no such config key %{key}") % {:key => key}\r
       return\r
     end\r
-    unless @bot.config.items[key].class <= BotConfigArrayValue\r
-      m.reply "config key #{key} is not an array"\r
+    unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
+      m.reply _("config key %{key} is not an array") % {:key => key}\r
       return\r
     end\r
+    return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
     begin\r
       @bot.config.items[key].add(value)\r
     rescue ArgumentError => e\r
-      m.reply "failed to add #{value} to #{key}: #{e.message}"\r
+      m.reply _("failed to add %{value} to %{key}: %{error}") % {:value => value, :key => key, :error => e.message}\r
       return\r
     end\r
     handle_get(m,{:key => key})\r
-    m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart\r
-    m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan\r
+    m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart\r
+    m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan\r
   end\r
 \r
   def handle_rm(m, params)\r
     key = params[:key].to_s.intern\r
     value = params[:value]\r
     unless @bot.config.items.has_key?(key)\r
-      m.reply "no such config key #{key}"\r
+      m.reply _("no such config key %{key}") % {:key => key}\r
       return\r
     end\r
-    unless @bot.config.items[key].class <= BotConfigArrayValue\r
-      m.reply "config key #{key} is not an array"\r
+    unless @bot.config.items[key].kind_of?(BotConfigArrayValue)\r
+      m.reply _("config key %{key} is not an array") % {:key => key}\r
       return\r
     end\r
+    return if !@bot.auth.allow?(@bot.config.items[key].auth_path, m.source, m.replyto)\r
     begin\r
       @bot.config.items[key].rm(value)\r
     rescue ArgumentError => e\r
-      m.reply "failed to remove #{value} from #{key}: #{e.message}"\r
+      m.reply _("failed to remove %{value} from %{key}: %{error}") % {:value => value, :key => key, :error => e.message}\r
       return\r
     end\r
     handle_get(m,{:key => key})\r
-    m.reply "this config change will take effect on the next restart" if @bot.config.items[key].requires_restart\r
-    m.reply "this config change will take effect on the next rescan" if @bot.config.items[key].requires_rescan\r
+    m.reply _("this config change will take effect on the next restart") if @bot.config.items[key].requires_restart\r
+    m.reply _("this config change will take effect on the next rescan") if @bot.config.items[key].requires_rescan\r
   end\r
 \r
   def bot_save(m, param)\r
@@ -128,11 +152,11 @@ class ConfigModule < CoreBotModule
   end\r
 \r
   def bot_rescan(m, param)\r
-    m.reply "saving ..."\r
+    m.reply _("saving ...")\r
     @bot.save\r
-    m.reply "rescanning ..."\r
+    m.reply _("rescanning ...")\r
     @bot.rescan\r
-    m.reply "done. #{@plugins.status(true)}"\r
+    m.reply _("done. %{plugin_status}") % {:plugin_status => @bot.plugins.status(true)}\r
   end\r
 \r
   def bot_nick(m, param)\r
@@ -151,33 +175,58 @@ class ConfigModule < CoreBotModule
   #  end\r
 \r
   def bot_version(m, param)\r
-    m.reply  "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"\r
+    m.reply version_string\r
+  end\r
+\r
+  def ctcp_listen(m)\r
+    who = m.private? ? "me" : m.target\r
+    case m.ctcp.intern\r
+    when :VERSION\r
+      m.ctcp_reply version_string\r
+      @bot.irclog "@ #{m.source} asked #{who} about version info"\r
+    when :SOURCE\r
+      m.ctcp_reply Irc::Bot::SOURCE_URL\r
+      @bot.irclog "@ #{m.source} asked #{who} about source info"\r
+    end\r
   end\r
 \r
   def handle_help(m, params)\r
     m.reply help(params[:topic])\r
   end\r
 \r
-  def help(topic="")\r
-    case topic\r
-    when false\r
-      "config module - bot configuration. usage: list, desc, get, set, unset, add, rm"\r
-    when "list"\r
-      "config list => list configuration modules, config list <module> => list configuration keys for module <module>"\r
-    when "get"\r
-      "config get <key> => get configuration value for key <key>"\r
-    when "unset"\r
-      "reset key <key> to the default"\r
-    when "set"\r
-      "config set <key> <value> => set configuration value for key <key> to <value>"\r
-    when "desc"\r
-      "config desc <key> => describe what key <key> configures"\r
-    when "add"\r
-      "config add <value> to <key> => add value <value> to key <key> if <key> is an array"\r
-    when "rm"\r
-      "config rm <value> from <key> => remove value <value> from key <key> if <key> is an array"\r
+  def help(plugin, topic="")\r
+    case plugin\r
+    when "config"\r
+      case topic\r
+      when ""\r
+      _("config-related tasks: config topics, save, rescan")\r
+      when "list"\r
+      _("config list => list configuration modules, config list <module> => list configuration keys for module <module>")\r
+      when "get"\r
+      _("config get <key> => get configuration value for key <key>")\r
+      when "unset"\r
+      _("reset key <key> to the default")\r
+      when "set"\r
+      _("config set <key> <value> => set configuration value for key <key> to <value>")\r
+      when "desc"\r
+      _("config desc <key> => describe what key <key> configures")\r
+      when "add"\r
+      _("config add <value> to <key> => add value <value> to key <key> if <key> is an array")\r
+      when "rm"\r
+      _("config rm <value> from <key> => remove value <value> from key <key> if <key> is an array")\r
+      else\r
+      _("config module - bot configuration. usage: list, desc, get, set, unset, add, rm")\r
+      # else\r
+      #   "no help for config #{topic}"\r
+      end\r
+    when "save"\r
+      _("save => save current dynamic data and configuration")\r
+    when "rescan"\r
+      _("rescan => reload modules and static facts")\r
+    when "version"\r
+      _("version => describes software version")\r
     else\r
-      "no help for config #{topic}"\r
+      _("config-related tasks: config, save, rescan, version")\r
     end\r
   end\r
 \r
@@ -249,4 +298,8 @@ conf.map 'config help :topic',
 \r
 conf.default_auth('*', false)\r
 conf.default_auth('show::status', true)\r
+# TODO these shouldn't be set here, we need a way to let the default\r
+# permission be specified together with the BotConfigValue\r
+conf.default_auth('key', true)\r
+conf.default_auth('key::auth::password', false)\r
 \r