]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/config.rb
timer: tick when rescheduling
[user/henk/code/ruby/rbot.git] / lib / rbot / config.rb
index fd745397a9470fd07d5e0bca9739a00bbaa4b8f0..eb342f7cbcc27fe55010a1c069c592e0d4545064 100644 (file)
@@ -70,6 +70,8 @@ module Irc
     end
     def unset
       @manager.config.delete(@key)
+      @manager.changed = true
+      @on_change.call(@manager.bot, value) if @on_change
     end
 
     # set string will raise ArgumentErrors on failed parse/validate
@@ -112,15 +114,36 @@ module Irc
     def parse(string)
       return true if string == "true"
       return false if string == "false"
-      raise ArgumentError, "#{string} does not match either 'true' or 'false'"
+      if string =~ /^-?\d+$/
+        return string.to_i != 0
+      end
+      raise ArgumentError, "#{string} does not match either 'true' or 'false', and it's not an integer either"
+    end
+    def get
+      r = super
+      if r.kind_of?(Integer)
+        return r != 0
+      else
+        return r
+      end
     end
   end
 
   class BotConfigIntegerValue < BotConfigValue
     def parse(string)
+      return 1 if string == "true"
+      return 0 if string == "false"
       raise ArgumentError, "not an integer: #{string}" unless string =~ /^-?\d+$/
       string.to_i
     end
+    def get
+      r = super
+      if r.kind_of?(Integer)
+        return r
+      else
+        return r ? 1 : 0
+      end
+    end
   end
 
   class BotConfigFloatValue < BotConfigValue
@@ -167,7 +190,7 @@ module Irc
       string
     end
     def desc
-      "#{@desc} [valid values are: " + values.join(", ") + "]"
+      _("%{desc} [valid values are: %{values}]") % {:desc => @desc, :values => values.join(', ')}
     end
   end
 
@@ -236,7 +259,7 @@ module Irc
       #        return @config[key]
       #      end
       if @config.has_key?(key.to_sym)
-        warning "Unregistered lookup #{key.to_sym.inspect}"
+        warning _("Unregistered lookup #{key.to_sym.inspect}")
         return @config[key.to_sym]
       end
       return false
@@ -269,6 +292,7 @@ module Irc
         debug "Officializing conf.yaml ..."
         File.rename("#{@bot.botclass}/conf.yaml.new",
                     "#{@bot.botclass}/conf.yaml")
+        @changed = false
       rescue => e
         error "failed to write configuration file conf.yaml! #{$!}"
         error "#{e.class}: #{e}"
@@ -298,18 +322,16 @@ module Irc
     end
 
     def run()
-      puts "First time rbot configuration wizard"
+      puts _("First time rbot configuration wizard")
       puts "===================================="
-      puts "This is the first time you have run rbot with a config directory of:"
-      puts @bot.botclass
-      puts "This wizard will ask you a few questions to get you started."
-      puts "The rest of rbot's configuration can be manipulated via IRC once"
-      puts "rbot is connected and you are auth'd."
+      puts _("This is the first time you have run rbot with a config directory of: #{@bot.botclass}")
+      puts _("This wizard will ask you a few questions to get you started.")
+      puts _("The rest of rbot's configuration can be manipulated via IRC once rbot is connected and you are auth'd.")
       puts "-----------------------------------"
 
       return unless @questions
       @questions.sort{|a,b| a.order <=> b.order }.each do |q|
-        puts q.desc
+        puts _(q.desc)
         begin
           print q.key.to_s + " [#{q.to_s}]: "
           response = STDIN.gets
@@ -317,10 +339,10 @@ module Irc
           unless response.empty?
             q.set_string response, false
           end
-          puts "configured #{q.key} => #{q.to_s}"
+          puts _("configured #{q.key} => #{q.to_s}")
           puts "-----------------------------------"
         rescue ArgumentError => e
-          puts "failed to set #{q.key}: #{e.message}"
+          puts _("failed to set #{q.key}: #{e.message}")
           retry
         end
       end