]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/config.rb
Fix #65 and #95. Disable topic built-in command since the new topic plugin handles...
[user/henk/code/ruby/rbot.git] / lib / rbot / config.rb
index 383cef962e2196857923e153fda96921e21bcca0..027b99023443c1b870a9e432343400e7590d5a07 100644 (file)
@@ -4,13 +4,11 @@ module Irc
   require 'rbot/messagemapper'
 
   unless YAML.respond_to?(:load_file)
-    module YAML
       def YAML.load_file( filepath )
         File.open( filepath ) do |f|
-          load( f )
+          YAML::load( f )
         end
       end
-    end
   end
 
   class BotConfigValue
@@ -138,8 +136,8 @@ module Irc
       end
     end
     def parse(string)
-      unless @values.include?(string)
-        raise ArgumentError, "invalid value #{string}, allowed values are: " + @values.join(", ")
+      unless values.include?(string)
+        raise ArgumentError, "invalid value #{string}, allowed values are: " + values.join(", ")
       end
       string
     end
@@ -313,20 +311,30 @@ module Irc
                    :defaults => {:topic => false}
       
       if(File.exist?("#{@@bot.botclass}/conf.yaml"))
-        newconfig = YAML::load_file("#{@@bot.botclass}/conf.yaml")
-        @@config.update newconfig
-      else
-        # first-run wizard!
-        BotConfigWizard.new(@@bot).run
-        # save newly created config
-        save
+        begin
+          newconfig = YAML::load_file("#{@@bot.botclass}/conf.yaml")
+          @@config.update newconfig
+          return
+        rescue
+          $stderr.puts "failed to read conf.yaml: #{$!}"
+        end
       end
+      # if we got here, we need to run the first-run wizard
+      BotConfigWizard.new(@@bot).run
+      # save newly created config
+      save
     end
 
     # write current configuration to #{botclass}/conf.rbot
     def save
-      File.open("#{@@bot.botclass}/conf.yaml", "w") do |file|
-        file.puts @@config.to_yaml
+      begin
+        File.open("#{@@bot.botclass}/conf.yaml.new", "w") do |file|
+          file.puts @@config.to_yaml
+        end
+        File.rename("#{@@bot.botclass}/conf.yaml.new",
+                    "#{@@bot.botclass}/conf.yaml")
+      rescue
+        $stderr.puts "failed to write configuration file conf.yaml! #{$!}"
       end
     end