]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/quotes.rb
Module\#define_structure method: define a new Struct only if doesn't exist already...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / quotes.rb
index e08d8f9ac4ec83b61c368614eb88852cdd119063..b5017a58852fe121472a7011db787511495e613d 100644 (file)
@@ -1,12 +1,14 @@
 # GB: Ok, we *really* need to switch to db for this plugin too
 
-Quote = Struct.new("Quote", "num", "date", "source", "quote")
+define_structure :Quote, :num, :date, :source, :quote
 
 class QuotePlugin < Plugin
   def initialize
     super
     @lists = Hash.new
+    @changed = Hash.new
     Dir["#{@bot.botclass}/quotes/*"].each {|f|
+      next if File.directory?(f)
       channel = File.basename(f)
       @lists[channel] = Array.new if(!@lists.has_key?(channel))
       IO.foreach(f) {|line|
@@ -15,33 +17,40 @@ class QuotePlugin < Plugin
           @lists[channel][num] = Quote.new(num, $2, $3, $4)
         end
       }
+      @changed[channel] = false
     }
   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|
       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}"
+        if @changed[channel]
+          debug "Writing new quotefile for channel #{channel} ..."
+          Utils.safe_save("#{@bot.botclass}/quotes/#{channel}") {|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}")
+          @changed[channel] = false
+        else
+          debug "Not writing quotefile for channel #{channel} (unchanged)"
+        end
       rescue => e
-        $stderr.puts "failed to write quotefile for channel #{channel}!\n#{$!}"
-        debug "#{e.class}: #{e}"
-        debug e.backtrace.join("\n")
+        error "failed to write quotefile for channel #{channel}!\n#{$!}"
+        error "#{e.class}: #{e}"
+        error e.backtrace.join("\n")
       end
     }
   end
+  def cleanup
+    @lists.clear
+    @changed.clear
+  end
   def addquote(source, channel, quote)
     @lists[channel] = Array.new if(!@lists.has_key?(channel))
     num = @lists[channel].length 
-    @lists[channel][num] = Quote.new(num, Time.new, source, quote)
+    @lists[channel][num] = Quote.new(num, Time.new, source.fullform, quote)
+    @changed[channel] = true
     return num
   end
   def getquote(source, channel, num=nil)
@@ -63,6 +72,7 @@ class QuotePlugin < Plugin
     if(@lists[channel][num])
       @lists[channel][num] = nil
       @lists[channel].pop if num == @lists[channel].length - 1
+      @changed[channel] = true
       return true
     end
     return false
@@ -237,12 +247,12 @@ class QuotePlugin < Plugin
       case command
         when (/^addquote\s+(.+)/)
           if(@bot.auth.allow?("addquote", m.source, m.replyto))
-            num = addquote(m.source, m.target, $1)
+            num = addquote(m.source, m.target.to_s, $1)
             m.reply "added the quote (##{num})"
           end
         when (/^getquote$/)
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target)
+            quote, total = getquote(m.source, m.target.to_s)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -252,7 +262,7 @@ class QuotePlugin < Plugin
         when (/^getquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -262,7 +272,7 @@ class QuotePlugin < Plugin
         when (/^whenquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "quote #{quote.num} added on #{quote.date}"
             else
@@ -272,7 +282,7 @@ class QuotePlugin < Plugin
         when (/^whoquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               m.reply "quote #{quote.num} added by #{quote.source}"
             else
@@ -281,7 +291,7 @@ class QuotePlugin < Plugin
           end
         when (/^topicquote$/)
           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target)
+            quote, total = getquote(m.source, m.target.to_s)
             if(quote)
               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
             else
@@ -291,7 +301,7 @@ class QuotePlugin < Plugin
         when (/^topicquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("topicquote", m.source, m.replyto))
-            quote, total = getquote(m.source, m.target, num)
+            quote, total = getquote(m.source, m.target.to_s, num)
             if(quote)
               @bot.topic m.target, "[#{quote.num}] #{quote.quote}"
             else
@@ -301,7 +311,7 @@ class QuotePlugin < Plugin
         when (/^delquote\s+(\d+)$/)
           num = $1.to_i
           if(@bot.auth.allow?("delquote", m.source, m.replyto))
-            if(delquote(m.target, num))
+            if(delquote(m.target.to_s, num))
               m.okay
             else
               m.reply "quote not found!"
@@ -310,7 +320,7 @@ class QuotePlugin < Plugin
         when (/^searchquote\s+(.*)$/)
           reg = $1
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            quote, total = searchquote(m.source, m.target, reg)
+            quote, total = searchquote(m.source, m.target.to_s, reg)
             if(quote)
               m.reply "[#{quote.num}] #{quote.quote}"
             else
@@ -320,7 +330,7 @@ class QuotePlugin < Plugin
         when (/^countquote(?:\s+(.*))?$/)
           reg = $1
           if(@bot.auth.allow?("getquote", m.source, m.replyto))
-            total = countquote(m.source, m.target, reg)
+            total = countquote(m.source, m.target.to_s, reg)
             if(reg && reg.length > 0)
               m.reply "#{total} quotes match: #{reg}"
             else