]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/auth.rb
Lots of fixes all around, in preparation for the new auth coremodule
[user/henk/code/ruby/rbot.git] / lib / rbot / auth.rb
index 3fbf4b697b4dbf4a7dec3afad67b80c8eca1d5cb..c865636cd937072c9e72a4ed2b1685eb06fef636 100644 (file)
@@ -53,6 +53,9 @@ module Irc
           end
         end
       end
+      if @levels.length < 1
+        raise RuntimeError, "No valid levels.rbot found! If you really want a free-for-all bot and this isn't the result of a previous error, write a proper levels.rbot"
+      end
     end
 
     # save current users and levels to files.
@@ -60,13 +63,33 @@ module Irc
     # users are written to #{botclass}/users.yaml
     def save
       Dir.mkdir("#{@bot.botclass}") if(!File.exist?("#{@bot.botclass}"))
-      File.open("#{@bot.botclass}/users.yaml", 'w') do |file|
-        file.puts @users.to_yaml
+      begin
+        debug "Writing new users.yaml ..."
+        File.open("#{@bot.botclass}/users.yaml.new", 'w') do |file|
+          file.puts @users.to_yaml
+        end
+        debug "Officializing users.yaml ..."
+        File.rename("#{@bot.botclass}/users.yaml.new",
+                    "#{@bot.botclass}/users.yaml")
+      rescue
+        error "failed to write configuration file users.yaml! #{$!}"
+        error "#{e.class}: #{e}"
+        error e.backtrace.join("\n")
       end
-      File.open("#{@bot.botclass}/levels.rbot", 'w') do |file|
-        @levels.each do |key, value|
-          file.puts "#{value} #{key}"
+      begin
+        debug "Writing new levels.rbot ..."
+        File.open("#{@bot.botclass}/levels.rbot.new", 'w') do |file|
+          @levels.each do |key, value|
+            file.puts "#{value} #{key}"
+          end
         end
+        debug "Officializing levels.rbot ..."
+        File.rename("#{@bot.botclass}/levels.rbot.new",
+                    "#{@bot.botclass}/levels.rbot")
+      rescue
+        error "failed to write configuration file levels.rbot! #{$!}"
+        error "#{e.class}: #{e}"
+        error e.backtrace.join("\n")
       end
     end