]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Improve robustness while saving auth config files and channel quotes
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 18 Jul 2006 14:44:17 +0000 (14:44 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Tue, 18 Jul 2006 14:44:17 +0000 (14:44 +0000)
data/rbot/plugins/quotes.rb
lib/rbot/auth.rb
lib/rbot/config.rb

index 10743dfa5aea4c3ece0405ffee5a475c4a03842e..e08d8f9ac4ec83b61c368614eb88852cdd119063 100644 (file)
@@ -1,3 +1,5 @@
+# GB: Ok, we *really* need to switch to db for this plugin too
+
 Quote = Struct.new("Quote", "num", "date", "source", "quote")
 
 class QuotePlugin < Plugin
@@ -17,12 +19,23 @@ class QuotePlugin < Plugin
   end
   def save
     Dir.mkdir("#{@bot.botclass}/quotes") if(!FileTest.directory?("#{@bot.botclass}/quotes"))
+    Dir.mkdir("#{@bot.botclass}/quotes/new") if(!FileTest.directory?("#{@bot.botclass}/quotes/new"))
     @lists.each {|channel, quotes|
-      File.open("#{@bot.botclass}/quotes/#{channel}", "w") {|file|
-        quotes.compact.each {|q| 
-          file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
+      begin
+        debug "Writing new quotefile for channel #{channel} ..."
+        File.open("#{@bot.botclass}/quotes/new/#{channel}", "w") {|file|
+          quotes.compact.each {|q| 
+            file.puts "#{q.num} | #{q.date} | #{q.source} | #{q.quote}"
+          }
         }
-      }
+        debug "Officializing quotefile for channel #{channel} ..."
+        File.rename("#{@bot.botclass}/quotes/new/#{channel}",
+                    "#{@bot.botclass}/quotes/#{channel}")
+      rescue => e
+        $stderr.puts "failed to write quotefile for channel #{channel}!\n#{$!}"
+        debug "#{e.class}: #{e}"
+        debug e.backtrace.join("\n")
+      end
     }
   end
   def addquote(source, channel, quote)
@@ -41,7 +54,7 @@ class QuotePlugin < Plugin
     else
       # random quote
       return @lists[channel].compact[rand(@lists[channel].nitems)],
-                                       @lists[channel].length - 1
+      @lists[channel].length - 1
     end
   end
   def delquote(channel, num)
@@ -76,31 +89,31 @@ class QuotePlugin < Plugin
     return nil unless(@lists[channel].length > 0)
     matches = @lists[channel].compact.find_all {|a| a.quote =~ /#{regexp}/i }
     if(matches.length > 0)
-       return matches[rand(matches.length)], @lists[channel].length - 1
+      return matches[rand(matches.length)], @lists[channel].length - 1
     else
       return nil
     end
   end
   def help(plugin, topic="")
     case topic
-      when "addquote"
-        return "addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !addquote without addressing if so configured"
-      when "delquote"
-        return "delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !delquote without addressing if so configured"
-      when "getquote"
-        return "getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be returned. Responds to !getquote without addressing if so configured"
-      when "searchquote"
-        return "searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !searchquote without addressing if so configured"
-      when "topicquote"
-        return "topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be set. Responds to !topicquote without addressing if so configured"
-      when "countquote"
-        return "countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !countquote without addressing if so configured"
-      when "whoquote"
-        return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
-      when "whenquote"
-        return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
-      else
-        return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
+    when "addquote"
+      return "addquote [<channel>] <quote> => Add quote <quote> for channel <channel>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !addquote without addressing if so configured"
+    when "delquote"
+      return "delquote [<channel>] <num> => delete quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !delquote without addressing if so configured"
+    when "getquote"
+      return "getquote [<channel>] [<num>] => get quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be returned. Responds to !getquote without addressing if so configured"
+    when "searchquote"
+      return "searchquote [<channel>] <regexp> => search for quote from <channel> that matches <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !searchquote without addressing if so configured"
+    when "topicquote"
+      return "topicquote [<channel>] [<num>] => set topic to quote from <channel> with number <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Without <num>, a random quote will be set. Responds to !topicquote without addressing if so configured"
+    when "countquote"
+      return "countquote [<channel>] <regexp> => count quotes from <channel> that match <regexp>. You only need to supply <channel> if you are addressing #{@bot.nick} privately. Responds to !countquote without addressing if so configured"
+    when "whoquote"
+      return "whoquote [<channel>] <num> => show who added quote <num>. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
+    when "whenquote"
+      return "whenquote [<channel>] <num> => show when quote <num> was added. You only need to supply <channel> if you are addressing #{@bot.nick} privately"
+    else
+      return "Quote module (Quote storage and retrieval) topics: addquote, delquote, getquote, searchquote, topicquote, countquote, whoquote, whenquote"
     end
   end
   def listen(m)
index 3fbf4b697b4dbf4a7dec3afad67b80c8eca1d5cb..04e1e98b001a80fbd5f60b91ec67576c848f3063 100644 (file)
@@ -60,13 +60,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
+        $stderr.puts "failed to write configuration file users.yaml! #{$!}"
+        debug "#{e.class}: #{e}"
+        debug 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
+        $stderr.puts "failed to write configuration file levels.rbot! #{$!}"
+        debug "#{e.class}: #{e}"
+        debug e.backtrace.join("\n")
       end
     end
 
index 027b99023443c1b870a9e432343400e7590d5a07..c974c78f2dd08b0137f04e67fdf034f0aa3d5495 100644 (file)
@@ -328,13 +328,17 @@ module Irc
     # write current configuration to #{botclass}/conf.rbot
     def save
       begin
+        debug "Writing new conf.yaml ..."
         File.open("#{@@bot.botclass}/conf.yaml.new", "w") do |file|
           file.puts @@config.to_yaml
         end
+        debug "Officializing conf.yaml ..."
         File.rename("#{@@bot.botclass}/conf.yaml.new",
                     "#{@@bot.botclass}/conf.yaml")
-      rescue
+      rescue => e
         $stderr.puts "failed to write configuration file conf.yaml! #{$!}"
+        debug "#{e.class}: #{e}"
+        debug e.backtrace.join("\n")
       end
     end